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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
The range to be checked by the Function in post # 8 needs to be contiguous.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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