Verify range in VBA question

kstrick9

Board Regular
Joined
Nov 5, 2012
Messages
122
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
[COLOR=#ff0000]' 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:[/COLOR]
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("K3") & "Punch List " & rng.Value & " (" & Format(Date, "m.d.yy") & ")"
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

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