Solved

Same number of instances in two columns?

Posted on 2013-05-29
7
211 Views
Last Modified: 2013-05-30
Looking for a macro that I can run that will tell me the number of instances that the number one is in column C and the number of instances that the number one is in column AG.  These should match.  If 6 in column C there should be 6 in column AG.

Ideally a dialog box that shows the results of each would be nice.  Thanks -R-
0
Comment
Question by:RWayneH
  • 4
  • 3
7 Comments
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 39205376
Here it is try this code

Sub CountCAG()
Dim WS As Worksheet
Dim MaxRow As Long, I As Long
Dim CountC As Long, CountAG As Long

Set WS = ActiveSheet
MaxRow = WS.Range("C" & WS.Rows.Count).End(xlUp).Row

For I = 1 To MaxRow
    If WS.Cells(I, "C").Value = 1 Then
        CountC = CountC + 1
    End If
    
    If WS.Cells(I, "AG").Value = 1 Then
        CountAG = CountAG + 1
    End If
Next I

MsgBox ("Results of Counting Cells containing 1:" & Chr(10) & "Col C: " & Chr(9) & CountC & Chr(10) & "Col AG: " & Chr(9) & CountAG)

End Sub

Open in new window


Rgds/gowflow
0
 

Author Closing Comment

by:RWayneH
ID: 39205432
EXCELent!!  Thanks -R-
0
 

Author Comment

by:RWayneH
ID: 39205534
Hold the bus...  Do I need to reformat the columns so it recongizes all the ones?  It is failing on the attached sheet.  Please advise and thanks. -R-
CountCAG-Error.xlsx
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:RWayneH
ID: 39205544
I noticed that AG is not the same length as C.  They can have different row amount in each column.  Is that a possible issue? -R-
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39206801
Well I incorporated the macro to your workbook and renamed it as .xlsm and changed the maximum rows as seem it was the problem. Check this version and note that in your data Col AG has more rows than Col C maybe that is your problem. Go down in your data and you will see that Col C stops at row 66 and then Col AG keeps on till row 172 maybe this is your problem ??? Anyhow the previous version would compare only the first 66 rows now it goes far beyond that to ensure no rows are missed.

Let me know. Delay in reply due to diffrent time zones.
gowflow
CountCAG-Error.xlsm
0
 

Author Comment

by:RWayneH
ID: 39207484
Now it is good.  Thanks for the additional edit.  -R-
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39207516
your welcome pls feel free to ask for me case u need it more adapted to what you want
gowflow
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now