首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在编写VBA时,我无法获得两个要运行的特定错误

在编写VBA时,我无法获得两个要运行的特定错误
EN

Stack Overflow用户
提问于 2019-03-07 03:41:49
回答 1查看 37关注 0票数 0

我在第一行得到了无效的代码使用,并且在输入我的对象作为范围时,我得到了对象'_Worksheet‘的方法' range’失败。有人能帮上忙吗?

代码语言:javascript
复制
Sub SplitandFilterSheet()

    Dim SplitOrderNum As Range

    Sheets("Sum To Line Item").Select

    Set SplitOrderNum = Range("SplitOrderNum")

    For Each cell In SplitOrderNum
        Sheets("Sum To Line Item").Copy After:=Worksheets(Sheets.Count)
        ActiveSheet.Name = cell.Value

        With ActiveWorkbook.Sheets(cell.Value).Range("OrderData")
            .AutoFilter Field:=2, Criteria1:="<>" & cell.Value, Operator:=xlFilterValues
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        ActiveSheet.AutoFilter.ShowAllData
    Next cell
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 04:21:35

尝试此代码,阅读其中的注释,并查找<<<< Customize this >>>行:

代码语言:javascript
复制
Sub SplitandFilterSheet()

    ' Declare objects
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim splitOrderNum As Range
    Dim sourceCell As Range

    ' Declare other variables
    Dim sourceSheetName As String
    Dim sourceRangeName As String
    Dim targetSheetName As String
    Dim targetRangeName As String

    Dim lastSheetHidden As Boolean


    ' <<< Customize this >>>
    sourceSheetName = "Sum To Line Item"
    sourceRangeName = "SplitOrderNum"
    targetRangeName = "OrderData"

    ' Initialize the source sheet
    Set sourceSheet = ThisWorkbook.Sheets(sourceSheetName)

    ' Initialize the range (Add full qualifier to the current workbook, sheet and range)
    Set splitOrderNum = sourceSheet.Range(sourceRangeName)

    ' Get if last sheet is visible in current workbook
    lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible
    ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True

    For Each sourceCell In splitOrderNum
        ' Copy the source worksheet
        sourceSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count)
        ' Rename the new worksheet
        Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value
        ' Reference to the added worksheet
        Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

        With targetSheet.Range(targetRangeName)
            .AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value, Operator:=xlFilterValues
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        ' Check this next line if this should point to the orderdata range too (?)
        targetSheet.AutoFilter.ShowAllData

    Next sourceCell

    ' Return the last sheet visible state
    If lastSheetHidden = False Then
        ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden
    End If

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55031015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档