我正在嘗試創建一個執行以下操作的 IF 陳述句:
- 突出顯示(紅色)值大于 1 且小于 26 的任何內容,然后繼續執行宏的其余部分并執行其他操作(我已成功完成)。
- 如果值超過 25,則用紅色突出顯示,生成一個訊息框,然后退出(我已成功完成)。
- 如果所有行都 = 1,則什么都不做并退出 sub (我正在努力解決)。

For Each C In Range("B2:B25000").Cells
If C.Value > 1 And C.Value < 26 Then
firstValue = C.Value
firstAddress = C.Address
Exit For
If Not (C.Value > 1 And C.Value < 26) Then Exit Sub 'No
ElseIf C.Value > 25 Then
C.Interior.Color = VBA.ColorConstants.vbRed
MsgBox "Too big!"
Exit Sub
End If
Next
C.Interior.Color = VBA.ColorConstants.vbRed 'if greater than 1 & less than 26 then Color = red
'remaining of the macro goes here
End Sub
uj5u.com熱心網友回復:
使用 if 陳述句設定邏輯標志,然后決定是退出 sub 還是 continue。
Option Explicit
Sub test()
Dim ws As Worksheet, c As Range, lastrow As Long
Dim bAllOnes As Boolean, bTooBig As Boolean
Set ws = Sheet1
bAllOnes = True
bTooBig = False
lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
For Each c In ws.Range("B2:B" & lastrow).Cells
If Val(c.Value) > 1 Then
bAllOnes = False
c.Interior.Color = VBA.ColorConstants.vbRed
If c.Value > 25 Then
bTooBig = True
End If
ElseIf Val(c.Value) < 1 Then
bAllOnes = False
End If
Next
If bTooBig Then
MsgBox "Too big!", vbCritical
Exit Sub
ElseIf bAllOnes Then
MsgBox "All 1's!", vbCritical
Exit Sub
Else
MsgBox "Continueing"
End If
End Sub
uj5u.com熱心網友回復:
我換了這個
If Not (C.Value > 1 And C.Value < 26) Then Exit Sub
有了這個 ElseIf Application.WorksheetFunction.max(Range("b:b")) = 1 Then Exit Sub
它作業得很好
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354004.html
上一篇:使用vba提取xml資訊
