移动工作表许多部分的宏出现语法错误,我哪里出错了?

Macro that moves many sections of my worksheet is getting a syntax error, where did I go wrong?

提问人:Jenny 提问时间:4/2/2022 最后编辑:Jenny 更新时间:4/9/2022 访问量:70

问:

我不知何故打破了几个月前 VBasic2008 给我的这个超级棒的代码。工作簿变得超级慢,可能是因为我没有正确使用它,我只是不停地用螺栓固定在它上面。因此,我刚刚重新创建了它,看看我是否可以简化我的一些添加,以及我是否可以删除任何步骤。 我有一个宏可以运行其他大多数宏,但是当它运行并移动到移动许多不同类型的行的进程的大部分时,我遇到了以前没有的语法错误。它在第一部分(NoAddress)上给了我错误,所以我希望它在以下每个部分上都做同样的事情。


Sub MoveMatchingRows( _
        ByVal SourceWorksheet As Worksheet, _
        ByVal SourceColumn As Long, _
        ByVal SourceCriteria As Variant, _
        ByVal DestinationWorksheet As Worksheet, _
        Optional ByVal DestinationColumn As Long = 1, _
        Optional ByVal DoClearPreviousDestinationData As Boolean = False)
    
    Const ProcTitle As String = "Move Matching Rows"
    
    ' Remove any previous filters.
    If SourceWorksheet.AutoFilterMode Then
        SourceWorksheet.AutoFilterMode = False
    End If
    
    ' Filter.
    Dim srg As Range ' Source Range (Headers and Data)
    Set srg = SourceWorksheet.Range("A1").CurrentRegion
    srg.AutoFilter SourceColumn, SourceCriteria, xlFilterValues
    
    ' Create a reference to the Source Data Range (no headers).
    Dim sdrg As Range
    Set sdrg = srg.Resize(srg.Rows.Count - 1).Offset(1)
    
    ' Clear Destination worksheet.
    If DoClearPreviousDestinationData Then ' new data, copies headers
        DestinationWorksheet.Cells.Clear
    End If
    
    ' Attempt to create a reference to the Source Data Filtered Rows Range.
    Dim sdfrrg As Range
    On Error Resume Next
        Set sdfrrg = sdrg.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    
    If Not sdfrrg Is Nothing Then
        
        ' Create a reference to the Destination Cell (also, add headers).
        Dim dCell As Range ' Destination Cell
        Set dCell = DestinationWorksheet.Cells(1, DestinationColumn)
        If IsEmpty(dCell) Then
            srg.Rows(1).Copy dCell
            Set dCell = dCell.Offset(1)
        Else
            Set dCell = DestinationWorksheet.Cells( _
                DestinationWorksheet.Rows.Count, DestinationColumn) _
                .End(xlUp).Offset(1, 0)
        End If
        
        With sdfrrg
            .Copy dCell
            
            ' Either delete the entire worksheet rows...
            '.EntireColumn.Delete
            
            ' ... or remove filter to prevent...
            SourceWorksheet.AutoFilterMode = False
            ' ... deleting the entire worksheet rows leaving possible data
            ' to the right (after the empty column) intact.
            .Delete xlShiftUp
        
        End With
    
    Else ' no matches
        
        SourceWorksheet.AutoFilterMode = False
   
    End If
        
End Sub
_____________________________________________________________________

Sub NoAddress()
    MoveMatchingRows Sheet1, 6, "=", Sheet12, 1, False
End Sub
________________________________________________

Sub Zoos()
    MoveMatchingRows Sheet1, 4, "*Zoos*", Sheet11, 1, False
End Sub
______________________________________

Sub MoveMemorial()
    MoveMatchingRows Sheet1, 18, "Memorial", Sheet6, 1, False
End Sub
_______________________________________

Sub MoveHonor()
    MoveMatchingRows Sheet1, 18, "Honor", Sheet6, 1, False
End Sub
_______________________

Sub MoveMatchingGift()
    MoveMatchingRows Sheet1, 4, "*Matching Gift*", Sheet9, 1, False
End Sub
______________________

Sub MovePayroll()
    MoveMatchingRows Sheet1, 4, "*Payroll*", Sheet9, 1, False
End Sub
________________________________

Sub NotGenOpFund()
    MoveMatchingRows Sheet1, 23, "<>*FD.IND.GenOp*", Sheet12, 1, False
End Sub
_____________________________________________________________________

Sub GiftMemberships()
    MoveMatchingRows Sheet1, 15, "<>", Sheet10, 1, False
End Sub
_____________________________________________________________________

Sub More_Gift_Mems()
    MoveMatchingRows Sheet1, 25, "*gift for*", Sheet10, 1, False
End Sub
____________________________________________________________________

Sub Gift_Mem_Recipient()
    MoveMatchingRows Sheet1, 31, "<>", Sheet10, 1, False
End Sub
__________________________________

Sub Move_Managed()
    MoveMatchingRows Sheet1, 19, "<>", Sheet5, 1, False
End Sub
___________________________

Sub Stock_InKind_IRA()
    MoveMatchingRows Sheet1, 34, "<>", Sheet7, 1, False
End Sub
_____________________________________________________________________

Sub Move_DAF()
    MoveMatchingRows Sheet1, 42, "<>*/*", Sheet8, 1, False
End Sub
______________________

Sub Oddballs()
    MoveMatchingRows Sheet1, 3, "<> *AF.IND*", Sheet12, 1, False
End Sub
_____________________________


Sub Over_500_Unmanaged()
    MoveMatchingRows Sheet1, 15, ">=500", Sheet4, 1, False
End Sub
_____________________________

Sub Over_250_Unmanaged()
    MoveMatchingRows Sheet1, 15, ">=250", Sheet3, False
End Sub



我在这里做错了什么?

更新 (4.8.22) 我删除了“......1,FALSE“,但我仍然收到一个错误,说子例程可能不可用或所有宏都可能被禁用。当你说要把我所有的数字都加引号时,你不是在谈论引用数据列的数字,是吗?我不认为你是,但我还是尝试过,但没有帮助。你对我接下来可能尝试什么有什么建议吗?

数组 VBA 语法错误

评论

0赞 BigBen 4/2/2022
你真的有子例程吗?这些应该被注释掉。_____________________________
0赞 Jenny 4/2/2022
哎呀!谢谢。但现在它在代码中被挂得更远了。 设置 sdrg = srg。调整大小(srg.Rows.Count - 1)。偏移量(1)'''
0赞 BigBen 4/2/2022
“挂断”具体是什么意思?
0赞 Jenny 4/2/2022
运行时错误“1004”:应用程序定义错误或对象定义错误
0赞 BigBen 4/2/2022
在之前添加并运行 - 即时窗口中的输出是什么?Debug.Print srg.Rows.Count

答:

1赞 VBasic2008 4/4/2022 #1

将筛选的行移动到另一个工作表

  • 这是另一个改进。希望它会比第一次持续更长的时间。
  • 使用数字时会失败,因此请将它们放在引号中,例如."7"
  • xlFilterValues用于允许多个条件,例如 或。Array("4", "7")Array("Yes", "Maybe")
  • 所有示例都使用参数 和 用于最后两个参数。您可以放心地省略它们,因为它们是默认值,即 (和的含义)。1FalseMoveMatchingRows Sheet1, 6, "=", Sheet12Optional... = 1Optional... = False
  • 在您的最后一个示例中,您使用了第 5 个参数而不是合理的正整数,因此它将失败,即使用此新代码,它几乎什么都不做。False
Option Explicit

Sub MoveFilteredRows( _
        ByVal SourceWorksheet As Worksheet, _
        ByVal SourceColumn As Long, _
        ByVal SourceCriteria As Variant, _
        ByVal DestinationWorksheet As Worksheet, _
        Optional ByVal DestinationColumn As Long = 1, _
        Optional ByVal DoClearPreviousDestinationData As Boolean = False)
    Const ProcName As String = "MoveFilteredRows"
    On Error GoTo ClearError
    
    ' Show all rows if the source worksheet is filtered.
    If SourceWorksheet.FilterMode Then SourceWorksheet.ShowAllData
    
    ' Reference the source range (has headers).
    Dim srg As Range ' Source Range (one row of headers and data)
    Set srg = SourceWorksheet.Range("A1").CurrentRegion
    
    ' Show all rows if the destination worksheet is filtered.
    If DestinationWorksheet.FilterMode Then DestinationWorksheet.ShowAllData
    
    ' Reference the destination first cell and take care of the headers.
    Dim dfCell As Range
    If DoClearPreviousDestinationData Then
        DestinationWorksheet.UsedRange.Clear
        Set dfCell = DestinationWorksheet.Cells(1, DestinationColumn)
        srg.Rows(1).Copy dfCell ' copy headers
        If srg.Rows.Count = 1 Then Exit Sub
        Set dfCell = dfCell.Offset(1)
    Else
        If srg.Rows.Count = 1 Then Exit Sub ' don't want to copy headers
        With DestinationWorksheet.Columns(DestinationColumn) _
                .Resize(, srg.Columns.Count)
            Set dfCell = .Find("*", , xlFormulas, , xlByRows, xlPrevious)
            If dfCell Is Nothing Then
                Set dfCell = .Cells(1)
                srg.Rows(1).Copy dfCell ' copy headers anyway
                Set dfCell = dfCell.Offset(1)
            Else
                Set dfCell = .Cells(dfCell.Row + 1, 1)
            End If
        End With
    End If
    
    ' Filter.
    srg.AutoFilter SourceColumn, SourceCriteria, xlFilterValues
    
    ' (Attempt to) reference the source data filtered range.
    Dim sdfrg As Range
    On Error Resume Next
        Set sdfrg = srg.Resize(srg.Rows.Count - 1).Offset(1) _
            .SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    SourceWorksheet.AutoFilterMode = False
    
    ' Move i.e. copy and delete
    If sdfrg Is Nothing Then Exit Sub
    sdfrg.Copy dfCell
    sdfrg.Delete xlShiftUp
    
ProcExit:
    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Sub