Display message in a cell based on value in a cell that is selected

minisp4m

New Member
Joined
Oct 29, 2015
Messages
3
Hello!

I have a table with codes E4:P250, example of codes mjk502, mjb333 etc. I would like, if possible, that if I select any cell, for example F65, that has the code mjk502, it displays what the code means in C2. All codes are in a different sheet in the same workbook.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You can't do that with native Excel functions, since there's no way to know which cell is active. But you can do it with VBA.

Open a COPY of your workbook to the sheet with the codes range E4:P250. Right click on the sheet tab on the bottom and select View Code. Paste the following code into the window that opens:
Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    [C2].ClearContents
    If Not Intersect(Target, Range("E4:P250")) Is Nothing Then
        On Error Resume Next
        [C2].Value = WorksheetFunction.VLookup(Target.Value, Sheets("Sheet7").Range("A1:B2"), 2, 0)
    End If
End Sub
The values in red refer to the locations on the current sheet. The values in blue are where the codes and meanings are defined in the other sheet, codes in column A, meanings in column B. Change these values as needed. Then close the VBA editor (Alt-Q or the X in the upper right). You should now get the meaning in C4 as you desire.
 
Upvote 0
You can't do that with native Excel functions, since there's no way to know which cell is active. But you can do it with VBA.

Open a COPY of your workbook to the sheet with the codes range E4:P250. Right click on the sheet tab on the bottom and select View Code. Paste the following code into the window that opens:
Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    [C2].ClearContents
    If Not Intersect(Target, Range("E4:P250")) Is Nothing Then
        On Error Resume Next
        [C2].Value = WorksheetFunction.VLookup(Target.Value, Sheets("Sheet7").Range("A1:B2"), 2, 0)
    End If
End Sub
The values in red refer to the locations on the current sheet. The values in blue are where the codes and meanings are defined in the other sheet, codes in column A, meanings in column B. Change these values as needed. Then close the VBA editor (Alt-Q or the X in the upper right). You should now get the meaning in C4 as you desire.

Thank you!!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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