Solved

Same number of instances in two columns?

Posted on 2013-05-29
7
223 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 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
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!

 

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

Technology Partners: 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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

756 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