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
UsedRange
is unreliable ; use End(xlUp)
to find the last row.AutoFill
, you can apply a NumberFormat
and write a Value
to an entire range.Select
. 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
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.