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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,040
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,040
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,040
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,040
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,889
Messages
5,489,546
Members
407,697
Latest member
Lotte_4

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top