简体   繁体   中英

Excel VBA select new usedrange portion of a column

数据样本

Hi All.

I need your help please.

I want to apply a formula to the new rows that I've just added. I have a line of code I use for a full set of data, but I'm not sure how to modify it, or if there is a better way to do it.

I'm a bit out of my league with this line of code, I understand what it's doing but I didn't write it.

Sheets("Closed Loop").Range(varPastePos).Offset(0, 2).Resize(ActiveSheet.UsedRange.Rows.Count - 1, columnsize:=1).FormulaR1C1 = "My Formula"

varPastePos is the position the new data was pasted, in this case $F$28

Any ideas?

.UsedRange would also include formatted blank cells so it is not reliable for finding the last used row.

Set ws = Sheets("Closed Loop")
Set cell1 = ws.Range(varPastePos) ' F28
Set cell2 = cell1.End(xlDown)     ' F32 ? the last cell with data below F28. Similar to clicking on F28 and pressing Ctrl + down
Set rngF = ws.Range(cell1, cell2) ' F28:F32
Set rngH = rngF.Offset(, 2)        ' H28:H32
rngH.FormulaR1C1 = "My Formula"

Use Range().FillDown to extend a pre-existing formula.

Dim lastRow As Long
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("A2", "A" & lastRow).FillDown

You can FillDown multiple columns at once like this:

 Range("A2", "D" & lastRow).FillDown

You can do it all in one line like this:

Range("A2", Range("F" & Rows.Count).End(xlUp)).FillDown

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