计算excel中的记录数,直到显示Total

时间:2018-02-13 13:33:50

标签: vba excel-vba excel

每天提取一个excel文件,记录数量也各不相同。假设显示10条记录,则总计显示在A1列的最后一行。之后,下面还有一张桌子。我需要计算从A2到总数之前的行数。写了下面的代码:

myRange = Range("B65536")

If Application.WorksheetFunction.CountA(myRange) <> 0 Then
    lastCol = Cells.Find("Total", Range("B1"), xlPart, , xlByRows, xlDown, False).Row
    MsgBox lastCol
Else
    lastCol = 1
End If

2 个答案:

答案 0 :(得分:0)

如果您正在寻找&#34; Total&#34;在A列上,以下代码将显示一个带行号的msgbox:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Dim TotalRow As Long
TotalRow = ws.Range("A:A").Find("Total").Row
'look for the word "Total" on Column A
MsgBox "Your desired Range is from A2 to A" & TotalRow
End Sub

答案 1 :(得分:0)

像这个小的东西应该在第一列中找到“Total”的行:

Public Sub TestMe()
    Dim rowWithTotal As Long
    rowWithTotal = Worksheets(1).Columns(1).Find("Total").Row
    MsgBox "Row with total is - " & rowWithTotal
End Sub