Forum Discussion

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

Get value from lastrow and lastcol VBA

How do i amend the code below to get value from a lastrow and lastcol any data.

When the code executed, it should be populating value of $1,350

 

Dim lastRow_TCA_corp as long
Dim lastCol_TCA as long
Dim Destws_TCA_corp as Worksheet
Dim Destwb_TCA_corp as Workbook
Set Destwb_TCA_corp = Workbooks.Open(filename_TCA_corp)
Set Destws_TCA_corp = Destwb_CMS_corp.Sheets("Collection for Invoicing")

lastRow_TCA_corp = Destws_TCA_corp.Cells(Destws_TCA_corp.Rows.Count, "A").End(xlUp).Row

lastCol_TCA = Destws_TCA_corp.Cells(1, Columns.Count).End(xlToLeft).Column

ws.Range("B28").Value = Destws_TCA_corp.Range("A2:A" & lastRow_TCA_corp & lastCol_TCA).Value

 

Appreciate the assistance in advance!

 

  • hrh_dash Last line of your code is problematic. Try below codes.

    Sub LrLc()
        Dim lastRow_TCA_corp As Long
        Dim lastCol_TCA As Long
        Dim Destws_TCA_corp As Worksheet
        Dim Destwb_TCA_corp As Workbook
        Set Destwb_TCA_corp = Workbooks.Open(filename_TCA_corp)
        Set Destws_TCA_corp = Destwb_CMS_corp.Sheets("Collection for Invoicing")
        
        lastRow_TCA_corp = Destws_TCA_corp.Cells(Destws_TCA_corp.Rows.Count, "A").End(xlUp).Row
        
        lastCol_TCA = Destws_TCA_corp.Cells(1, Columns.Count).End(xlToLeft).Column
        
        ws.Range("B28").Value = Destws_TCA_corp.Cells(lastRow_TCA_corp, lastCol_TCA).Value
    End Sub

2 Replies

  • Harun24HR's avatar
    Harun24HR
    Bronze Contributor

    hrh_dash Last line of your code is problematic. Try below codes.

    Sub LrLc()
        Dim lastRow_TCA_corp As Long
        Dim lastCol_TCA As Long
        Dim Destws_TCA_corp As Worksheet
        Dim Destwb_TCA_corp As Workbook
        Set Destwb_TCA_corp = Workbooks.Open(filename_TCA_corp)
        Set Destws_TCA_corp = Destwb_CMS_corp.Sheets("Collection for Invoicing")
        
        lastRow_TCA_corp = Destws_TCA_corp.Cells(Destws_TCA_corp.Rows.Count, "A").End(xlUp).Row
        
        lastCol_TCA = Destws_TCA_corp.Cells(1, Columns.Count).End(xlToLeft).Column
        
        ws.Range("B28").Value = Destws_TCA_corp.Cells(lastRow_TCA_corp, lastCol_TCA).Value
    End Sub
    • hrh_dash's avatar
      hrh_dash
      Iron Contributor
      the code works perfectly. thank you for the assistance!

Resources