Bob*_*Bob 0 excel vba excel-vba
我不能让错误消失,它只会变成一个不同的错误.顶部的IF结束.FORs全部对齐.如果我在"End Sub"之前添加"End IF",我会收到错误"for next 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
Run Code Online (Sandbox Code Playgroud)
你似乎缺少Next的第一个For和End If for First If
既然您了解逻辑,那么您应该能够正确地放置它们.
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
Run Code Online (Sandbox Code Playgroud)
这是正确缩进代码的一个很好的理由!