下面是我收到错误的代码 如果DOJ =" "然后。错误是"错误13,类型不匹配"
我想检查单元格是否为空,如果为空,则将内部颜色更改为蓝色,但是因为类型不匹配而给出错误。 请帮助解决。
Sub BGVslaCheck()
fpath = Worksheets("Refrence").Range("C9").Value
fname = Worksheets("Refrence").Range("b9").Value & ".xlsx"
Dim Emp1 As Date
Dim DOJ As Date
Dim Cri As Date
Dim Edu As Date
'On Error Resume Next
Workbooks.Open (fpath & fname)
Worksheets("BGV").Select
Set wf = Application.WorksheetFunction
i = Range("a65536").End(xlUp).Row
For J = 11 To i
DOJ = Worksheets("BGV").Range("c" & J).Value
Cri = Worksheets("BGV").Range("g" & J).Value
Edu = Worksheets("BGV").Range("i" & J).Value
Emp1 = Worksheets("BGV").Range("K" & J).Value
a = wf.NetworkDays(DOJ, Cri)
b = wf.NetworkDays(DOJ, Edu)
c = wf.NetworkDays(DOJ, Emp)
Worksheets("BGV").Range("N" & J).Select
If a > 14 Or b > 14 Or c > 14 Then
With Selection.Interior
.Color = VBA.RGB(250, 0, 0)
End With
Else
With Selection.Interior
.Color = VBA.RGB(0, 250, 0)
End With
End If
'If DOJ = " " Or Cri = " " Or Edu = " " Then
If DOJ = " " Then
With Selection.Interior
.Color = VBA.RGB(0, 0, 250)
End With
End If
Next
Workbooks(fname).Close
答案 0 :(得分:1)
DOJ会变暗为日期,而您的支票则是字符串。以下应该交替使用,您可以将其保留为日期,然后执行isempty(DOJ)。
Sub BGVslaCheck()
fpath = Worksheets("Refrence").Range("C9").Value
fname = Worksheets("Refrence").Range("b9").Value & ".xlsx"
Dim Emp1 As Date
Dim DOJ
Dim Cri As Date
Dim Edu As Date
'On Error Resume Next
Workbooks.Open (fpath & fname)
Worksheets("BGV").Select
Set wf = Application.WorksheetFunction
i = Range("a65536").End(xlUp).Row
For J = 11 To i
DOJ = Worksheets("BGV").Range("c" & J).Value
Cri = Worksheets("BGV").Range("g" & J).Value
Edu = Worksheets("BGV").Range("i" & J).Value
Emp1 = Worksheets("BGV").Range("K" & J).Value
a = wf.NetworkDays(DOJ, Cri)
b = wf.NetworkDays(DOJ, Edu)
c = wf.NetworkDays(DOJ, Emp)
Worksheets("BGV").Range("N" & J).Select
If a > 14 Or b > 14 Or c > 14 Then
With Selection.Interior
.Color = VBA.RGB(250, 0, 0)
End With
Else
With Selection.Interior
.Color = VBA.RGB(0, 250, 0)
End With
End If
'you could try: if isempty(DOJ) then
'If DOJ = " " Or Cri = " " Or Edu = " " Then
If DOJ = " " Then
With Selection.Interior
.Color = VBA.RGB(0, 0, 250)
End With
End If
Next
Workbooks(fname).Close