简体   繁体   中英

Copy formula until blank cell

I would like to write a code in VBA which copies formula from range("A3:H3") to these columns until there is data in column J. I've tried this code:

Sub fill_up()
If Not IsNull(Range("J3:J30000")) Then
Range("A3:H3").Select
Selection.Copy
Range("A4:H30000").PasteSpecial xlPasteFormulas
End If
End Sub

But this way I get filled up the columns until the 30000. row anyways. Another code I wrote for a command button:

Private Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
i = 3
j = 4
If Not IsEmpty(Cells(i, 9)) Then
Range("A3:H3").Select
Selection.Copy
Cells(j, 1).PasteSpecial xlPasteFormulas
Do until j > 30000
i = i + 1
j = j + 1
Loop
End If
End Sub

But here the formula is only pasted to the 4. row.

Any suggestions?

Something like this:

sub test1()

iRow = 5
Do Until not IsEmpty(cells(iRow,10))
    Range(cells(3,1), cells(3,8)).Copy

    Range(cells(iRow,1), cells(iRow,8)).PasteSpecial Paste:=xlPasteFormulas

    iRow = iRow + 1
Loop

End Sub

That won't copy anything into the row that contains data in col J. If you want to include that row for pasting, then you could add a further single paste op after the do loop as a quick 'n' dirty fix.

I recommend referencing the last cell containing data using ActiveSheet.Usedrange.Rows.Count property.

In your case:

Range("A4:H" & ActiveSheet.UsedRange.Rows.Count)

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