簡體   English   中英

宏運行時錯誤

[英]Macro run time error

我有一小段代碼,可以為零值過濾一列並返回行數。 我試圖循環遍歷不同的列。此宏在較小的輸入下效果很好。 但是我有一個具有160106行的Excel工作表。 我想在此運行我的宏。 我遇到了運行1004錯誤。我找到了以下鏈接,該鏈接解釋了該問題, 網址為http://support.microsoft.com/kb/210684

但是我無法解決它。 誰能幫我。 我在下面粘貼我的宏

我的樣本文件位於http://rapidshare.com/files/457005707/data1.xlsx,它是一個96mb的文件

Option Explicit
Sub findrcn()
Dim wsStart As Worksheet
Dim sWord As String
Dim RowCount As Integer
Dim i As Long
Dim j As Long
Dim l As Long
Dim k As String
Dim Final As Integer
Dim lastrow As Integer
Dim rng As Range
Dim oBook As Workbook





Set wsStart = ActiveSheet
'this loop is to check if a sheet exists
    For j = 1 To Worksheets.Count
    k = Worksheets(j).Name
    If UCase(k) = UCase("Analysis") Then
        lastrow = ((Sheets("Analysis").Range("A" & Rows.Count).End(xlUp).Row) + 1)
    Else
        lastrow = 0
    End If

    Next j
    MsgBox "finished checking the sheets"
i = 1
For Each rng In Range("A1:B1").Columns
        sWord = Replace(rng.Address(RowAbsolute:=False), "$", "")   ''Now I am trying to loop over all the columns

    If lastrow = 0 Then
            Sheets.Add After:=Sheets(Sheets.Count) 'Adding a new sheet
            Sheets(Sheets.Count).Name = "Analysis"
            wsStart.AutoFilterMode = False

                With wsStart
                    .Range(sWord).AutoFilter Field:=i, Criteria1:="=0" 'if my column contains a 0 in it filter that

                        With .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible)
                        Final = .Count 'get the count of the number of rows after the filter
                        RowCount = Final - 1
                        End With

                       Sheets("Analysis").Range("A") = RowCount 'paste it in the analysis tab
                       Sheets("Analysis").Range("B") = (Range(sWord))

                End With
                wsStart.AutoFilterMode = False
                MsgBox "Trust in the Lord with all your heart and lean not on your own understanding; In all your ways acknowledge Him, and He will make your paths straight." & vbCrLf & "Proverbs 3:5" & vbCrLf & "                        SUCCESSFULLY     COMPLETED!!!"

    Else

        wsStart.AutoFilterMode = False

                lastrow = ((Sheets("Analysis").Range("A" & Rows.Count).End(xlUp).Row) + 1)
                With wsStart
                    .Range(sWord).AutoFilter Field:=i, Criteria1:="=0" 'if my column contains a 0 in it filter that
                        With .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible)
                        Final = .Count
                        RowCount = Final - 1 ' to account for column name
                        End With

                       Sheets("Analysis").Range("A" & lastrow) = RowCount 'paste it in the analysis tab
                       Sheets("Analysis").Range("B" & lastrow) = (Range(sWord))

                End With
                wsStart.AutoFilterMode = False


    End If
i = i + 1
Next rng

盡管該知識庫文章討論的是復制工作表,而您正在復制單元格,但是您可以遵循其建議並定期保存和關閉工作表。

乍一看,似乎有一個簡單得多的解決方案:使用CountIf函數

例如,假設您的數據在Sheet2上,則在分析表上的單元格中=COUNTIF(Sheet2!A:A,0)
這給出了列A中單元格的計數= 0

如果出於其他原因在VBA中需要此功能,則可以使用類似

Set r = ActiveSheet.Columns("A:A")
z = Application.WorksheetFunction.CountIf(r, "0")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM