Five Digit Append

Evremonde

New Member
Joined
Sep 29, 2016
Messages
25
Looking for some VBA code to help with the following need. Rows in column G contains a five digit number. Rows in column H contains a combination of text and numbers. Combinations in column H always ends with “-XXXXX”. I need to append the five digit number in column G such that the five digit number replaces the “XXXXX” in column H. Additionally, if the cell is blank in Column G then do nothing to the corresponding row contents of column H. Can you help?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Does this macro do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReplaceXXXXX()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "H").End(xlUp).Row
  Range("H1:H" & LastRow) = Evaluate(Replace("IF(H1:H#="""","""",SUBSTITUTE(H1:H#,""XXXXX"",G1:G#))", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Deleted my attempt as Rick's solution is much better.
 
Last edited:
Upvote 0
This solution works. But now I see a flaw my original request. Suppose a number column G changes. Can this solution be made to update the corresponding row in column H in the code is run again (or multiple times)? Thank you for the help...
 
Upvote 0
This solution works. But now I see a flaw my original request. Suppose a number column G changes. Can this solution be made to update the corresponding row in column H in the code is run again (or multiple times)? Thank you for the help...
Give this macro a try then...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReplaceLast5Characters()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "H").End(xlUp).Row
  Range("H1:H" & LastRow) = Evaluate(Replace("IF(H1:H#="""","""",LEFT(H1:H#,LEN(H1:H#)-5)&G1:G#)", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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