VBA-如何重置已定义的变量

时间:2018-04-04 09:45:26

标签: excel vba excel-vba

我有一个如下代码,我想在下一行开始新循环时将“add”重置为1。

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

1 个答案:

答案 0 :(得分:2)

切换这两行:

Add = 1
For i = 19 To 1819

I.e:

For i = 19 To 1819
    Add = 1