Solved

Same number of instances in two columns?

Posted on 2013-05-29
7
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 30

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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

752 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