简体   繁体   English

使用excel VBA在每一列中找到最后使用的行数

[英]to find the last used row count in every column using excel VBA

I'm trying to get the last used row count in every column of a particular sheet using VBA.I have written the code,but this prints only the row count of first column.Can you please help me to find the last used row in every column.我正在尝试使用 VBA 在特定工作表的每一列中获取最后使用的行数。我已经编写了代码,但这仅打印第一列的行数。你能帮我找到最后使用的行吗?每列。 Please find the code请找到代码

Dim row As Long
Dim i As Integer
Dim j As Integer
Dim a As String
Dim b As Range
Dim myrange As Range
Dim count As Integer
Dim url As String
Dim lastRow, lRow As Long
Dim iCntr, jCntr, iMaxRow As Integer
Dim vMax
Dim arr2(18)


For iCntr = 1 To 18 ' for each column
     vMax = 0
     iMaxRow = 2
     
    'Finding last Row of current Column
    With ActiveSheet
        lastRow = .Cells(.Rows.count, iCntr).End(xlUp).row
    End With
    arr2(iCntr - 1) = lastRow
Next

Worksheets("Sheet1").Range("B2:B18").Value = arr2

Try:尝试:

Sub test()

    Dim arrLastRows(1 To 18) As Variant
    Dim i As Long, LastRow As Long
          
    With ThisWorkbook.Worksheets("Sheet1")
        'Loop columns
        For i = 1 To 18
            'Get the last row of the current column
            LastRow = .Cells(.Rows.Count, i).End(xlUp).Row
            'Import the value to position i-7 (array start from 0)
            arrLastRows(i) = LastRow
        Next i
        
    .Range("T1:T18").Value = Application.Transpose(arrLastRows)
        
    End With
    
End Sub

Output:输出:

在此处输入图片说明

I have written the code,but this prints only the row count of first column for every column.我已经编写了代码,但这仅打印每一列的第一列的行数。

There is no problem with your main code.你的主代码没有问题。 The problem is with the way you are writing the output to "Sheet 1"问题在于您将输出写入“工作表 1”的方式

Change改变

Worksheets("Sheet1").Range("B2:B18").Value = arr2

to

'~~> Note: It has to be 19 and not 18. If there are 18 columns and so on
Worksheets("Sheet1").Range("B2:B19").Value = Application.Transpose(arr2)

Tested on经过测试

在此处输入图片说明

Output输出

在此处输入图片说明

Collect Last Rows收集最后一行

  • The first code is a quick fix of your issues.第一个代码可以快速解决您的问题。 It is still not recommended.仍然不推荐。
  • The second code shows the advantages of using constants, of fully qualify ing worksheets and ranges and of commenting your code (Be aware that this code is over-commented, you usually only comment sections of it.).第二个代码显示了使用常量、完全限定工作表和范围以及注释代码的优点(请注意,此代码被过度注释,您通常只注释它的部分。)。

Tips提示

  • This post explains why we mostly do not use Integer any more. 这篇文章解释了为什么我们大多不再使用Integer
  • Try avoiding Active in any flavor ie ActiveWorkbook , ActiveSheet , ActiveCell .尽量避免使用任何形式的Active ,即ActiveWorkbookActiveSheetActiveCell
  • Use variables.使用变量。 Use meaningful names (poorly demonstrated).使用有意义的名称(演示不佳)。 Additionally maybe make up names for them.另外也许为他们编造名字。
  • Be careful when declaring variables in one line: Dim x As..., y As..., z As... .在一行中声明变量时要小心: Dim x As..., y As..., z As...
  • In the second code, if declaring variables as they are needed is too confusing at this stage, copy all those Dim s to the beginning of the code right after the constants to make it more readable for you.在第二个代码中,如果在此阶段根据需要声明变量过于混乱,请将所有这些Dim复制到代码开头的常量之后,以使其对您更具可读性。

The Code编码

Option Explicit

Sub LastRowsQF()
    Dim row As Long
    Dim i As Integer
    Dim j As Integer
    Dim a As String
    Dim b As Range
    Dim myrange As Range
    Dim count As Integer
    Dim url As String
    Dim lastRow As Long, lRow As Long
    ' Not:
    'Dim lastRow, lRow As Long ' Wrong!
    Dim iCntr As Integer, jCntr As Integer, iMaxRow As Integer
    ' Not:
    'Dim iCntr, jCntr, iMaxRow As Integer ' Wrong!
    Dim vMax
    ' This means 18 rows by 1 column.
    Dim arr2(1 To 18, 1 To 1)
    ' Not:
    ' This means 1 row with 19 columns (elements)!
    'Dim arr2(18)
    
    For iCntr = 1 To 18 ' for each column
         'vMax = 0 ' Not used.
         'iMaxRow = 2 '  Not used.
         
        'Finding last Row of current Column
        With ActiveSheet
            lastRow = .Cells(.Rows.count, iCntr).End(xlUp).row
        End With
        arr2(iCntr, 1) = lastRow
    Next
    
    Worksheets("Sheet1").Range("B2:B19").Value = arr2

End Sub

Sub LastRows()
    
    ' Constants
    Const srcNumberOfColumns As Long = 18 ' Source Number of Columns
    Const tgtName As String = "Sheet1"    ' Target Worksheet Name
    Const tgtFirstCell As String = "B2"   ' Target First Cell Range Address
    
    ' Define workbook ('wb').
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containig this code.
    
    ' Define Source Worksheet ('ws').
    Dim ws As Worksheet
    Set ws = wb.ActiveSheet ' e.g. wb.worksheets("Sheet2") is preferred.
    
    ' Define (2D one-based one-column) Data Array ('Data').
    Dim Data As Variant
    ReDim Data(1 To srcNumberOfColumns, 1 To 1)
    
    ' Write last row of each column to rows of Data Array.
    Dim j As Long
    For j = 1 To srcNumberOfColumns
        Data(j, 1) = ws.Cells(ws.Rows.count, j).End(xlUp).row
    Next
    
    ' Define Target Worksheet ('ws').
    ' Note: Use a different variable if you still need to use Source Worksheet.
    Set ws = wb.Worksheets(tgtName)
    
    ' Define Target Range ('rng').
    Dim rng As Range
    Set rng = ws.Range(tgtFirstCell).Resize(UBound(Data, 1))
    
    ' Write values from Data Array to Target Range.
    rng.Value = Data

End Sub

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

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