Replace En Masse Values of Cells With New Names

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
In Sheet "YES_DATA" I have a table C2 to AD227 (some cells are blank) (Product ID in Column A)

Column AColumn BColumn CColumn DColumn EColumn F
PRODUCT ID(COUNT of values In Row)Detail_1Detail_2Detail_3Detail_4
0014Olive0.5kgSquareCheese
0022Gray10cm
0031Golden
0043Golden0.5kgSquare
0051Round



The ones that contain values have the old names associated with products

In Sheet New_Names I have a list of ID's in Column J, and the new name in Column K.

Column JColumn K
(OLD VALUE - TO BE REPLACED)(NEW VALUE)
OliveGreen
GoldenGold
RoundCicrular
0.5kg500g
SquareCube
CheeseCheesy
GrayGrey
10cm0.1m

So, in a new sheet I end up with

Column AColumn BColumn CColumn DColumn EColumn F
PRODUCT IDCOUNTDetail_1Detail_2Detail_3Detail_4
0014Green500gCubeCheesy
0022Grey0.1m
0031Gold
0043Gold500gCube
0051Circular

Hope that makes sense!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi Surkdidat,

I believe this is what you want.

Surkdidat2.xlsx
ABCDEF
1PRODUCT ID(COUNT of values In Row)Detail_1Detail_2Detail_3Detail_4
214Olive0.5kgSquareCheese
322Gray10cm
431Golden
543Golden0.5kgSquare
651Round
7
YES_DATA
Cell Formulas
RangeFormula
B2:B6B2=COUNTA(C2:F2)


Surkdidat2.xlsx
JK
1(OLD VALUE - TO BE REPLACED)(NEW VALUE)
2OliveGreen
3GoldenGold
4RoundCicrular
50.5kg500g
6SquareCube
7CheeseCheesy
8GrayGrey
910cm0.1m
10
New_Names


Surkdidat2.xlsx
ABCDEF
1PRODUCT ID(COUNT of values In Row)Detail_1Detail_2Detail_3Detail_4
214Green500gCubeCheesy
322Grey0.1m  
431Gold   
543Gold500gCube 
651Cicrular   
7      
8      
NewSheet
Cell Formulas
RangeFormula
A2:B8A2=IF(YES_DATA!A2="","",YES_DATA!A2)
C2:F8C2=IF(YES_DATA!C2="","",IF(ISNA(MATCH(YES_DATA!C2,New_Names!$J$2:$J$9999,0)),YES_DATA!C2,INDEX(New_Names!$K$2:$K$9999,MATCH(YES_DATA!C2,New_Names!$J$2:$J$9999,0))))
 
Upvote 0
For a macro approach, try this.

VBA Code:
Sub Update_Values()
  Dim a As Variant
  Dim i As Long
  
  With Sheets("New_Names")
    a = Range("J2", Range("K" & Rows.Count).End(xlUp)).Value
  End With
  Application.ScreenUpdating = False
  Sheets("YES_DATA").Copy After:=Sheets("YES_DATA")
  With Sheets(Sheets("YES_DATA").Index + 1).Columns("C:AD")
    For i = 1 To UBound(a)
      .Replace What:=a(i, 1), Replacement:=a(i, 2), LookAt:=xlWhole, MatchCase:=False
    Next i
    .Parent.Name = "YES_DATA_NEW"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi

I have tried this, however, i seems to have only made an exact copy of the original data (ie not updated with te new values)

PRODUCT ID(COUNT of values In Row)Detail_1Detail_2Detail_3Detail_4
214Olive0.5kgSquareCheese
322Gray10cm
431Golden
543Golden0.5kgSquare
651Round

For a macro approach, try this.

VBA Code:
Sub Update_Values()
  Dim a As Variant
  Dim i As Long
 
  With Sheets("New_Names")
    a = Range("J2", Range("K" & Rows.Count).End(xlUp)).Value
  End With
  Application.ScreenUpdating = False
  Sheets("YES_DATA").Copy After:=Sheets("YES_DATA")
  With Sheets(Sheets("YES_DATA").Index + 1).Columns("C:AD")
    For i = 1 To UBound(a)
      .Replace What:=a(i, 1), Replacement:=a(i, 2), LookAt:=xlWhole, MatchCase:=False
    Next i
    .Parent.Name = "YES_DATA_NEW"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi

I have just tried this again on a new spreadsheet and it has worked, so not sure what I did first time for it NOT to work. Thank you. I will now upscale this to my larger data, and hopefuly this will all work!

Hi

I have tried this, however, i seems to have only made an exact copy of the original data (ie not updated with te new values)

PRODUCT ID(COUNT of values In Row)Detail_1Detail_2Detail_3Detail_4
214Olive0.5kgSquareCheese
322Gray10cm
431Golden
543Golden0.5kgSquare
651Round
 
Upvote 0
(ie not updated with te new values)

has worked, so not sure what I did first time for it NOT to work
Sorry, it was my mistake. I had omitted a couple of "." from the code. That meant it would work or not work depending on what the active sheet was when the code ran. Try this instead.

Rich (BB code):
Sub Update_Values()
  Dim a As Variant
  Dim i As Long
  
  With Sheets("New_Names")
    a = .Range("J2", .Range("K" & Rows.Count).End(xlUp)).Value
  End With
  Application.ScreenUpdating = False
  Sheets("YES_DATA").Copy After:=Sheets("YES_DATA")
  With Sheets(Sheets("YES_DATA").Index + 1).Columns("C:AD")
    For i = 1 To UBound(a)
      .Replace What:=a(i, 1), Replacement:=a(i, 2), LookAt:=xlWhole, MatchCase:=False
    Next i
    .Parent.Name = "YES_DATA_NEW"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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