Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

EXCEL VBA Question

Dear Experts,

In Excel97, I want a Calendar to pop-up when the user clicks on a cell in specific cell ranges. The user should only be able to enter dates chosen from the Calendar into the cell.

At the moment, I am using the following code. Form1 has the Calendar control on it. What this code does is that when an Active Cell is within the specified range, then when the user presses a shortcut, it runs the macro to pop-up the Calendar. This works fine, but I'd like the Calendar to pop-up when the user simply highlights a cell (makes a cell Active) in the specified ranges.

Thanks,
DocNash


Sub auto_open()
Dim KeyCells As String
KeyCells = "D3:D1000, E3:E1000, H3:H1000, J3:J1000, K3:K1000"
If Not Application.Intersect(ActiveCell, Range(KeyCells)) Is Nothing Then DoIt
End Sub

Sub DoIt()
UserForm1.Show
End Sub
0
DoctorNash
Asked:
DoctorNash
  • 3
1 Solution
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

You need to place your code presently in auto_open() in the Worksheet_SelectionChange() event.

Replacing "ActiveCell" with "Target", thus:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim KeyCells As String
KeyCells = "D3:D1000, E3:E1000, H3:H1000, J3:J1000, K3:K1000"
If Not Application.Intersect(Target, Range(KeyCells)) Is Nothing Then DoIt

End Sub


BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
PS. This area is probably the most appropriate for your question:

http://www.experts-exchange.com/Applications/MS_Office/Excel/
0
 
DoctorNashAuthor Commented:
Nice piece of work - thanks FanPages. By the way, it still seemed to work with 'ActiveCell' in the expression instead of 'Target'.
Regards,
DocNash
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Yes, ActiveCell would work if you had just selected a single cell, but if you had multiple cells selected, then Target would be the most appropriate.

Either way, glad you're happy with the solution.

BFN,

fp.
[ http://NigelLee.info ]
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now