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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
If the range is one area only :
Code:
Sub v()
Dim rng As Range
Set rng = Selection 'Change range as required
If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
    'next step
Else: MsgBox "Not same"
End If
End Sub
If the range is not contiguous, post again.
 
Last edited:

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
I do not think you would need more then one macro to perform the task.

Please provide specific details.

Like the name of the defined range.
Search for what value.

If all values in range are not the same do what next.
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
For non-contiguous range (also will work for contiguous range :
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
    'next step
Else: MsgBox "Not same"
End If
End Sub
 

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
Hi, sorry for poor description...
In fact, what I need is not allow print cover sheet if there are different values in range of cells. (Range of cells means A9:AXXX - no. of filled cells in column vary as data copied from different sheet - depends on filter applied)
I would like to link this with button (code to preview cover sheet for printing). When push button "Print" code will check firstly if text in filled cells A9: AXXX is the same (e.g. name of supplier) If code finds difference (for example 2 different supplier's name found) ,message box will pop up (text : More suppliers selected) and cancel the printing. If all cells in range contains same value / text then finish printing (or initiate preview of cover sheet for printing)

Just need include this control before printing into this code :) :) :) - see below...

Thank you for your advice in advance

Sub Button1_Click()


ActiveWindow.SelectedSheets.PrintPreview


End Sub
 

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
I do not think you would need more then one macro to perform the task.

Please provide specific details.

Like the name of the defined range.
Search for what value.

If all values in range are not the same do what next.

Hi, sorry for poor description...
In fact, what I need is not allow print cover sheet if there are different values in range of cells. (Range of cells means A9:AXXX - no. of filled cells in column vary as data copied from different sheet - depends on filter applied)
I would like to link this with button (code to preview cover sheet for printing). When push button "Print" code will check firstly if text in filled cells A9: AXXX is the same (e.g. name of supplier) If code finds difference (for example 2 different supplier's name found) ,message box will pop up (text : More suppliers selected) and cancel the printing. If all cells in range contains same value / text then finish printing (or initiate preview of cover sheet for printing)

Just need include this control before printing into this code :) :) :) - see below...

Thank you for your advice in advance

Sub Button1_Click()


ActiveWindow.SelectedSheets.PrintPreview


End Sub
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
Did you try the macros posted?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,846
Office Version
2016
Platform
Windows
Another way without looping:

Code:
Function AreCellsEqual(ByVal R As Range, Optional ByVal CaseSensitive As Boolean) As Boolean

    With R
        If CaseSensitive Then
            AreCellsEqual = Evaluate("=AND(EXACT(" & .Address & "," & .Cells(1).Address & "))")
        Else
            AreCellsEqual = Evaluate("=COUNTIF( " & .Address & "," & .Cells(1).Address & ")=" & _
            "ROWS(" & .Address & ")" & "*COLUMNS(" & .Address & ")")
        End If
    End With

End Function


Sub Test()
    MsgBox AreCellsEqual(R:=Range("A1:F10"), CaseSensitive:=True)
End Sub
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,033
Office Version
2016
Platform
Windows
The range to be checked by the Function in post # 8 needs to be contiguous.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,223
Messages
5,485,476
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