如何在do while循环中嵌套do while循环?

时间:2019-07-16 12:57:15

标签: excel vba

我是VBA和编码方面的新手,并且正处于创建项目管理工具的初期。

我苦苦挣扎的代码的目的是循环遍历每一列的第一行。如果字符串“ SKA”出现在该行中,则它将从另一张纸上复制模板并将其粘贴到另一张纸上。模板是“新市场跟踪器”。

一旦粘贴了模板(这是“嵌套的do while循环”出现的地方),我试图遍历列中为“是”找到字符串“ SKA”的行。如果找到字符串“是”,则我要使用包含“是”的单元格左侧的单元格1列的值填充粘贴在单独工作表上的模板下的行。字符串“ Yes”左侧的单元格1列中的值是国家/地区的名称。

Sub Adding_New_Markets()

Dim SKA_Country_Column As Integer, Y_N_Column As Integer, Last_Tracker_Column As Integer, _
CurrentRow As Integer, Last_Open_Row As Integer, Last_Tracker_Row As Integer, New_Market_Tracker As Range

SKA_Country_Column = 1
Y_N_Column = 2
Last_Tracker_Column = ThisWorkbook.Worksheets("Markets to Open").UsedRange.Columns.Count
Last_Open_Row = ThisWorkbook.Worksheets("Markets to Open").UsedRange.Rows.Count
Last_Tracker_Row = ThisWorkbook.Worksheets("Market Tracker").UsedRange.Rows.Count
Set New_Market_Tracker = ThisWorkbook.Worksheets("Market Tracker Template").Range("A1:T1")

Do While SKA_Country_Column <= Last_Tracker_Column
    If ThisWorkbook.Worksheets("Markets to Open").Cells(1, SKA_Country_Column).Value = "SKA" Then
        New_Market_Tracker.Copy Sheet6.Range("A1").Offset(Last_Tracker_Row, 0)
    End If
        Do While Y_N_Column <= Last_Open_Row
            With ThisWorkbook.Worksheets("Markets to Open")
                If .Cells(1, Y_N_Column).Value = "Yes" Then
                    Sheet6.Cells(Last_Tracker_Row + 1, 1).Value = .Cells(1, Y_N_Column - 1).Value
                End If
            End With
                Y_N_Column = Y_N_Column + 1
        Loop
    SKA_Country_Column = SKA_Country_Column + 1
    Last_Tracker_Row = Last_Tracker_Row + 1
Loop

End Sub

当我以当前形式运行代码时遇到的问题是,只有“ New Market Tracker”才按需要出现。国家名称没有出现在每个跟踪器下方。我可能会以错误的方式思考这个问题,因此我愿意接受建议。

1 个答案:

答案 0 :(得分:0)

不查看输入和输出数据就很难纠正代码。但是,我试图弄明白这一点:

Sub Adding_New_Markets()
    Dim ewsMarketTracker As Worksheet: Set ewsMarketTracker = ThisWorkbook.Worksheets("Market Tracker")
    Dim ewsMarketsToOpen As Worksheet: Set ewsMarketsToOpen = ThisWorkbook.Worksheets("Markets to Open")
    Dim ewsMarketTemplate As Worksheet: Set ewsMarketTemplate = ThisWorkbook.Worksheets("Market Tracker Template")
    Dim ewsNewSheet As Worksheet: Set ewsNewSheet = Sheet6

    Dim Last_Tracker_Row As Long: Last_Tracker_Row = ewsMarketTracker.UsedRange.Rows.Count ' Very confused: which sheet is this on? write to +0, +1 but increment with 1 only?
    Dim Last_Tracker_Col As Long: Last_Tracker_Col = ewsMarketsToOpen.UsedRange.Columns.Count ' Shouldn't it be on ewsMarketTracker? (based on variable name)
    Dim SKA_Country_Col As Long: For SKA_Country_Col = 1 To Last_Tracker_Col
        If ewsMarketsToOpen.Cells(1, SKA_Country_Col).Value = "SKA" Then
            ewsMarketTemplate.Range("A1:T1").Copy ewsNewSheet.Range("A1").Offset(Last_Tracker_Row, 0)
        End If

        Dim Last_Open_Row As Long: Last_Open_Row = ewsMarketsToOpen.UsedRange.Rows.Count
        Dim Y_N_Column As Long: For Y_N_Column = 2 To Last_Open_Row ' Was not reset to 2 in the original
            With ewsMarketsToOpen
                If .Cells(1, Y_N_Column).Value = "Yes" Then
                    ewsNewSheet.Cells(Last_Tracker_Row + 1, 1).Value = .Cells(1, Y_N_Column - 1).Value
                End If
            End With
        Next Y_N_Column
        Last_Tracker_Row = Last_Tracker_Row + 1
    Next SKA_Country_Col
End Sub

我将检查以下问题:

  1. Last_Tracker_Row和Last_Tracker_Column的名称非常相似,但可以从不同的图纸中读取?这是故意的吗?

  2. Last_Tracker_Row的用途很难看:它指的是哪张纸?您引用的是Last_Tracker_Row + 0和Last_Tracker_Row + 1上的单元格,但仅将其递增1,因此迭代之间必须存在重叠。

  3. Y_N_Column看起来像内部循环的循环变量,但是,仅在外部循环之前进行初始化。因此,在到达Last_Open_Row之后,它将永远不会更改(不会重置为2,因为该指令仅执行一次)。顺便说一下,为此目的,For循环是语义上正确的选择。