【问题标题】:VBA AutoFill - Cell references variableVBA 自动填充 - 单元格引用变量
【发布时间】:2020-12-22 15:37:01
【问题描述】:

我正在尝试将 A 列中的最后一个单元格自动填充到 B 列中的最后一个单元格。

但是,每列的最后一个单元格是可变的。

以下是我当前的代码:

Sub Import()

'Select first empty cell in column B
    Sheets("Sheet1").Select
    Range("B1").End(xlDown).Offset(1, 0).Select

'Paste  Data
    ActiveSheet.Paste
    
'Allocate source_file to pasted data
    Range("A1").End(xlDown).Offset(1, 0).Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "TEMP"

End Sub

我在 A 列中找到了第一个空单元格并输入了“TEMP”。我现在想将其填充到 B 列的最后一行。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: excel vba variables range autofill


    【解决方案1】:

    应该这样做:

        Dim lastrowa As Long
        Dim lastrowb As Long
        
        With Sheet2
            lastrowa = .Cells(.Rows.Count, 1).End(xlUp).Row
            lastrowb = .Cells(.Rows.Count, 2).End(xlUp).Row
            
            .Cells(lastrowa + 1, 1).Value = "TEMP"
            .Cells(lastrowa + 1, 1).AutoFill .Range(.Cells(lastrowa + 1, 1), .Cells(lastrowb, 1))
        End With
    

    【讨论】:

    • 赞赏 Warcupine- 完美运行 :) 不幸的是不能投票,因为我的声誉不够高
    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多