【问题标题】:VBA Excel Compare sheets with nested ForVBA Excel 将工作表与嵌套的 For 进行比较
【发布时间】:2017-04-21 06:42:32
【问题描述】:

我想将 NBG_Data 表的每一行与 Comparison_Data 表的每一行进行比较,所以我希望将 NBG_Data 的每一行与 Comparison_Data 表的所有行进行比较,然后转到下一行,直到达到 MAX_Row,问题是我无法嵌套 For 循环(我真的想要)所以我正在尝试 Do 循环,但是当我开始错误是“没有 Do 的循环” 如果有人请告诉我我在 Do 循环中做错了什么或更好地修改 for 以便我可以有一个嵌套的工作 for 循环。

For Row = 2 To MAX_Row


CompMonth = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, SOP).Value
CompMonth = DatePart("m", CompMonth)

CompYear = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, SOP).Value
CompYear = DatePart("yyyy", CompYear)


CompCarmaker = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, Carmaker).Value
CompProject = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, Project).Value
CompFamily = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, Family).Value
CompStatus = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, Status).Value
CompShare = Worksheets(NBG_ComparisonDataWorksheetName).Cells(Row, Share).Value


Do While Row <= 2

NBGMonth = Worksheets(NBG_DataWorksheetName).Cells(Row, SOP).Value
NBGMonth = DatePart("m", NBGMonth)

NBGYear = Worksheets(NBG_DataWorksheetName).Cells(Row, SOP).Value
NBGYear = DatePart("yyyy", NBGYear)

NBGCarmaker = Worksheets(NBG_DataWorksheetName).Cells(Row, Carmaker).Value
NBGProject = Worksheets(NBG_DataWorksheetName).Cells(Row, Project).Value
NBGFamily = Worksheets(NBG_DataWorksheetName).Cells(Row, Family).Value
NBGStatus = Worksheets(NBG_DataWorksheetName).Cells(Row, Status).Value
NBGShare = Worksheets(NBG_DataWorksheetName).Cells(Row, Share).Value

Row = Row + 1


 ' StatusBar Show

 Application.StatusBar = "VerifySumofShares. Progress: " & Row & " of " & MAX_Row


        If (NBGMonth = CompMonth And NBGYear = CompYear And CompCarmaker = NBGCarmaker And CompProject = NBGProject And CompFamily = NBGFamily And NBGStatus <> "LOST" And CompStatus <> "LOST" And CompShare + NBGShare <= 99 And CompShare + NBGShare > 100) Then



            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "A").Value = Row
            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "B").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row, Project).Value
            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "C").Value = GetMonthAndQuarter(Worksheets(NBG_RegionaDataWorksheetName).Cells(Row, SOP).Value)
            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "D").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row, Family).Value
            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "E").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row, Responsible).Value

           ' Region As String
            Region = ""

            'Add any other GeoRegion which is also responsible in the recorded data


            If Worksheets(NBG_DataWorksheetName).Cells(Row, "BC") Then
            Region = Region + "@EMEA"
            End If

            If Worksheets(NBG_DataWorksheetName).Cells(Row, "BD") Then
            Region = Region + "@AMERICAS"
            End If

            If Worksheets(NBG_DataWorksheetName).Cells(Row, "BE") Then
            Region = Region + "@GCSA"
            End If

            If Worksheets(NBG_DataWorksheetName).Cells(Row, "BF") Then
            Region = Region + "@JAPAN&KOREA"
            End If


            Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "F").Value = Region

            'Count the number of the cases recorded


            Issue_SumofSharesCnt = Issue_SumofSharesCnt + 1

            'If there is no items , the Message to show


        ElseIf (Worksheets(NBG_RegionaDataWorksheetName).Cells(Row, SOP).Value = "There are no items to show in this view.") Then


    End If

     Loop Until Row = MAX_Row

Next Row

【问题讨论】:

  • 不良缩进使代码更难阅读,尤其是涉及嵌套结构的代码。 Rubberduck 的 2.x 预发布版本(我将很快发布一个新版本)包括对流行的 Smart Indenter 插件的重写,它可以自动、一致、快速地缩进您的代码给你。

标签: excel vba for-loop do-while


【解决方案1】:

Do While...LoopDo...Loop Until 循环之间存在混合,VBA 不知道如何解释。

任一变化:

    Do While Row <= 2

到:

    Do

创建一个有效的Do...Loop Until 循环。

或改变:

    Loop Until Row = MAX_Row

到:

    Loop

创建一个有效的Do While...Loop 循环。

【讨论】:

  • 非常感谢
  • 代码运行良好,但有一个问题,现在“下一行”(For循环)不起作用,如果你能帮助我。
  • 你不能在循环内部更新当前的外部For- Next循环迭代器(Row)(你在内部Do While Row &lt;= 2循环中有那个Row = Row + 1)。您可能希望将外部 For Row = 2 To MAX_Row - Next Row 循环变成 Do - While Row &lt;= MAX_Row 一个
  • 好的,谢谢:)
猜你喜欢
  • 2020-02-29
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多