Type a code in a cell and show a Name on the adjacent cell.

Agnarr

New Member
Joined
Jan 15, 2023
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hello everybody and you all do an amazing work.
I need your help please and i apologize in advance for any mistakes (English is not my native language).

I have 4-digit codes with corresponding names in one sheet and on another I've made it so you type one code on a cell and on the next cell to show the name of the corresponding code with an IF statement. The problem is that people keep erasing lines or add so i really need to do that with VBA.

So for example one writes a code in B2, presses enter and on cell C2 appears the name Mr. Smith.

Thank you all in advance. Any help will be appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This macro assumes your codes and names are in columns A and B respectively in Sheet1.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Sheet2 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a code in cell B2 and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "B2" Then Exit Sub
    Application.ScreenUpdating = False
    Dim fnd As Range
    Set fnd = Sheets("Sheet1").Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Target.Offset(, 1) = fnd.Offset(, 1)
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This macro assumes your codes and names are in columns A and B respectively in Sheet1.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Sheet2 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a code in cell B2 and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "B2" Then Exit Sub
    Application.ScreenUpdating = False
    Dim fnd As Range
    Set fnd = Sheets("Sheet1").Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Target.Offset(, 1) = fnd.Offset(, 1)
    End If
    Application.ScreenUpdating = True
End Sub
ok. but how will that work for the whole range?
I need it to be done not only for the b2 but for the whole range of b:b but also allow my to type anything else in there.
 
Upvote 0
Replace this line of code:
VBA Code:
If Target.Address(0, 0) <> "B2" Then Exit Sub
with this line:
VBA Code:
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
 
Upvote 0
Replace this line of code:
VBA Code:
If Target.Address(0, 0) <> "B2" Then Exit Sub
with this line:
VBA Code:
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
Thank you! Your help is greatly appreciated!
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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