VBA code - check if cells in range contain same value
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: VBA code - check if cells in range contain same value
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Apr 2019
    Posts
    11
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA code - check if cells in range contain same value

    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

  2. #2
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,510
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    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 by footoo; Jul 17th, 2019 at 08:10 AM.

  3. #3
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,675
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    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.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I’m not perfect yet. "Memories are forever"

  4. #4
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,510
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    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

  5. #5
    New Member
    Join Date
    Apr 2019
    Posts
    11
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    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

  6. #6
    New Member
    Join Date
    Apr 2019
    Posts
    11
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    Quote Originally Posted by My Aswer Is This View Post
    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

  7. #7
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,510
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    Did you try the macros posted?

  8. #8
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,237
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    3 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    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
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  9. #9
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,510
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    The range to be checked by the Function in post # 8 needs to be contiguous.

  10. #10
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,237
    Post Thanks / Like
    Mentioned
    34 Post(s)
    Tagged
    3 Thread(s)

    Default Re: VBA code - check if cells in range contain same value

    Quote Originally Posted by footoo View Post
    The range to be checked by the Function in post # 8 needs to be contiguous.
    Correct.

    Thank you.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •