【发布时间】:2019-03-07 18:16:03
【问题描述】:
我在使用 Excel 中的 VBA 自动填充某些代码时遇到问题。我不断收到错误“范围类的自动填充方法失败”,但我不确定代码有什么问题。对我来说看起来不错,但也许我错过了什么?代码如下:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
A = MsgBox("Do you really want to save the workbook?", vbYesNo)
If A = vbNo Then Cancel = True
Dim lrow As Long
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:A" & lastrow)
Range("A1:A" & lastrow).Select
'Range("D1").Select
'Selection.AutoFill Destination:=Range("D1:D" & lastrow)
'Range("D1:D" & lastrow).Select
'Range("H1").Select
'Selection.AutoFill Destination:=Range("H1:H" & lastrow)
'Range("H1:H" & lastrow).Select
'Range("L1").Select
'Selection.AutoFill Destination:=Range("L1:L" & lastrow)
'Range("L1:L" & lastrow).Select
End Sub
我们将不胜感激任何正确方向的帮助。 (请注意,我只是想修复 A 列,我想如果我能正常工作,其余部分将符合要求)。 谢谢!!! -D
【问题讨论】:
-
你打算在这里做什么?您的代码似乎没问题,但如果用户所在的工作表是您需要的工作表,它将起作用。有更好的方法可以做到这一点,但我们需要更多信息。
-
这应该是一个简单的代码,它基本上会在保存电子表格时自动填充显示的列(A、D、H 和 L)。
-
Dim lastrow As Long -
使用
Option Explicit。你可能想看看To ‘Err’ is Human -
然后将
lrow = .Range("A" & .Rows.Count).End(xlUp).Row替换为lrow = .Range("B" & .Rows.Count).End(xlUp).Row