问题
I have a spreadsheet with a list of people in the first column and various bits of information about them in the rest of each row. I have a userform which will populate and subsequently edit each record, which I want to have pop up when the user clicks on the person's name.
e.g.:
Name Age DOB Postcode
John Smith 55 3/6/1958 RM3 5WO
Mary Jones 22 4/2/1991 RM2 6TP
So I want to be able to click on John Smith (A2) to bring up a form with fields for his age, DOB, postcode etc. I also want to be able to use the form to add entries, so it will also function on at least one row below the list.
I've been given the following code to use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$2" Then
frm_tch_staffinfo.Show
End If
End Sub
I've tried using this in the sheet, but it doesn't seem to be working - nothing happens when I click on A2. Is there some error in the code or how I've implemented it? (I've put it in the code for the relevant sheet).
Additionally, I want to expand the target address to include the range of all the names (the number of which may change) and at least one cell below (but not the header in A1). Is it possible to define such a range? Either simply selecting A2 and below, or using a process which counts the cells which are full and adds on or more at the end.
Thanks for your help!
ETA:
I definitely require Excel for this, as my usage is that of a spreadsheet rather than a database. This was a simplified example: the data is in fact all numeric and used in relatively complex calculations in other worksheets.
回答1:
As mentioned in comment: the first part of your code works just fine (selection change).
I just did the test and there is nothing wrong with it.
Just note:
- Put the correct form name;
- Place the code in the correct sheet module
As for your second question, the code you are looking for:
Set oRange = Range("A1:A10")
If Not Intersect(Target, oRange) Is Nothing Then
'do something
End If
If you want to set the range dynamically, there have been many posts on this before.
Check out for example:
Getting the actual usedrange
来源:https://stackoverflow.com/questions/18892664/bring-up-user-form-when-clicking-on-cell