Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I want to read a particular application from a excel (current) and count it the number of times it has appeared in the excel.
Then in my master excel i have defined these applications, i want to create a column with name as "Aug 20" and corresponding to the application in excel it should add the count in master excel.
If there are not applications found in current excel then in master excel i want to add the count value to corresponding application in master excel as 0.

Please let me know how this can be achieved. I am not getting the logic to do this.

Thanks!
Posted
Updated 29-Aug-12 8:58am
v2
Comments
Kenneth Haugland 29-Aug-12 14:58pm    
This is rather simple to achive, but what have you tried?
Maciej Los 29-Aug-12 15:12pm    
Do you want to compare the data stored in 2 Excel sheets?
Do you really need vba? Can't you use Lookup or CountIf function?

1 solution

As i wrote in comment, you need only CountIf worksheet function. But if you would like to do it with VBA code, OK, i'll show you an example. Below code is to use when both excel files (master and current) are opened.

Copy the code below and paste it into master file in new module :
VB
Dim srcwsh as Worsheet, dstwsh As Worksheet
Dim i as Integer, j As Integer, counter as Integer
Dim sAppName As String

Set dstWsh = ThisWorkbook.Worksheets(1)
Set srcWsh = Application.Workbooks("current.xls").Worksheets(1)
counter = 0
i = 2 'start from row no. 2 in srcwsh
j = 2 'start from row no. 2 in dstwsh
Do While srcwsh.Range("A" & i)<>""
    sAppName = srcwsh.Range("A" & i)
    Do While dstwsh.Range("A" & j)<>""
        If dstwsh.Range("A" & j) = sAppname Then counter = counter + 1
    Loop
    srcwsh.Range("B" & i) = counter
    counter = 0 'new application, so reset counter    
Loop


This is not optimal code, because it compares data row by row (cell by cell), but it shows "logic".
 
Share this answer
 
v2
Comments
Kenneth Haugland 29-Aug-12 15:46pm    
Sheet.UsedRange is much faster than looping :)
Maciej Los 29-Aug-12 15:47pm    
... to count applications in both files?
How do you want to use it? If you know an answer, post it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900