VBA code - check if cells in range contain same value

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
Dears,

I would like to kindly ask you for your help regards VBA code..I'm looking for code which will be checking certain range of defined cells for values.
If all values will be the same, then will execute next defined step/macro...If different, Msg Box will pop up...and defined macro will not start up...


Thanks

Radek
 
I did not expressed myself correctly in origin...I need to assure that use right data. As I'm just beginner, not sure how to proceed with that... When push the button, data(text) in column A will be checked for equality. If all the same, run preview before printing. If not, pop up the message window and close macro. Problem will be that no. of rows in column A will change every time...depends of filtered name. I just need to make sure that only 1 name is selected (for further processing of data) Cells in considered / filtered range will be every time checked toward text / data in cell A2

A B C
1 Name
2 John
3 John -----> run preview for printing
4 John


1 Name
2 John
3 John -----> Pop up message box and close running macro
4 William


Many thanks for your help in advance Radek
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Cells in considered / filtered range will be every time checked toward text / data in cell A2

More details required re the structure of your data:
Is column A filtered? If so, will the name to be checked always be in A2?
Will you select the cells before running the macro or do you want the macro to do this (e.g. based on visible cells in column A)?

If the following code does not do what you want, advise exactly what it doesn't do :
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 <> v Then
        x = 1
        Exit For
    End If
Next
If x = 0 Then
    ActiveWindow.ActiveSheet.PrintPreview
Else
    MsgBox "Not same"
    Exit Sub
End If
End Sub
 
Last edited:
Upvote 0
Maybe :
Code:
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) Then
            MsgBox "Name in " & cel.Address(0, 0) & " is different."
            cel.Select
            Exit Sub
        End If
    Next
    ActiveSheet.PrintPreview
End If
End Sub
 
Upvote 0
Thanks Sir...works perfectly...Really appreciate your help...

Regards

Radek

Maybe :
Code:
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) Then
            MsgBox "Name in " & cel.Address(0, 0) & " is different."
            cel.Select
            Exit Sub
        End If
    Next
    ActiveSheet.PrintPreview
End If
End Sub
 
Upvote 0
Hi,

maybe one more question, may I?...how would be code looks like if same condition should be also applied for another column... Code now referring for A, but what if needs to be also checked column B for equality (in same moment)?

Thanks

Radek

Maybe :
Code:
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) Then
            MsgBox "Name in " & cel.Address(0, 0) & " is different."
            cel.Select
            Exit Sub
        End If
    Next
    ActiveSheet.PrintPreview
End If
End Sub
 
Upvote 0
Code:
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
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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