按顺序从一个子移动到下一个子,在原始帖子中添加了缺少的子 aMoveHTotals()

问题描述 投票:0回答:1

我使用了不同的方法从一个子移动到下一个或另一个子,没有任何问题。但是我很难让这个工作

我使用过 CALL、GOTO、MOVE RANGE、FIND,但运气不佳。 我想知道我是否与另一个变量或范围发生冲突。 我已经包含了上一个和下一个子中的代码。到目前为止,我的项目中有大约 300 行代码。 SUB ColAInsertrow() 是不会移动到 SUB aMoveHTotals

的 SUB
Sub aGToOColDelete() 
    Range("g1:g1000000").Clear
    Range("i1:i1000000").Clear
    Range("j1:j1000000").Clear
    Range("k1:k1000000").Clear
    Range("l1:l1000000").Clear
    Range("m1:m1000000").Clear
    Range("n1:n1000000").Clear
    Range("o1:o1000000").Clear
    Call ColAInsertrow
End Sub

Sub ColAInsertrow() '===================================yay
    
    'Declare Variables
    Dim LastRow As Long, FirstRow As Long
    Dim Row As Long
    Dim moveHTotalsexit As String
    
    With ActiveSheet
        'Define First and Last Rows
        FirstRow = 1
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        
        'Loop Through Rows (Bottom to Top)
        For Row = LastRow To FirstRow Step -1
            If .Range("A" & Row).Value <> "" Then
                If InStr(.Range("A" & Row).Value, "Transactions for") > 0 Then
                    .Range("A" & Row).EntireRow.Insert
                ElseIf .Range("A" & Row).Value = ("Start") Then GoTo moveHTotalsexit
                    
                End If
            End If
        Next Row
        
    End With
    
moveHTotalsexit:
    'Call aMoveHTotals
    Exit Sub
    Call aMoveHTotals
End Sub

Sub aMoveHTotals()'I LEFT THIS OUT OF MY FIRST POST   ***
    
    Dim ws As Worksheet: Set ws = ActiveSheet'   ***
    Dim rg As Range: Set rg = ws.UsedRange'   ***
    
    aMoveHTotalsx rg, "Date:  8/30/2023", 1, , 0, True'   ***
       
End Sub  '   ***

Sub aMoveHTotals( _
    ByVal searchRange As Range, _
    ByVal SearchPattern As String, _
    ByVal SearchColumn As Long, _
    Optional ByVal RowOffset As Long = 0, _
    Optional ByVal ColumnOffset As Long = 0, _
    Optional ByVal ShowMessages As Boolean = False)
    'Argumented (An Improvement) - A Method
    Const MoveHTotalsX As String = "Totals for date:*"

excel vba call
1个回答
0
投票

Exit Sub
立即退出 Sub 程序。这是之前
Call ..

顺便说一句,最好将

GoTo moveHTotalsexit
保留在新行中,尽管它在您的代码中有效。

        'Loop Through Rows (Bottom to Top)
        For Row = LastRow To FirstRow Step -1
            If .Range("A" & Row).Value <> "" Then
                If InStr(.Range("A" & Row).Value, "Transactions for") > 0 Then
                    .Range("A" & Row).EntireRow.Insert
                ElseIf .Range("A" & Row).Value = "Start" Then 
                    GoTo moveHTotalsexit  ' **                    
                End If
            End If
        Next Row
        
    End With
    
moveHTotalsexit:
    'Call aMoveHTotals
    ' Exit Sub ' **
    Call aMoveHTotals
© www.soinside.com 2019 - 2024. All rights reserved.