2014-10-16 43 views
0

我不能讓錯誤消失,它只會變成另一個錯誤。頂部的IF結束。 FORs全部對齊。如果我在「End Sub」之前添加'End IF',我會收到錯誤信息「for without next」。 這裏是我的代碼:「如果沒有結束,則阻止if」或「沒有next」

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 

回答

7

你似乎缺少下一頁第一個對於結束如果的第一如果

因爲你理解其中的邏輯,你應該能夠適當地放置它們。

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 

這是正確縮進代碼的好理由之一!

3

第一和第一如果沒有下一頁結束如果

避免這種問題的一種方法是在寫出條件語句之後立即寫入結束語句,然後在Cicle中移動並寫入代碼。