在VBA中,我编写了一个子程序来定义Excel中单元格的边框:
Sub Borderline(cell As Range)
With C.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
但是,当我调用它时,出现错误424:
Sub Sub_1()
Dim C As Range
Set C = Range("e1")
Borderline C
End Sub
为什么?
答案 0 :(得分:1)
长话:
用Borderline C
调用子程序时,参数C
被“翻译”为参数cell
。新的子Sub Borderline(cell As Range)
不知道C
是什么,但是很清楚cell
是什么。虽然它们是相同的。由于不知道C
是什么,因此在此处调用C
-With C.Borders(xlEdgeLeft)
时会引发424错误。
短篇小说:
在VBA旅程的开始,请确保以相同的方式调用参数和参数。因此,如果您使用Borderline C
调用函数,请确保接受的参数是Sub Borderline(C as Range)
,这样就可以减少一个麻烦。
工作代码:
Sub Borderline(c As Range)
With c.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
Sub Sub1()
Dim c As Range
Set c = Range("E11")
Borderline c
End Sub