我的GoTo不会跳到Reve:,但是会跳到Gen:。我不知道为什么它不起作用。问题是,当我在单元格C22中输入41010020时,它只是清除了在Reve:中调用的单元格。它不会在Reve:上继续。我认为代码中的逻辑是正确的,但我不知道为什么它不起作用。
Sub ValidateCode2()
Dim sto As String, cod As String, rev As String, cog As String, dis As String, brandlist As String, prodlist As String
Dim codres As String, revres As String, cogres As String, disres As String, stores As String
Dim brandresult As String, prodresult As String
Dim i As Long, x As Long
Dim wb As Workbook: Set wb = ThisWorkbook
sto = wb.Worksheets("Item Groups form").Range("C26").Value
cog = wb.Worksheets("Item Groups form").Range("F22").Value
rev = wb.Worksheets("Item Groups form").Range("C22").Value
dis = wb.Worksheets("Item Groups form").Range("F23").Value
cod = wb.Worksheets("Item Groups form").Range("C18").Value
brandlist = wb.Worksheets("Item Groups form").Range("C19").Value
prodlist = wb.Worksheets("Item Groups form").Range("D19").Value
If wb.Worksheets("Item Groups form").Range("C22").Value = 41010020 Then GoTo Reve Else
For i = 2 To 13
If sto = wb.Worksheets("Validation").Range("B" & i).Value Then codres = wb.Worksheets("Validation").Range("D" & i).Value
Next
For i = 2 To 13
If sto = wb.Worksheets("Validation").Range("B" & i).Value Then revres = wb.Worksheets("Validation").Range("E" & i).Value
Next
For i = 2 To 13
If sto = wb.Worksheets("Validation").Range("B" & i).Value Then cogres = wb.Worksheets("Validation").Range("F" & i).Value
Next
For i = 2 To 13
If sto = wb.Worksheets("Validation").Range("B" & i).Value Then disres = wb.Worksheets("Validation").Range("G" & i).Value
Next
'Brand
For i = 2 To 5000
If brandlist = wb.Worksheets("Validation").Range("P" & i).Value Then brandresult = wb.Worksheets("Validation").Range("Q" & i).Value
Next
'Product
For i = 2 To 32
If prodlist = wb.Worksheets("Validation").Range("U" & i).Value Then prodresult = wb.Worksheets("Validation").Range("V" & i).Value
Next
GoTo Gen
Reve:
'Brand
For i = 2 To 5000
If brandlist = wb.Worksheets("Validation").Range("P" & i).Value Then brandresult = wb.Worksheets("Validation").Range("Q" & i).Value
Next
'Product
For i = 2 To 32
If prodlist = wb.Worksheets("Validation").Range("U" & i).Value Then prodresult = wb.Worksheets("Validation").Range("V" & i).Value
Next
wb.Worksheets("Item Groups form").Range("C18").Value = wb.Worksheets("Validation").Range("D3").Value + prodresult + brandresult
wb.Worksheets("Item Groups form").Range("C22").Value = wb.Worksheets("Validation").Range("E3").Value
wb.Worksheets("Item Groups form").Range("F22").Value = wb.Worksheets("Validation").Range("F3").Value
wb.Worksheets("Item Groups form").Range("F23").Value = wb.Worksheets("Validation").Range("G3").Value
wb.Worksheets("Item Groups form").Range("C26").Value = wb.Worksheets("Validation").Range("B3").Value
Gen:
wb.Worksheets("Item Groups form").Range("C18").Value = codres + prodresult + brandresult
wb.Worksheets("Item Groups form").Range("C22").Value = revres
wb.Worksheets("Item Groups form").Range("F22").Value = cogres
wb.Worksheets("Item Groups form").Range("F23").Value = disres
End Sub
答案 0 :(得分:0)
我建议您看一下BigBen对意大利面条代码的评论,因为有了这种结构,它很快就会变得混乱起来...
我认为您遇到的问题可以追溯到IF THEN结构。
您需要像这样在几行中重写它:
If wb.Worksheets("Item Groups form").Range("C22").Value = 41010020 Then
GoTo Reve
Else
...
End if
否则它会产生一个错误代码...您收到错误代码了吗?