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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,224,818
Messages
6,181,152
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