简体   繁体   English

如何查找工作簿表的最大行-VBA

[英]how to find maximum rows of the sheet of workbook - vba

I want to get the data from the workbook which contain the multiple sheets and dont know which sheet having the data. 我想从包含多个工作表的工作簿中获取数据,并且不知道哪个工作表具有数据。 So need to check the used rows count of the every sheet of the workbook and activate the sheet which contain maximum used rows for getting the data. 因此,需要检查工作簿每张工作表的已用行数,并激活包含用于获取数据的最大已用行的工作表。 struct with below code and any suggestion would appreciate. 结构与下面的代码,任何建议将不胜感激。

Sub Maxdatasheet()
Dim wscount As Integer
Dim myArray() As Variant

wscount = ActiveWorkbook.Worksheets.Count

  For i = 1 To wscount
    myArray(i) = Worksheets(i).UsedRange.Rows.Count
    Next
    'need to activate the maximus rows of the sheet
End Sub

A few changes: 一些变化:
- No array, keeping track of what sheet had the maximum instead -没有数组,而是跟踪哪个表具有最大值
- Changed to For Each (even though your code was perfectly fine in that aspect) -更改为For Each(即使您的代码在该方面非常完善)

Sub Maxdatasheet()
    Dim ws As Worksheet
    Dim MaxRowSheet As Worksheet
    Dim MaxRowCount As Long    ' Do not use Integer, may be too small and cause overflow

    wscount = ActiveWorkbook.Worksheets.Count

    MaxRowCount = 0

    For Each ws In ActiveWorkbook.Worksheets
        If ws.UsedRange.Rows.Count > MaxRowCount Then
            MaxRowCount = ws.UsedRange.Rows.Count
            Set MaxRowSheet = ws
        End If
    Next
    MaxRowSheet.Activate
End Sub

To get count of used rows from each sheet- 要获取每个工作表中已用行的数量,请执行以下操作:

Sub Maxdatasheet()
Dim wscount As Integer
Dim myArray() As Variant

wscount = ActiveWorkbook.Worksheets.Count
ReDim myArray(1 To wscount)

  For i = 1 To wscount
    myArray(i) = Worksheets(i).UsedRange.Rows.Count
    Debug.Print myArray(i)
  Next
End Sub

Here is a little code I wrote for myself some time ago. 这是我前一段时间为自己编写的一些代码。 It counts all the row numbers of all Worksheets and displays them seperately in a MsgBox (the total sum ist also being displayed). 它计算所有工作表的所有行号,并分别在MsgBox中显示它们(也显示总和)。

Sub Datensätze_zählen()
'
' Datensätze_zählen Makro
' Zählt alle Datensätze, aller Blätter einer Excel-Datei und gibt diese in einer MsgBox aus
'

Dim all_rows As Long
Dim sheet_row As String
Dim all_array() As String
Dim max_row As Long

If ActiveWorkbook.Sheets.Count > 1 Then
    ReDim all_array(ActiveWorkbook.Sheets.Count)
    For i = 1 To ActiveWorkbook.Sheets.Count
        ActiveWorkbook.Sheets(i).Activate
        sheet_row = ActiveSheet.name & " " & CStr(ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).row) - 1 ' wenn es keine Überschrift gibt, dann ohne "- 1"
        all_rows = all_rows + ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).row - 1 ' wenn es keine Überschrift gibt, dann ohne "- 1"
        all_array(i - 1) = sheet_row
    Next i
    MsgBox (Join(all_array, vbCrLf) & vbCrLf & all_rows)
ElseIf ActiveWorkbook.Sheets.Count = 1 Then
    ActiveWorkbook.Sheets(1).Activate
    MsgBox (ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).row) - 1 ' wenn es keine Überschrift gibt, dann ohne "- 1"
Else
    MsgBox ("In der ausgewählten Excel-Datei ist kein Worksheet vorhanden.")
End If
Erase all_array
End Sub

Hope that that is what you were looking for 希望那是你想要的

Function CountRow(ColumnName As String, ws as Worksheet) As Long
    CountRow = ws.Range(ColumnName& "65536").End(xlUp).row
End Function  

Sub SelectMAX()  
    Dim ws as Worksheet, max as Worksheet
    max = ThisWorkbook.Worksheet(1)
    For Each ws in ThisWorkbook.Worksheets
        if CountRoW("A", ws) > CountRow("A", max) then max = ws
    Next  
    max.Activate
End Sub

Hope this helps. 希望这可以帮助。
PS You can change "A" to any other column if you need. PS您可以根据需要将“ A”更改为任何其他列。

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

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