VBA 循环和 if/else 语句有问题
Trouble with VBA loop and if/else statements
我试图让一个包含多个 if/else 语句的循环起作用,但它一直说我有没有 End If 的 If 或我有一个没有 Do 的循环。
任何指点都很好。
以下是我到目前为止所做的,请放轻松我昨天才开始尝试写 vba...
Sub EditTransposeCopy()
Sheets("Altered").Select
Dim count As Long
count = WorksheetFunction.CountA(Range("A6", Range("A6").End(xlDown))) - 1
Do While count > 0
If InStr(1, (Range("A23").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("18").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A18").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:N6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:18").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 19
Else
If InStr(1, (Range("A20").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A16").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:L6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 16
Else
If InStr(1, (Range("A17").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A14").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:J6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 13
Else
If InStr(1, (Range("A15").Value), "£0.00") > 0 Then
Sheets("Altered").Select
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A12").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:H6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:12").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 10
Else
count = count - 10000000
End If
Loop
'
End Sub
提前致谢
使用ElseIf
或用End If
终止每个If
块
Sub EditTransposeCopy()
'...
Do While count > 0
If InStr(1, (Range("A23").Value), "Reason:") > 0 Then
'...
ElseIf InStr(1, (Range("A20").Value), "Reason:") > 0 Then
'...
ElseIf InStr(1, (Range("A15").Value), "£0.00") > 0 Then
'...
Else
'...
End If
Loop
End Sub
欢迎来到 SO,欢迎来到 VBA!
首先,您应该看看 how to avoid using select,因为虽然这是宏记录器的工作方式,但如果您将代码替换为
,则更好的做法(不太容易出现错误)并且更具可读性
Range("A1").Select
Selection.Copy
和
Range("A1").Copy
其次查阅syntax of an if
statement - in particular this part关于Else If
的使用,在上面的代码中会派上用场。每个 If
都需要它自己的 End If
,看起来您在原始代码中遗漏了一对。
我试图让一个包含多个 if/else 语句的循环起作用,但它一直说我有没有 End If 的 If 或我有一个没有 Do 的循环。
任何指点都很好。
以下是我到目前为止所做的,请放轻松我昨天才开始尝试写 vba...
Sub EditTransposeCopy()
Sheets("Altered").Select
Dim count As Long
count = WorksheetFunction.CountA(Range("A6", Range("A6").End(xlDown))) - 1
Do While count > 0
If InStr(1, (Range("A23").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("18").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A18").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:N6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:18").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 19
Else
If InStr(1, (Range("A20").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A16").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:L6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:16").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 16
Else
If InStr(1, (Range("A17").Value), "Reason:") > 0 Then
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A14").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:J6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:14").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 13
Else
If InStr(1, (Range("A15").Value), "£0.00") > 0 Then
Sheets("Altered").Select
Rows("9:11").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A7:A12").Select
Selection.Copy
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A6:H6").Copy
Sheets("Output").Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Altered").Select
Rows("6:12").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
count = count - 10
Else
count = count - 10000000
End If
Loop
'
End Sub
提前致谢
使用ElseIf
或用End If
If
块
Sub EditTransposeCopy()
'...
Do While count > 0
If InStr(1, (Range("A23").Value), "Reason:") > 0 Then
'...
ElseIf InStr(1, (Range("A20").Value), "Reason:") > 0 Then
'...
ElseIf InStr(1, (Range("A15").Value), "£0.00") > 0 Then
'...
Else
'...
End If
Loop
End Sub
欢迎来到 SO,欢迎来到 VBA!
首先,您应该看看 how to avoid using select,因为虽然这是宏记录器的工作方式,但如果您将代码替换为
,则更好的做法(不太容易出现错误)并且更具可读性Range("A1").Select
Selection.Copy
和
Range("A1").Copy
其次查阅syntax of an if
statement - in particular this part关于Else If
的使用,在上面的代码中会派上用场。每个 If
都需要它自己的 End If
,看起来您在原始代码中遗漏了一对。