简体   繁体   中英

Auto-fill Range to Last Row Used

I have 0 in cell G1 and need to autofill with 0s till the last used row.

In the below code the 0s are updating till row 30 while my rows are only filled till row 4.

I want the 0s to fill only if there's data in the adjacent row.

NumberConv is Sheet2 in my workbook.

Sub MacroNC

    Sheets("NumberConv").Select
    Range("G1").Select
    Selection.NumberFormat = "@"
    ActiveCell.FormulaR1C1 = "0"
    
    Sheet2.Select
    Range("G1").Select
    lastRow = ActiveSheet.UsedRange.Rows.Count
    Selection.AutoFill Destination:=Range("G1:G" & lastRow), Type:=xlFillCopy

End Sub
Sub MacroNC
    With Sheet2
        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

        .Range("G1:G" & lastRow).NumberFormat = "@"
        .Range("G1:G" & lastRow).Value = 0
    End With
End Sub
  • Side note: Unclear why you need to store the zeros as text.

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