Trying to call a macro on selection of a cell in a range

  • Thread starter Thread starter The Narcissist
  • Start date Start date
T

The Narcissist

Hi,

This might come across as a novice question, as I am in fact, a novice at
vba. I'm trying to call a macro if a specific cell in a range is selected.
This is what I've coded.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Start_Range As Range
Set Start_Range = Range("C2:C1001")
Dim End_Range As Range
Set End_Range = Range("D2:D1001")
If Target.Range = Start_Range or Target.Range = End_Range Then
frmCalendar.Show
End If

End Sub

However, I'm getting a Compile error stating Argument not optional.

Can anyone please guide me to code this correctly?

Thanks in advance.

Sam
 
Got a solution to this through another forum. This is what was suggested and
worked.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim my_Range As Range
Set my_Range = Intersect(Target, Range("C2:C1001,D2:D1001"))
If Not my_Range Is Nothing Then frmCalendar.Show
End Sub
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Start_Range As Range
Set Start_Range = Range("C2:C1001")
Dim End_Range As Range
Set End_Range = Range("D2:D1001")
If Not Intersect(Target, Start_Range) Is Nothing Or _
Not Intersect(Target, End_Range) Is Nothing Then

frmCalendar.Show
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top