VBA检查范围内的单元格是否为红色然后退出sub

时间:2018-06-04 21:56:27

标签: vba excel-vba excel

我正在尝试创建一个宏来执行对范围的检查,如果单元格被格式化为红色(基于条件格式),则停止子...否则继续。

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

我得到的错误是在线 - 对于每个cel在ws2.range(“A3:V”& lstrow) 我很感激帮助。

1 个答案:

答案 0 :(得分:0)

归因于my other answer以获取详细信息。

请更换代码

cel.Interior.Color

用这个:

cel.DisplayFormat.Interior.Color