【发布时间】:2021-09-11 23:36:04
【问题描述】:
当我在 VBA Excel 中运行代码时。我得到运行时错误'1004'方法'范围'对象'_工作表失败。 这是代码。
Private Sub Worksheet_Change(ByVal Target As Range)
With Range("A" & firstTickerRow & ":A" & Rows.Count).Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Range("A" & firstTickerRow - 1 & ":A" & Rows.Count)
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
lastRowColA = Cells(Rows.Count, "a").End(xlUp).Row
With Range("A" & firstTickerRow - 1 & ":A" & lastRowColA)
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
End With
With Range("A" & firstTickerRow & ":A" & lastRowColA).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
结束子
【问题讨论】:
-
什么是
firstTickerRow -
哪一行报错了?
-
第一行 @RBarryYoung - 'Private Sub Worksheet_Change(ByVal Target As Range)'
-
您没有为
firstTickerRow赋值,因此它是0并且没有0行。 -
对,正如@ScottCraner 所说,“A0:A99”是一个无效的范围地址,因为没有第 0 行。同样,稍后像“A-1:A88”这样的地址也是无效的。您需要确保构造的范围有效,或者使用错误处理来捕获并跳过错误的引用。