"如果没有结束则阻止"或者"没有下一个"

时间:2014-10-16 16:17:00

标签: vba excel-vba excel

我不能让错误消失,它只会变成一个不同的错误。顶部的IF结束。 FORs全部对齐。如果我添加一个结束IF'在" End Sub"之前,我收到错误"因为没有下一个"。 这是我的代码:

 Sub Updatevalue()

    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim ws4 As Worksheet

    wbname1 = Range("IllustWBDir1")

    Set wb1 = Application.Workbooks.Open(wbname1)

    ThisWorkbook.Activate

    For Item = 0 To Sheets("Documentation").ListBox1.ListCount - 1

      If Sheets("Documentation").ListBox1.Selected(Item) = True Then

          If Sheets("Documentation").ListBox1.List(Item) = "Compact" Then
             Range("Statename") = "MA"
             stname = "C"
          Else
             Range("Statename") = Sheets("Documentation").ListBox1.List(Item)
             stname = Range("Statename")
          End If

          Range("Statename").Copy
          wb1.Activate
          Sheets("Inputs").Select
          Range("State").Select
              Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
          Application.Calculate

          Set ws1 = wb1.Sheets("PPGs")
          Set tb1 = ws1.Range("PPG_Table")

          ThisWorkbook.Activate


    Base_Schematic = ThisWorkbook.Names("Base_Schematic").RefersToRange


' Key Ages
Dim ages(1 To 8) As String
    ages(1) = "30"
    ages(2) = "40"
    ages(3) = "50"
    ages(4) = "55"
    ages(5) = "60"
    ages(6) = "65"
    ages(7) = "70"
    ages(8) = "75"

' Gender
Dim uniorgd(1 To 3) As String
    uniorgd(1) = "U"
    uniorgd(2) = "F"
    uniorgd(3) = "M"

' Bps
Dim Bps(1 To 6) As String
    Bps(1) = "1"
    Bps(2) = "2"
    Bps(3) = "3"
    Bps(4) = "4"
    Bps(5) = "5"
    Bps(6) = "6"

' UW Classes
Dim UWs(1 To 4) As String
    UWs(1) = "P"
    UWs(2) = "S"
    UWs(4) = "1"
    UWs(5) = "2"

' Marital Status
Dim Mar(1 To 2) As String
    Mar(1) = "S"
    Mar(2) = "M"

' Inflations
Dim Infls(1 To 2) As String
    Infls(1) = "3C_PPG"
    Infls(2) = "5C_PPG"


For a = 1 To 8
  For b = 1 To 3
    For c = 1 To 6
      For d = 1 To 4
        For e = 1 To 2
          For f = 1 To 2

            findval = ages(a) & uniorgd(b) & Bps(c) & UWs(d) & Mar(e) & Infls(f)
            wb1.Activate
            Sheets("PPGs").Select
            pasteval = Application.WorksheetFunction.VLookup(Right(findval, 10), Range("PPG_Table"), Range("2,84"), False)

          Next f
        Next e
      Next d
    Next c
  Next b
Next a


End Sub

2 个答案:

答案 0 :(得分:7)

第一个 For 似乎缺少下一步,第一 结束 p>

因为您了解逻辑,所以您应该能够正确地放置它们。

Sub Updatevalue()
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim ws4 As Worksheet

    wbname1 = Range("IllustWBDir1")

    Set wb1 = Application.Workbooks.Open(wbname1)

    ThisWorkbook.Activate

    For Item = 0 To Sheets("Documentation").ListBox1.ListCount - 1
        If Sheets("Documentation").ListBox1.Selected(Item) = True Then
            If Sheets("Documentation").ListBox1.List(Item) = "Compact" Then
                Range("Statename") = "MA"
                stname = "C"
            Else
                Range("Statename") = Sheets("Documentation").ListBox1.List(Item)
                stname = Range("Statename")
            End If

            Range("Statename").Copy
            wb1.Activate
            Sheets("Inputs").Select
            Range("State").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                        :=False, Transpose:=False
            Application.Calculate

            Set ws1 = wb1.Sheets("PPGs")
            Set tb1 = ws1.Range("PPG_Table")

            ThisWorkbook.Activate

            Base_Schematic = ThisWorkbook.Names("Base_Schematic").RefersToRange

            ' Key Ages
            Dim ages(1 To 8) As String
            ages(1) = "30"
            ages(2) = "40"
            ages(3) = "50"
            ages(4) = "55"
            ages(5) = "60"
            ages(6) = "65"
            ages(7) = "70"
            ages(8) = "75"

            ' Gender
            Dim uniorgd(1 To 3) As String
            uniorgd(1) = "U"
            uniorgd(2) = "F"
            uniorgd(3) = "M"

            ' Bps
            Dim Bps(1 To 6) As String
            Bps(1) = "1"
            Bps(2) = "2"
            Bps(3) = "3"
            Bps(4) = "4"
            Bps(5) = "5"
            Bps(6) = "6"

            ' UW Classes
            Dim UWs(1 To 4) As String
            UWs(1) = "P"
            UWs(2) = "S"
            UWs(4) = "1"
            UWs(5) = "2"

            ' Marital Status
            Dim Mar(1 To 2) As String
            Mar(1) = "S"
            Mar(2) = "M"

            ' Inflations
            Dim Infls(1 To 2) As String
            Infls(1) = "3C_PPG"
            Infls(2) = "5C_PPG"


            For a = 1 To 8
                For b = 1 To 3
                    For c = 1 To 6
                        For d = 1 To 4
                            For e = 1 To 2
                                For f = 1 To 2
                                    findval = ages(a) & uniorgd(b) & Bps(c) & UWs(d) & Mar(e) & Infls(f)
                                    wb1.Activate
                                    Sheets("PPGs").Select
                                    pasteval = Application.WorksheetFunction.VLookup(Right(findval, 10), Range("PPG_Table"), Range("2,84"), False)
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
        End If
    Next
End Sub

这是正确缩进代码的一个很好的理由!

答案 1 :(得分:3)

第一个 For 和第一个 If 没有 Next End If

避免此类问题的一种方法是在编写条件语句后立即编写结束语句,然后在cicle中移动并编写代码。