Solved

Same number of instances in two columns?

Posted on 2013-05-29
7
192 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
Comment Utility
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
Comment Utility
EXCELent!!  Thanks -R-
0
 

Author Comment

by:RWayneH
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:RWayneH
Comment Utility
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
Comment Utility
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
Comment Utility
Now it is good.  Thanks for the additional edit.  -R-
0
 
LVL 29

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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 create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

772 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

10 Experts available now in Live!

Get 1:1 Help Now