我试图循环遍历单元格并用编码域号替换某些数字(即 - 6应编码为2,等等)。我在这个VBA中的许多其他地方使用了这种完全相同的方法,它似乎工作得很好。这一部分给出的错误是" Loop Without Do"即使有一个"结束如果"声明。我通过其他问题和答案进行了搜索,似乎无法查明我的错误。任何帮助将不胜感激!
Sub LateralSizeID()
'Column AO
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim nr As Long
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("From AGO")
Set sht2 = wb.Sheets("To MapCall")
nr = 2
Range("AO2").Select
Do Until IsEmpty(ActiveCell)
If sht2.Cells(nr, "AO").Value = "6" Then
sht2.Cells(nr, "AO").Value = "2"
If sht2.Cells(nr, "AO").Value = "4" Then
sht2.Cells(nr, "AO").Value = "1"
If sht2.Cells(nr, "AO").Value = "8" Then
sht2.Cells(nr, "AO").Value = "3"
Else: sht2.Cells(nr, "AO").Value = "2"
End If
nr = nr + 1
ActiveCell.Offset(1, 0).Select
Loop
MainSizeID
End Sub
答案 0 :(得分:3)
你还需要2个End Ifs
Option Explicit
Sub LateralSizeID()
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim nr As Long
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("From AGO")
Set sht2 = wb.Sheets("To MapCall")
nr = 2
Range("AO2").Select
Do Until IsEmpty(ActiveCell)
If sht2.Cells(nr, "AO").Value = "6" Then
sht2.Cells(nr, "AO").Value = "2"
If sht2.Cells(nr, "AO").Value = "4" Then
sht2.Cells(nr, "AO").Value = "1"
If sht2.Cells(nr, "AO").Value = "8" Then
sht2.Cells(nr, "AO").Value = "3"
Else
sht2.Cells(nr, "AO").Value = "2"
End If
nr = nr + 1
ActiveCell.Offset(1, 0).Select
End If
End If
Loop
MainSizeID
End Sub
我认为整件事情可能会变成:
Option Explicit
Public Sub LateralSizeID()
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim nr As Long, rng As Range
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("From AGO")
Set sht2 = wb.Sheets("To MapCall")
With sht2 'change as required
For Each rng In .Range(.Range("AO2"), .Range("AO2").End(xlDown))
Select Case rng.Value
Case 4
rng = 1
Case 8
rng = 3
Case Else
rng = 2
End Select
Next rng
MainSizeID
End With
End Sub