VBA - 如果单元格为空,则跳过子

时间:2017-12-07 08:42:00

标签: excel vba excel-vba

我有代码,将公式放入区域并且工作正常:

[net]
batch=1
channels=240
height=144
width=240

[convolutional]
filters=16
size=['7', '7', '7']
stride=2
pad=1
activation=relu

[convolutional]
filters=32
size=1
stride=1
pad=1
activation=linear

[route]
layers=-2

[convolutional]
filters=32
size=['3', '3', '3']
stride=1
pad=1
activation=relu

[convolutional]
filters=32
size=['3', '3', '3']
stride=1
pad=1
activation=linear

[shortcut]
from=-4
activation=relu

[maxpool]
size=2
stride=2

[...] # I cropped it here, since file is too lengthy

但是我想添加条件,如果表Crd_Headers上的单元格C2为空,则跳过整个子:

Private Sub Jeeves_account2_C()

Dim lastrow As Long
Dim rng As Range, C As Range

With Worksheets("Crd_Headers") ' <-- here should be the Sheet's name
    lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row in column B
    Set rng = .Range("C2:C" & lastrow) ' set the dynamic range to be searched

    ' loop through all cells in column B
    For Each C In rng
        If Not IsEmpty(C.Value) Then
            C.Offset(, -1).Formula = "=IFERROR(VLOOKUP(RC[2],Jeeves_Cust_list!C[-1]:C[1],3,0),RC[2])" ' use offset to put the formula in column "P"
        End If
    Next C
End With

End Sub

所以代码如下:

If Worksheets("Crd_Headers").Cells("C2") = "" Then
    Exit Sub
End If

但是它给了我错误消息无效的过程调用或参数

你能告诉我,我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

您只是混淆了CellsRange对象引用。
要引用单细胞,您有两种解决方案:

If Worksheets("Crd_Headers").Range("C2") = "" Then

If Worksheets("Crd_Headers").Cells(2, "C") = "" Then