简体   繁体   中英

VBA- how to reset defined variable

I have a code as below and i want to reset "add" as 1 when a new loop starts for the next row.

Sub Group()
    DataBook = ThisWorkbook.Name
    Dim i As Long
    RowCount = 1
    RowCount2 = 1
    Add = 1
    For i = 19 To 1819
        If Workbooks(DataBook).Sheets("Template").Range("G" & i).IndentLevel = 8 Then
            Workbooks(DataBook).Sheets("Sheet1").Cells(RowCount, 2) = Workbooks(DataBook).Sheets("Template").Range("G" & i).Value
            RowCount = RowCount + 1
            If Workbooks(DataBook).Sheets("Template").Range("G" & i - Add).IndentLevel = 7 Then
                Workbooks(DataBook).Sheets("Sheet1").Cells(RowCount2, 1) = Workbooks(DataBook).Sheets("Template").Range("G" & i - Add)
                RowCount2 = RowCount2 + 1
                Add = Add + 1
            End If
        End If
    Next i
End Sub

Switch these two lines:

Add = 1
For i = 19 To 1819

Ie:

For i = 19 To 1819
    Add = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM