Results 1 to 5 of 5

Thread: Verify range in VBA question
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2012
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Verify range in VBA question

    Trying to enter code that will verify that a cell is within the correct range prior to running the rest of the macro. I've tried several ways that give errors. Suggestions appreciated. Thanks

    Code:
    Private Sub CommandButton3_Click()
    Dim rng As Range
    Set rng = ActiveCell
    ' Need code to verify that the active cell is within the named range. If not, MsgBox to click within range and then exit sub. If in range, then do the following:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("K3") & "Punch List " & rng.Value & " (" & Format(Date, "m.d.yy") & ")"
    End Sub

  2. #2
    New Member
    Join Date
    Mar 2013
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Verify range in VBA question

    the easiest way to do this is use something like the following:

    Code:
    if intersect(rng, your_named_range) is nothing then
      msgbox "click within range"
    else
    'do export
    end if

  3. #3
    Board Regular
    Join Date
    Jul 2012
    Posts
    325
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Verify range in VBA question

    Hi,

    Try something like,

    Code:
    Sub test7()
    
    Dim rrange As Range
    
    Set rrange = Range("D1:I10")
    
    If Not Intersect(ActiveCell, rrange) Is Nothing Then
        MsgBox "In Range."
    Else
        MsgBox "Not in Range."
    End If
    
    End Sub
    Jai

  4. #4
    Board Regular
    Join Date
    May 2003
    Posts
    1,082
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Verify range in VBA question

    Try modifying this to work with your code:

    Code:
    Sub WithinRange()
    Dim rng As Range, rngReq As Range
    Set rngReq = Range("NamedRange")
    Set rng = ActiveCell
    
        If Not Intersect(rng, rngReq) Is Nothing Then
            'Code to execute here
        Else
            response = MsgBox("Please select a cell within the required range", vbExclamation, "Error")
            Exit Sub
        End If
    End Sub
    =IF(U:U="don't care where you are going",UR:UR<>"LOST",UR:UR="LOST")

  5. #5
    Board Regular
    Join Date
    Nov 2012
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Verify range in VBA question

    Thanks for everyone's help. Got it working!

Some videos you may like

User Tag List

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
  •