See if value exists multiple times in column

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi,

I have a dropdown list in cell B11 in sheet1 where I can select values from column A in sheet2.
Everything works okay, but I would like to have a quality routine that alerts me if the selected value in B11 exists more than once in column A in sheet2. That is, because this value is used several places in this sheet with lookup-function, and it only returns the first values content.

So, this is my code:

Code:
Public Sub Worksheet_Change2(ByVal Target As Range, Cancel As Boolean)Application.ScreenUpdating = False


    Dim KeyCells As Range


    Set KeyCells = Range("B11:D11")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then


        MsgBox "Det eksisterer minst to punkt med navnet" & Target.Address & " . Vennligst kontroller punktlisten!"
       
    End If
    
End Sub

Does anyone got a suggestion on code to use to search through column A in sheet 2 to see if the selected value exists more than once?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this code in sheet1.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, R As Range
If Target.Address(0, 0) = "B11" Then
With Sheets("Sheet2")
    Set Rng = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
End With
MsgBox "Count of value " & Target.Value & " in sht2 = " & Application.CountIf(Rng, Target)
End If
End Sub
 
Upvote 0
Hi and thank you for your reply,

Unfortunately, nothing happens when I run this one. I have configured it to my needs, but it doesnt work.
And: I would only like to get a notification if the value in cell B11 exists more than once in the column A in the sheet "Kumkort_Import". Not a message every time the value changes (as it seems like the code is doing as for now?).

Here is my modified code pasted in the sheet where I change the value in cell B11 (called "Kumkort").

Code:
Private Sub Worksheet_Change3(ByVal Target As Range)

Dim Rng As Range, R As Range


If Target.Address(0, 0) = "B11" Then
    With Sheets("Kumkort_Import")
        Set Rng = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
    End With


    MsgBox "Count of value " & Target.Value & " in sht2 = " & Application.CountIf(Rng, Target)


End If


End Sub
 
Upvote 0
It looks like you are trying to use Event Procedure VBA code (i.e. Worksheet_Change), which is triggered automatically.
Note that in order for this to work, you CANNOT change the name of it! It has to start out like:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
The minute you add a number to the end of the Procedure name (like 2 or 3), it is no longer an automatically called Event Procedure code, and will not run automatically.
 
Upvote 0
Oh, I see.
I guess that it is not possible to have multiple events with the same name (Worksheet_Change)? Then I need to add this code into my other Worksheet_Change code?
 
Upvote 0
I guess that it is not possible to have multiple events with the same name (Worksheet_Change)? Then I need to add this code into my other Worksheet_Change code?
That is correct. You can only have one Worksheet_Change Event Procedure code per worksheet. You would just put all the code into the one Worksheet_Change event Procedure (or you could have multiple procedures that are called by the single Worksheet_Change Event Procedure).
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,790
Members
449,468
Latest member
AGreen17

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