Excel Lastrow 使用 vba 自动填充问题

标签 excel vba autofill

我的代码在 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

最佳答案

这就是你正在尝试的吗?

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim Ret As Variant

    Ret = MsgBox("Do you really want to save the workbook?", vbYesNo)

    If Ret = vbNo Then
        Cancel = True
    Else
        '~~> Make these changes only if user wants to save the workbook
        Dim lrow As Long
        Dim ws As Worksheet

        Set ws = ThisWorkbook.Sheets(1)

        With ws
            '~~> If Col B has data then find the last row in Col B
            lrow = .Range("B" & .Rows.Count).End(xlUp).Row

            .Range("A1:A" & lrow).Formula = .Range("A1").Formula
        End With
    End If
End Sub

关于Excel Lastrow 使用 vba 自动填充问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52611403/

相关文章:

safari - jQuery Mobile 禁用 Mobile Safari 自动填充

vba - := and = in Excel VBA 和有什么区别

java - POI CellStyle 似乎未应用

excel - 实现更改事件以检查文本框值的更改并启用 "apply"按钮

excel - 在 VBA 中使用 Substring 和 IndexOf 方法

css - Chrome 自动填充黄色背景位于表单字段中的图标上方

php - 一起使用 php 和 python

vba - 从 visio 2013 文件中删除所有宏

excel - IE11框架通知栏保存按钮

javascript - ASP.NET 部分回发上的 Chrome 自动填充(更新面板)