I have created a generic excel file to help demonstrate what I'm looking to do. The file I've named Tool.xlsm contains two worksheets; Sheet1 and Sheet2. Sheet1 will be designed to have a few fields which will accept user input. Sheet2 will be hidden from the user, but will contain the various drop down list selection options and their corresponding descriptions which should be displayed in another cell on Sheet1 when a specific code is selected. Additionally, Sheet2 will contain numerous ID#s in one column, and their corresponding usernames in the next column. The purpose of this is for the user to be able quickly associate an ID# with user it belongs to.
Here is what I have so far...I doubt I'm going about it as efficiently as I should be, but I'd greatly appreciate your all's expertise!
Sub Button1_Click()
'Based on selected value of C1, show corresponding message in J1'
'Can this be done by simply referencing the code descriptions in sheet2?'
If Range("C1") = "code 1" Then
Range("J1") = "code 1 description"
End If
If Range("C1") = "code 2" Then
Range("J1") = "code 2 description"
End If
'End of code selection'
End Sub
Sub Button2_Click()
'Based on ID# entered into C3, display corresponding name in J1 (Sheet2 contains ID#s with corresponding names)'
'There has to be an esier way to loop through 1000s of records and display corresponding ID# and Person''s name'
'Rather than assigning Person 1, to Range J1, I should be able to just reference the cell Sheet2!E3 but that doesn''t seem to work'
If Range("C3") = "1001" Then
Range("J1") = "Person 1"
End If
If Range("C3") = "34349090" Then
Range("J1") = "Person 83"
End If
'End ID# search'
End Sub
Sub Button3_Click()
'Clear unlocked cells'
End Sub
To your queries:
Can this be done by simply referencing the code descriptions in sheet2?
Yes. You can use VLOOKUP
formula for this.
Likewise, you could use the VLOOKUP
formula to return the names based on the IDs.
Eg, assume your usernames are in column K and ID's in column J:
On sheet 1, assuming the ID in cell C3, enter the formula: =VLOOKUP(C3, Sheet2!$J$K, 2, False)
you can use the worksheet_change event. Kindly set rngFindCode & rngFindCode1 range accordingly to refer to your data in sheet2.
Below is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Application.EnableEvents = False
If Target.Address = "$C$1" And Target.Cells.Count = 1 And Target.Value <> "" Then
Dim rngFindCode As Range '
Dim cellCode As Range
Set rngFindCode = Sheets("Sheet2").Range("C1:C100") ' Refers to range where code is in sheet 2
Set cellCode = rngFindCode.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not cellCode Is Nothing Then
Range("J1").Value = cellCode.Offset(0, 1).Value
End If
ElseIf Target.Address = "$C$3" And Target.Cells.Count = 1 And Target.Value <> "" Then
Dim rngFindCode1 As Range '
Dim cellCode1 As Range
Set rngFindCode1 = Sheets("Sheet2").Range("E1:E100") 'Refers to range where name is
Set cellCode1 = rngFindCode1.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not cellCode1 Is Nothing Then
Range("J1").Value = cellCode1.Offset(0, 1).Value
End If
Else
Range("J1").Value = ""
End If
Application.EnableEvents = True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.