2011-04-12 94 views
0

我有一個代碼過濾一個零值的列並返回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 

回答

0

雖然這知識庫文章在談論複印頁數和你複製單元格,你可以按照其建議並定期保存並關閉工作表。

0

乍一看似乎有一個更簡單的解決方案:使用CountIf功能

例如,假設你的數據在Sheet2中,你的分析表放在一個細胞=COUNTIF(Sheet2!A:A,0)
這給細胞的數量= 0在A列

如果您需要這VBA出於其他原因,你可以使用類似的財產以後

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