简体   繁体   中英

VBA check if cells in range are red then exit sub

I'm attempting to create a macro that performs a check on a range that if a cell is formatted as red (based on conditional formatting) then stop the sub... otherwise continue.

Sub O_Upload()

' Keyboard Shortcut: Ctrl+Shift+U

Dim Wb1 As Workbook, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, 
wbactive As Workbook, wsactive As Worksheet, lstrow As Long
Set Wb1 = Workbooks("spreadsheet.xlsx")
Set ws1 = Wb1.Sheets("Stage 1 Trim")
Set ws2 = Wb1.Sheets("Stage 2 Data Validation")
Set ws3 = Wb1.Sheets("_1010u Sheet")
Set wbactive = ActiveWorkbook
Set wsactive = wbactive.Sheets("Data")

wsactive.range("B10").Select
lstrow = Selection.End(xlDown).Row

wsactive.range("A10:V" & lstrow).Copy Destination:=ws1.range("A4")
ws1.range("X4:AS" & lstrow).Copy
ws2.range("A3").PasteSpecial xlPasteValues

'add red validation check
'if red stop
'if green copy to ws3
Dim cel As range
For Each cel In ws2.range("A3:V" & lstrow)
 If cel.Interior.Color = RGB(255, 0, 0) Then
  MsgBox ("Data contains errors!"), vbOKOnly
Exit Sub
End If
Next

ws2.range("A3:V" & lstrow).Copy
ws3.range("B13").PasteSpecial xlPasteValues
MsgBox "Data is ready to be uploaded", vbOKOnly


End Sub

The error i'm getting is on line -For Each cel In ws2.range("A3:V" & lstrow) I appreciate the help.

Attributing my other answer for details.

Kindly replace the code

cel.Interior.Color

with this:

cel.DisplayFormat.Interior.Color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM