Macro to count the number of cells in column that contain a Name and worn me if its more than 1 or zero

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

Ok so i'm looking for a macro to do a check for me,
I have a sheet called FP Raw and it has a list of names,
I shouldn't have any duplicates but sometimes this happens and i want a simple macro to check this for me.

So when the macro is run i need it to do this:

Get a name from sheet "Welcome" range "ac57"

count how many times that name is found in sheet "FP raw" column Q

If its 0 times then

messagebox " That name does not exist"
exit sub
if its greater then 1 then

messagebox " The name appears ore then once please check data before proceeding"
exit sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
try this:

VBA Code:
Sub test()
    Dim lastrow As Long, i As Integer
    With Sheets("FP raw")
        lastrow = .Range("Q" & .Rows.Count).End(xlUp).Row
        i = Application.WorksheetFunction.CountIf(.Range("Q1:Q" & lastrow), Sheets("Welcome").Range("AC57").Value)
        If i = 0 Then
            MsgBox "That name does not exist"
        ElseIf i > 1 Then
            MsgBox "The name appears more then once please check data before proceeding"
        End If
    End With
End Sub
 
Upvote 0
You don't actually need it to be a macro, for instance you could have this setup: (assuming AD57 is available) which is just the formula as used in MrTomasz answer but put into a cell
Cell Formulas
RangeFormula
AD57AD57=IF(COUNTIF('FP Raw'!Q:Q,Welcome!AC57)=0,"That name does not exist",IF(COUNTIF('FP Raw'!Q:Q,Welcome!AC57)>1,"The name appears more than once please check data before proceeding","OK"))
 
Upvote 0
Thank you MrTomasz, this does exactly what i needed,
Thank you John, but i need it to pop up as a message box otherwise it will get missed. but thank you for the help.
Thanks Tony
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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