excel macro not working for autofill to various ro

2019-08-18 00:34发布

I am trying to create a macro.

LR = Range("Y3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("Y3:Y" & LR)

LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("R3:R" & LR)

LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("B3:B" & LR)

LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("A3:A" & LR)

My number of rows varies each time I run this. These (4) columns have nothing in rows 1 or 2. Row three is a formula that I want to copy to the last column.

Column C is the only column that will always have information in it all all times for all lines.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-08-18 00:52

use:

Dim LR As Long
With Worksheets("Sheet1") 'Change to your Worksheet name
    LR = .Cells(.Rows.Count, 3).End(xlUp).row
    .Range("Y3:Y" & LR).FillDown
    .Range("R3:R" & LR).FillDown
    .Range("B3:B" & LR).FillDown
    .Range("A3:A" & LR).FillDown
End With
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-18 01:07

An alternative (more common) way of writing it is:

Dim LR As Long
LR = Activesheet.Range("Y" & Rows.Count).End(xlUp).Row 
Range("C3").AutoFill Destination:=Range("Y3:Y" & LR)

This would work in any sheet (also compatibility mode), and you do not need to autofill anymore

查看更多
登录 后发表回答