Prabhu Srinivasan
Prabhu Srinivasan

Reputation: 1

Excel VBA lookup each through sheet last row

I have several sheets in the workbook. In sheet one I have customer name and sales amount. Like below.

Customer Name | Sales Amount

Prabhu
Srinivasan

Each has the sheet name as customer name. Like sheet 2 name is Prabhu and sheet 3 name is Srinivasan.

Other than sheet 1, all the sheet has sales amount on last row of B column.

Now I need to fetch, sales amount on each customer on sheet 1 against customer name.

Sub EachthroughLastSheet()

     Dim i As Integer

     For i = 2 To ThisWorkbook.Worksheets.Count

          MsgBox ThisWorkbook.Worksheets(i).Name

     Next

End Sub

Upvotes: 0

Views: 51

Answers (1)

Teamothy
Teamothy

Reputation: 2016

If I understand You correctly that should help:

Sub EachthroughLastSheet()

Dim j As Long
Dim nameCust As String
Dim valueSales As String
Dim lastRow As Long, lastRow2 As Long

    With Sheets("Sheet1")
        lastRow = .Cells(Rows.Count, 1).End(xlUp).Row

        For j = 2 To lastRow
            nameCust = .Cells(j, 1)

            With Sheets(nameCust)
                lastRow2 = .Cells(Rows.Count, 2).End(xlUp).Row
                valueSales = .Range("B" & lastRow2).Value
            End With
            .Cells(j, 2) = valueSales
        Next

    End With

End Sub

Upvotes: 1

Related Questions