具有多个ifs的VBA循环将无法编译,不确定原因

-3 excel vba loops excel-vba excel-2010

我试图将一张纸上的两列数据重新排列到另一张纸上的行中,由第一张纸上的列控制.

我正在浏览代码,使用F8命令对其进行调试,并在最后遇到循环命令的问题.我得到一个"Loop without Do"编译错误,最后用Excel突出显示"Loop Until".

这是它应该做的:

  • "JCX"(10,AttributeRowCounter)单元格和"08-Attribute"(1,AttributeRowCounter)应匹配,并控制"JCX"中哪一行"08-Attribute"(11,AttributeRowCounter)值.

  • "08-Attribute"(9,AttributeRowCounter)值控制"JCX"中的哪一列放置"08-Attribute"(11,AttributeRowCounter)值.

  • 如果在名为AHU-2的"08-Attribute"(1,AttributeRowCounter)中有一个值列表,则"08-Attribute"(9,AttributeRowCounter)中的相应值列表为"CFM,EAT,LAT,RPM和SP" "然后,在(9,AttributeRowCounter)中带有CFM的"08-Attribute"(11,AttributeRowCounter)中的值进入"JCX"(14,TagNumberRow).

  • 具有RPM的(08,AttributeRowCounter)中的"08-Attribute"(11,AttributeRowCounter)中的值进入"JCX"(16,TagNumberRow).

  • 具有SP in(9,AttributeRowCounter)的"08-Attribute"(11,AttributeRowCounter)中的值进入"JCX"(15,TagNumberRow).

  • 如果"08-Attribute"(11,AttributeRowCounter)中的值在(9,AttributeRowCounter)中有"CFM,SP,RPM和Motor HP"以外的值,则(11,AttributeRowCounter)中的值进入"JCX"(21,TagNumberRow)和(9,AttributeRowCounter)中的值进入"JCX"(20,TagNumberRow).

  • 前面的语句循环,将任何其他非"CFM,SP,RPM和Motor HP"值加载到右侧的下一列,根据需要跳过(Name1,Value1,Name2,Value2,Name3,Value3等).

谁知道发生了什么事?

'Tag Values
Do
    With Sheets("08-Attribute")
    Copy.Range("9,AttributeRowCounter").Value
    Application.WorksheetFunction.VLookup((Sheets("JCX").Cells(10, AttributeRowCounter)), (Sheets("08-Attribute").Cells(1, AttributeRowCounter)), True).Value
    With Sheets("08-Attribute")
If "9,AttributeRowCounter" = "CFM" Then
    .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("14,TagNumberRow").Value
    ElseIf "9" = "SP" Then
    .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("15,TagNumberRow").Value
    ElseIf "9" = "RPM" Then
    .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("16,TagNumberRow").Value
    ElseIf "9" = "Motor_HP" Then
    .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("17,TagNumberRow").Value
    Else
    .Range("9,AttributeRowCounter").Value = Sheets("JCX").Range("20,TagNumberRow").Value
    TagValueNameColumn = TagValueNameColumn + 2
    .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("21,TagNumberRow").Value
    TagValueNameColumn = TagValueNameColumn + 2
    Loop Until Cells(1, AttributeRowCounter) = False
Run Code Online (Sandbox Code Playgroud)

Dic*_*ika 6

如果你缩进,问题会变得更加清晰.

Do
    With Sheets("08-Attribute")
        copy.Range("9,AttributeRowCounter").Value
        Application.WorksheetFunction.VLookup((Sheets("JCX").Cells(10, attributerowcounter)), (Sheets("08-Attribute").Cells(1, attributerowcounter)), True).Value

        With Sheets("08-Attribute")
            If "9,AttributeRowCounter" = "CFM" Then
                .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("14,TagNumberRow").Value
            ElseIf "9" = "SP" Then
                .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("15,TagNumberRow").Value
            ElseIf "9" = "RPM" Then
                .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("16,TagNumberRow").Value
            ElseIf "9" = "Motor_HP" Then
                .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("17,TagNumberRow").Value
            Else
                .Range("9,AttributeRowCounter").Value = Sheets("JCX").Range("20,TagNumberRow").Value
                TagValueNameColumn = TagValueNameColumn + 2
                .Range("11,AttributeRowCounter").Value = Sheets("JCX").Range("21,TagNumberRow").Value
                TagValueNameColumn = TagValueNameColumn + 2
            End If 'new
        End With 'new
    End With 'new
Loop Until Cells(1, attributerowcounter) = False
Run Code Online (Sandbox Code Playgroud)

底部附近的三条线'new在末端是关闭If块和两个With块所必需的.