PDA

View Full Version : Solved: Count non-identical codenumbers in big file



hunsnowboard
02-08-2009, 05:35 AM
Hi Everyone! I try to make a macro which should be relatively easy but I am stucked with it for hours, and it is really getting on my nerves.

The real file which I am working on is really big, with around 65000 rows, and a few worksheets. The number of worksheets will growing in the future, so I need the macro to check all the worksheets (that is why I use the: for each worksheet in workbook command).
The point of the whole macro is to count the total codenumbers (which are in column "A"). The problem is that the codenumbers are repeated, so what I tried to do is to copy the whole column "A" to a separate worksheet (that is "List2") and then delete the repeating/duplicated codenumbers. Then again, copy the column "A" from the next workbook and paste it on "List2" column "B" and delete the repeating/duplicated codenumbers and then paste the remaining ones to column "A" on List2 worksheet. Then if the macro finished with all the workbooks, count the rows of column "A" on "List2" and then I will find out how many different codenames are in the workbook.
You can see my macro on the file attached. There are more problems with my macro. First of all, I have to run the macro from Sheet1 in order to start to copy column "A", if I run it from "List2" worksheet then nothing happens.
Second (if I start the macro from Sheet1) it copies only the "Sheet1" codenumbers, but it copies them three times... like ws next command would not work. Please try to help me! Thank you in advance!

Bob Phillips
02-08-2009, 07:11 AM
Sub Teljcsal()
Const SUM_FORMULA As String = _
"SUMPRODUCT((A2:A<row><>"""")/COUNTIF(A2:A<row>,A2:A<row>&""""))"
Dim ws As Worksheet
Dim LastRow As Long
Dim num As Long

For Each ws In ActiveWorkbook.Worksheets

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If LastRow > 1 Then _
num = num + Application.Evaluate(Replace(SUM_FORMULA, "<row>", LastRow))
Next ws

MsgBox num
End Sub

hunsnowboard
02-08-2009, 08:11 AM
Hi Xld! Thank you for your quick reply. I tried your macro, but it seems that it is not working. The messagebox output message is: 0, which is definetelly not good. :(

Bob Phillips
02-08-2009, 08:46 AM
It output 1800+ for me.

hunsnowboard
02-08-2009, 09:18 AM
What could be wrong then? I run the macro in the same file I attached. :(

Bob Phillips
02-08-2009, 09:31 AM
Post the updated file.

hunsnowboard
02-08-2009, 09:55 AM
I checked a few things and I found out the followings. If I run the macro from activesheet as Sheet1 then the result I get is 1815. If I run it from the activesheet as Sheet2 then the result is 5979, and if I run the macro from activesheet as Sheet3 then the result is 14856. If I run the macro from the Sheet List2 then the result is 0. The macro will be used from List2 sheet with a button. Why is the outcome always different? Thank you!

Bob Phillips
02-08-2009, 10:04 AM
Crap code, that is why!



Sub Teljcsal()
Const SUM_FORMULA As String = _
"SUMPRODUCT(('<sheet>'!A2:A<row><>"""")/COUNTIF('<sheet>'!A2:A<row>,'<sheet>'!A2:A<row>&""""))"
Dim ws As Worksheet
Dim LastRow As Long
Dim num As Long

For Each ws In ActiveWorkbook.Worksheets

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If LastRow > 1 Then _
num = num + Application.Evaluate(Replace(Replace(SUM_FORMULA, _
"<row>", LastRow), _
"<sheet>", ws.Name))
Next ws

MsgBox num
End Sub

hunsnowboard
02-08-2009, 10:59 AM
Yes, yes! :) It is working! Thank you very much XLD!