Click here to Skip to main content
12,822,050 members (42,588 online)
Rate this:
Please Sign up or sign in to vote.
See more: VBA

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.

Posted 29-Aug-12 9:13am
Updated 29-Aug-12 9:58am
Kenneth Haugland 29-Aug-12 14:58pm
This is rather simple to achive, but what have you tried?
losmac 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 :
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
    srcwsh.Range("B" & i) = counter
    counter = 0 'new application, so reset counter    

This is not optimal code, because it compares data row by row (cell by cell), but it shows "logic".
Kenneth Haugland 29-Aug-12 15:46pm
Sheet.UsedRange is much faster than looping :)
losmac 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100