Type a code in a cell and have it replaced with a given name.

Agnarr

New Member
Joined
Jan 15, 2023
Messages
29
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 an excel where I need to type a 4 digit code in a cell and when after pressing enter, I need it to be replaced by a name corresponding to said 4-digit code.
There is a separate sheet with the list of each code to a name.
For example:
Type in cell C4 "3625" and hit enter. Automatically it changes into "Mr. Addams".
I'm not even sure something like this is possible. I tried to to work my way around this but my vba knowledge is limited.

If the above is not possible then please help me solve this: Check each cell in a column and for each cell that is not blank have it copied to another cell in another column.
Example:
In column C cells 1,2,5, and 8 have values. Have these cells be copied in column z in the 1,2,4 and 8 position accordingly but for each cell that becomes blank, clear contents from the corresponding one as well. (if i delete values from c3 have the cell in z3 also become empty).

All of the above i would need them to work for any sheet i place the macro in, so no ties to worksheets names.

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

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Assum sheet1, C1:C100 is input range
sheet names "code", A1:B3 is lookup range, with codes in column A, names in column B
Right click sheet1, view code, then paste below code into.
VBA Code:
Option Explicit
'This code is placed in "sheet1" (input sheet) module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim inputR As Range, codeR As Range, cell As Range
Dim f
Set inputR = Range("C1:C100") ' range of input data. adjust to actual range
Set codeR = Worksheets("code").Range("A1:B3") ' assums sheet "code", range "A1:B3" contains lookup code. Adjust to actual sheet-range
If Intersect(Target, inputR) Is Nothing Then Exit Sub
With Application
    .EnableEvents = False
    For Each cell In Target
        Set f = codeR.Find(cell.Value, , , xlWhole)
        If Not f Is Nothing Then cell.Value = f.Offset(, 1).Value
    Next
    .EnableEvents = True
End With
End Sub
Book1
ABC
11
22
33
4Mr A
5Mr B
63333
7Mr B
8Mr B
9Mr A
10Mr A
11Mr C
12Mr C
13Mr C
14Mr C
15Mr C
16Mr C
17
18
19
20
21
22
23
24
25
26
Sheet1

Book1
AB
11234Mr A
25678Mr B
33625Mr C
code
 
Upvote 0
Solution
Assum sheet1, C1:C100 is input range
sheet names "code", A1:B3 is lookup range, with codes in column A, names in column B
Right click sheet1, view code, then paste below code into.
VBA Code:
Option Explicit
'This code is placed in "sheet1" (input sheet) module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim inputR As Range, codeR As Range, cell As Range
Dim f
Set inputR = Range("C1:C100") ' range of input data. adjust to actual range
Set codeR = Worksheets("code").Range("A1:B3") ' assums sheet "code", range "A1:B3" contains lookup code. Adjust to actual sheet-range
If Intersect(Target, inputR) Is Nothing Then Exit Sub
With Application
    .EnableEvents = False
    For Each cell In Target
        Set f = codeR.Find(cell.Value, , , xlWhole)
        If Not f Is Nothing Then cell.Value = f.Offset(, 1).Value
    Next
    .EnableEvents = True
End With
End Sub
Book1
ABC
11
22
33
4Mr A
5Mr B
63333
7Mr B
8Mr B
9Mr A
10Mr A
11Mr C
12Mr C
13Mr C
14Mr C
15Mr C
16Mr C
17
18
19
20
21
22
23
24
25
26
Sheet1

Book1
AB
11234Mr A
25678Mr B
33625Mr C
code
I can't believe it was that simple... I have been trying to do a whole other set of crap...
Thank you so much!!!!!!!
 
Upvote 0
Now please one more thing... assume I want to do the same, but instead of the same cell, show the names to the cell next to the code.
 
Upvote 0
I can't believe it was that simple... I have been trying to do a whole other set of crap...
Thank you so much!!!!!!!
one more question about this one please... in the same sheet i have a button that clears the whole list. now it's lagging incredibly. is there a workaround?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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