VBA code - check if cells in range contain same value

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
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
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
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:

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
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
 

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
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
 

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
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
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,221
Messages
5,485,464
Members
407,502
Latest member
Bloople

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top