简体   繁体   中英

VBA setting values to a column to the last row of another column

I am trying to make a VBA that gives certain columns a value up until the last row of the first column. This is my code:

Sub Macro8()
'
' Macro8 Macro
'

        Dim sh As Worksheet, lastRow As Long
        Set sh = ActiveWorkbook.ActiveSheet

        lastRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
        sh.Range("D" & lastRow).Value = 8.5
        sh.Range("E" & lastRow).Value = 6

End Sub

I only seem to get the value on the last row of data, but I want it from the 2nd row to the last row of the first column

all good, worked it out. Pretty basic

Sub Macro8()
'
' Macro8 Macro
'

        Dim sh As Worksheet, lastRow As Long
        Set sh = ActiveWorkbook.ActiveSheet

        lastRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
        sh.Range("D2:D" & lastRow).Value = 8.5
        sh.Range("E2:E" & lastRow).Value = 6

End Sub

didnt set the range up properly

lastRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row + 1

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