简体   繁体   English

Excel VBA查找每个工作表的最后一行

[英]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. 像工作表2的名称是Prabhu,工作表3的名称是Srinivasan。

Other than sheet 1, all the sheet has sales amount on last row of B column. 除工作表1外,所有工作表的销售额都在B列的最后一行。

Now I need to fetch, sales amount on each customer on sheet 1 against customer name. 现在,我需要根据客户名称获取工作表1上每个客户的销售额。

Sub EachthroughLastSheet()

     Dim i As Integer

     For i = 2 To ThisWorkbook.Worksheets.Count

          MsgBox ThisWorkbook.Worksheets(i).Name

     Next

End Sub

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM