在EXCEL MACRO中自动填充

时间:2014-07-25 07:55:20

标签: vba excel-vba excel

我正在尝试在Excel中使用宏录制器来录制宏以填充一列单元格,但是因为每次填充的是不同数量的单元格,它要么填充到短或太长,这似乎是因为宏标识了单元格范围及其固定。

我需要的是自动填充或自动填充:

ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1],"" "",RC[2])"
Range("C1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
ActiveCell.Range("A1:A261").Select

由于文件不总是261?如何设置命令来选择/自动填充最后一列?

  

块引用

Sub WMEHOT_Cleaner()
'
' WMEHOT_Cleaner Macro
'

'
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("L:L").Select
    Selection.Cut
    Columns("B:B").Select
    ActiveSheet.Paste
    Columns("C:C").Select
    Selection.Cut
    Range("O1").Select
    ActiveSheet.Paste
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1],"" "",RC[2])"
    Range("C1").Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
    ActiveCell.Range("A1:A261").Select
    Range("C1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.End(xlUp).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("D:E").Select
    Selection.ClearContents
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[2],"" "",RC[6])"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[5],""_"",RC[6])"
    Range("D1").Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
    ActiveCell.Range("A1:A261").Select
    ActiveCell.Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.End(xlUp).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("I:J").Select
    Selection.ClearContents
    Columns("O:O").Select
    Selection.Cut
    Columns("E:E").Select
    ActiveSheet.Paste
    Columns("F:F").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("L:L").Select
    Selection.Cut
    Columns("J:J").Select
    ActiveSheet.Paste
    Range("J1").Select
    Selection.Copy
    Range("L1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "=""WMEOnline_""&RC[9]"
    Range("A1").Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A261")
    ActiveCell.Range("A1:A261").Select
    ActiveCell.Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.End(xlUp).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveCell.Offset(2, 0).Range("A1").Select
    Range("O5").Select




        Cells.Select
    Range("A1").Activate
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste

    msgclean = MsgBox("Cleaning and Sorting Complete!!" & vbNewLine & "File Ready for LMS." & vbNewLine & "Please SAVE this file as CSV Format", vbInformation + vbOKOnly, "WME HOT Cleaner Template")


End Sub

2 个答案:

答案 0 :(得分:0)

使用以下代码:

Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=CONCATENATE(RC[1],"" "",RC[2])"

如果您的列不是A列,则替换A

答案 1 :(得分:0)

使用

lr = Cells(Rows.Count, "A").End(xlUp).Row

查找列的最后一行或

Set wks = ActiveWorkbook.Worksheets(sheet)
i = wks.Range("A:A").End(xlDown).Row
j = wks.Cells.End(xlToRight).Column

找到最后一个单元格的位置。