简体   繁体   中英

How to Filldown activecell in vba

Could anyone help me? Let's say I4:I is my Lastrow and Activecell in G will fill down the formula.

Dim Lastrowzxc As Long
Lastrowzxc = Range("I4").CurrentRegion.Rows.Count

Range("G" & Rows.Count).End(xlUp).Offset(1).Select
  ActiveCell.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"
Selection.AutoFill Destination:=Range(ActiveCell.Address & Lastrowzxc)

You have incorrect address in 'Range' of autofill. This might work, but this is far from perfect (just a starting point to get you going):

Dim Lastrowzxc As Long
Lastrowzxc = Range("I4").CurrentRegion.Rows.Count

Range("G" & Rows.Count).End(xlUp).Offset(1).Select
  ActiveCell.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"
Selection.AutoFill Destination:=Range(Cells(2, 7), Cells(Lastrowzxc, ActiveCell.Column))

假设I4:I20是要填充的范围,并且G4包含公式,这是您应该执行的操作:

Range("I4:I20").FormulaR1C1 = Range("G4").FormulaR1C1

Try this it works for me:

Dim Lastrowzxc As Long
Lastrowzxc = ActiveSheet.Range("I4").currentRegion.Rows.Count

Range("G" & ActiveSheet.Rows.count).End(xlUp).Offset(1).Select

Selection.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"

Selection.AutoFill Destination:=Range(Range(ActiveCell.Address), Cells(Lastrowzxc, ActiveCell.Column))

Why bother selecting ranges? It's bad practice.

I don't quite understand what your goal is as you are selecting the cell below the last cell in col G. I suppose you want to start at the last row of col G until the last row for col I? If so..:

With Workbooks("REFERENCE").Sheets("REFERENCE")
LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
LastrowG = .Cells(.Rows.Count, "G").End(xlUp).Row

.Range("G" & LastrowG).FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")" 'change Range if necessary
.Range("G" & LastrowG).FormulaR1C1.AutoFill Destination:= .Range("G" & LastrowG & ":G" & LastrowI)

End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM