Forum Discussion

hrh_dash's avatar
hrh_dash
Iron Contributor
Sep 07, 2022
Solved

Macro to input values for lastcol with regards to a specific row

Would like to seek advise for a macro to input values in the next column of lastcol; ie lastcol.offset(0, 1).

 

For example if there is a value in A1, when the macro executed, the value will be populated in A2.

 

Currently, the code below would populate data in cell B32. The column is correct but the row no is incorrect

Dim lastCol As Long
Dim ws as Worksheet

Set ws = Sheet4
lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1

ws.Range("A3" & lastCol).Offset(0, 1).value = "Yes"

 

Thanks for the help in advance!

 

  • hrh_dash If you want to add data to right after last column of a specific row then try-

    Sub AddToLastCol()
    Dim lastCol As Long
    Dim ws As Worksheet
    
        Set ws = Sheet4
        lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1
        ws.Cells(1, lastCol) = "Yes"
    
    End Sub

4 Replies

  • Harun24HR's avatar
    Harun24HR
    Silver Contributor

    hrh_dash If you want to add data to right after last column of a specific row then try-

    Sub AddToLastCol()
    Dim lastCol As Long
    Dim ws As Worksheet
    
        Set ws = Sheet4
        lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1
        ws.Cells(1, lastCol) = "Yes"
    
    End Sub
      • Harun24HR's avatar
        Harun24HR
        Silver Contributor
        If it helps then please mark the answer as solved (tick mark it).