[英]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.