VBA code - check if cells in range contain same value
Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

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

  1. #11
    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

    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

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

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

    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 by footoo; Jul 22nd, 2019 at 08:21 PM.

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

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

    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

  4. #14
    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

    Thanks Sir...works perfectly...Really appreciate your help...

    Regards

    Radek

    Quote Originally Posted by footoo View Post
    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

  5. #15
    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,

    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

    Quote Originally Posted by footoo View Post
    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

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

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

    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

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
  •