帮助嵌套 if/Loop VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6599776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:36:29  来源:igfitidea点击:

Help with nested if/Loop VBA

excelvbanested-loops

提问by jz3

I'm in the process of looping through an Excel spreadsheet and combining all the cells into a string, which I did. Now I need to format the string with XML tags before I send it for upload, and I'm having some difficulty working the tagging into the loop correctly. It seems like it is almost working, but a few of the tags are not going in the correct place. Any help would be much appreciated.

我正在遍历 Excel 电子表格并将所有单元格组合成一个字符串,我做到了。现在,我需要在发送以进行上传之前使用 XML 标记格式化字符串,并且在将标记正确地处理到循环中时遇到了一些困难。看起来它几乎可以正常工作,但是一些标签没有放在正确的位置。任何帮助将非常感激。

Code:

代码:

Public file As String

Sub locate_file()

Dim sheet1_95 As String
Dim theRange As Range
Dim strVal As String
Dim wb As Workbook
Dim counterDT As Integer
Dim counterSVR As Integer
Dim counterMB As Integer

Dim outputStr As String


'prompt user for location of other excel sheet'
file = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")

Set wb = Workbooks.Open(file)

Dim cell As Range

'initializing the xml string'
strVal = "<root>"

Sheets("DT").Activate

counterDT = 1

For Each cell In ActiveSheet.UsedRange.Cells
'this first if-block is just excluding the few header cells from the data collection'
  If cell.Value <> "SKU" And cell.Value <> "P Number" And cell.Value <> "Month" _
     And cell.Value <> "DP Dmd" And cell.Value <> "Vertical" Then
    If cell.Column = "1" Then
      strVal = strVal & "<item><sku>" & cell.Value & "</sku>"
    ElseIf cell.Column = "2" Then strVal = strVal & "<pnum>" & cell.Value & "</pnum>"
    ElseIf cell.Column = "3" Then strVal = strVal & "<month>" & cell.Value & "</month>"
    ElseIf cell.Column = "4" Then strVal = strVal & "<forecast>" & cell.Value & "</forecast>"
    Else: strVal = strVal & "<vertical>" & cell.Value & "</vertical>"
    End If

    counterDT = counterDT + 1

    If cell.Row <> 1 Then
      If counterDT Mod 6 = 0 Then
        strVal = "<item>" & strVal & "<percent>" & category.percent(cell, "DT") & "</percent>"
       Else: End If
    Else: End If
  End If
Next

strVal = strVal & "</root>"

So basically the problem is, this loop/nested if is printing like 30 "item" tags at the very beginning of the string and I'm not sure why.

所以基本上问题是,这个循环/嵌套 if 在字符串的开头打印了 30 个“项目”标签,我不知道为什么。

For some other information, the Excel sheet is 6 columns, and will always be 6.

对于其他一些信息,Excel 工作表有 6 列,并且始终为 6。

回答by Dick Kusleika

When I'm creating xml tags, I like to move the actual tagging into a separate function. The upside is that it ensures my tags match. The downside is that you don't "apply" the tags until the end. Tags like item and root are done after all the tags within them are done. Here's an example:

当我创建 xml 标签时,我喜欢将实际的标签移动到一个单独的函数中。好处是它可以确保我的标签匹配。缺点是你直到最后才“应用”标签。像 item 和 root 这样的标签是在它们中的所有标签都完成后完成的。下面是一个例子:

Sub locate_file()

    Dim sVal As String
    Dim sRow As String
    Dim wb As Workbook
    Dim sh As Worksheet
    Dim lCntDT As Long
    Dim rCell As Range
    Dim rRow As Range
    Dim vaTags As Variant

    gsFile = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")

    If gsFile <> "False" Then
        Set wb = Workbooks.Open(gsFile)
        Set sh = wb.Sheets("DT")
        vaTags = Array("sku", "pnum", "month", "forecast", "vertical")

        lCntDT = 1

        For Each rRow In sh.UsedRange.EntireRow
            sRow = ""
            If rRow.Cells(1) <> "SKU" Then
                For Each rCell In Intersect(sh.UsedRange, rRow).Cells
                    If rCell.Column <= 4 Then
                        sRow = sRow & TagValue(rCell.Value, vaTags(rCell.Column - 1))
                    Else
                        sRow = sRow & TagValue(rCell.Value, vaTags(UBound(vaTags)))
                    End If
                Next rCell

                lCntDT = lCntDT + 1
                If rRow.Row <> 1 And lCntDT Mod 6 = 0 Then
                    sVal = sVal & TagValue("CatPct", "percent")
                End If
                sRow = TagValue(sRow, "item")
                sVal = sVal & sRow & vbNewLine
            End If
        Next rRow
        sVal = TagValue(sVal, "root")
    End If

    Debug.Print sVal

End Sub

Function TagValue(ByVal sValue As String, ByVal sTag As String) As String

    TagValue = "<" & sTag & ">" & sValue & "</" & sTag & ">"

End Function