宏运行时错误

时间:2011-04-12 05:21:17

标签: excel-vba vba excel

我有一段代码可以过滤列的零值并返回rowcount。我尝试在不同的列上循环。这个宏适用于小输入。 但我有一张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

2 个答案:

答案 0 :(得分:0)

尽管该知识库文章正在讨论复制工作表并且您正在复制单元格,但您可以按照其建议并定期保存并关闭工作表。

答案 1 :(得分:0)

乍一看似乎有一个更简单的解决方案:使用CountIf函数

例如假设您的数据在Sheet2上,在Analysis表上放入单元格=COUNTIF(Sheet2!A:A,0)
这给出了A列中cell = 0的计数

如果出于其他原因需要在VBA中使用此功能,则可以使用

之类的内容
Set r = ActiveSheet.Columns("A:A")
z = Application.WorksheetFunction.CountIf(r, "0")