Changing msgbox

rumble123

New Member
Joined
Aug 2, 2017
Messages
4
Hello everyone,
I have a data in excel and I want to create a macro which will count for me the number of cells in the column which contains a specified value and I want to paste the count of that cells next to that data in excel. Right now I'm using this code:
Code:
[COLOR=blue]Dim[/COLOR] lCount [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] 
     
    lCount = WorksheetFunction.CountIf(Range("B1").EntireColumn, "Men") 
    MsgBox lCount 
     
    lCount = WorksheetFunction.CountIf(Range("B1").EntireColumn, "Women") 
    MsgBox lCount

The thing is I don't know how to change the msgbox to the pasting the count into that sheet with data (next to table for example). To be honest I'm open to other solutions - the main objective is to count each of the cell in the specified column and paste that data into the worksheet instead of appearing msgbox. And I need to make it 'universal' (the number of rows of the column B may change with different file). Something like the table below.

[TABLE="width: 500"]
<tbody>[TR]
[TD]First Column: Name[/TD]
[TD]Second Column: Sex[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Abc[/TD]
[TD]Men[/TD]
[TD]Number of 'men'[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Aac[/TD]
[TD]Women[/TD]
[TD]Number of 'women'[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Bac[/TD]
[TD]Men[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Bcc[/TD]
[TD]Women[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Thank you.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
To echo the data to the screen you'd just change your MsgBox line to Range("D1").Value= lCount if you wanted the data in D1, not too sure what you mean by universal, the countif part is counting all of the column B so as long as the data was in column B it'd be counted
 
Upvote 0
Thank you MrTeeny for help! Working perfect. I'm aware that it wasn't the hardest question but I'm new to VBA and i couldn't resolve that issue.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top