VBA: Unique items in a column across several worksheets

前端 未结 1 664
面向向阳花
面向向阳花 2021-01-25 00:36

So I have a column, say Expenses, which contains the name of an expense being claimed. I have such a list for each month, each month being a separate sheet. The goal is to get a

1条回答
  •  被撕碎了的回忆
    2021-01-25 01:09

    Excel freezes, because you are using Rows.Count in your code. Thus, it loops up to 1048576 for every worksheet in your workbook. Try to get the last used row and to loop up to it.

    This is a way to check the last used row in column F in worksheet ws:

    lastRow (ws.Name, 6)
    

    Function lastRow(Optional strSheet As String, Optional columnToCheck As Long = 1) As Long
    
        Dim shSheet As Worksheet
    
        If strSheet = vbNullString Then
            Set shSheet = ActiveSheet
        Else
            Set shSheet = Worksheets(strSheet)
        End If
    
        lastRow = shSheet.Cells(shSheet.Rows.Count, columnToCheck).End(xlUp).Row
    
    End Function
    

    0 讨论(0)
提交回复
热议问题