VBA: Check if cells in range contain same value and ignore blanks

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Good day!

I was looking for some codes on how to create a prompt if the data is not the same.

I saw this which is workable but I need to apply the condition that if the cell is blank, it should ignore it:

=========================================================
"= Existing Code from Source: VBA code - check if cells in range contain same value

Sub vv()
Dim rng As Range, v, cel As Range, x%
Set rng = Selection 'Change range as required
v = rng(1)
For Each cel In rng
If cel <> v Then
x = 1
Exit For
End If
Next
If x = 0 Then
'next step
Else: MsgBox "Not same"
End If
End Sub

=========================================================

Sample data:

Title NameCountry
001MarcusAUS
002SeanUS
001MelodyAUS

Here: The macro should prompt and detect that the data is not the same for Title & Country Columns. Blank cells should be ignored.

Appreciate the help.

Cheers!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If you simply want to take the current code and add a condition to the loop to check to make sure the value is not blank, then you can do this:
Rich (BB code):
Sub vv()

    Dim rng As Range, v, cel As Range, x%

    Set rng = Selection 'Change range as required
    v = rng(1)

    For Each cel In rng
        If cell <> "" Then
            If cel <> v Then
                x = 1
                Exit For
            End If
        End If
    Next
    
    If x = 0 Then
        'next step
    Else: MsgBox "Not same"
    End If
    
End Sub
 
Upvote 0
If you simply want to take the current code and add a condition to the loop to check to make sure the value is not blank, then you can do this:
Rich (BB code):
Sub vv()

    Dim rng As Range, v, cel As Range, x%

    Set rng = Selection 'Change range as required
    v = rng(1)

    For Each cel In rng
        If cell <> "" Then
            If cel <> v Then
                x = 1
                Exit For
            End If
        End If
    Next
  
    If x = 0 Then
        'next step
    Else: MsgBox "Not same"
    End If
  
End Sub
Hey Joe! Thanks for sharing however in Column A, there are times that some rows are blank so I was hoping for some amendments. Appreciate the help.

I found this handy code but having problem ignoring the blank cells.

'Source: VBA code - check if cells in range contain same value
=====================
Sub vv()
Dim rng As Range, cel As Range
On Error Resume Next
Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
For Each cel In rng
If cel <> rng(1) Or cel(1, 2) <> rng(1, 2) Then
MsgBox cel.Address(0, 0) & " and/or " & cel(1, 2).Address(0, 0) & " is different."
cel.Select
Exit Sub
End If
Next
ActiveSheet.PrintPreview
End If
End Sub
=====================

Thanks!
 
Last edited:
Upvote 0
Did you try the code I posted, which was an amendment of your original code?
 
Upvote 0
Sorry, minor typo. Missed that the code originally only has one "l" in "cel" (not "cell").
So it should be:
VBA Code:
Sub vv()

    Dim rng As Range, v, cel As Range, x%

    Set rng = Selection 'Change range as required
    v = rng(1)

    For Each cel In rng
        If cel <> "" Then
            If cel <> v Then
                x = 1
                Exit For
            End If
        End If
    Next
  
    If x = 0 Then
        'next step
    Else: MsgBox "Not same"
    End If
  
End Sub
 
Upvote 0
Solution
Sorry, minor typo. Missed that the code originally only has one "l" in "cel" (not "cell").
So it should be:
VBA Code:
Sub vv()

    Dim rng As Range, v, cel As Range, x%

    Set rng = Selection 'Change range as required
    v = rng(1)

    For Each cel In rng
        If cel <> "" Then
            If cel <> v Then
                x = 1
                Exit For
            End If
        End If
    Next
 
    If x = 0 Then
        'next step
    Else: MsgBox "Not same"
    End If
 
End Sub
Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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