Replacing variable/column prefixes and suffixes

iotapsi321

New Member
Joined
Oct 6, 2017
Messages
22
Hi, everyone. This is such a wonderful resource, glad I was able to find it. I am a beginner to excel so this may be an easy question for most of you. I have hundreds of variables that need to be renamed. Each variable has a prefix and a suffix. The variables are sequentially ordered in batches according to their keycode, but the variables are randomly ordered according to their suffixes. For instance, a variable named xaa.100 in column A1 needs to be renamed to time1_age. A more thorough example is provided below. There are 35 unique prefixes, and unique10 suffixes, so a simple find and replace all would take quite a bit of time. Is there a formula that can tackle this problem more efficiently? Thank you in advance for your help!

FROM

xaa.100xaa.101xaa.102bzb.101bzb.102bzb.100nrf.102nrf.100nrf.101

TO

time1.agetime1.gendertime1.racetime2.gendertime2.racetime2.agetime2.racetime2.agetime2.gender
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This piece of code will do that to the active sheet when its run:

VBA Code:
arr = Array("xaa", "time1", "100", "age", "bzb", "time2", "101", "gender", "nrf", "time2", "102", "race")

With ActiveSheet.UsedRange
    For i = LBound(arr) To UBound(arr) - 1 Step 2
        .Replace arr(i), arr(i + 1)
    Next
End With
 
Upvote 0
Thanks. I’m a beginner so I just want to make sure I understand correctly. Do I just enter this code into any cell within the active sheet?
 
Upvote 0
To be clear are these constants in the cells or as the result of formulas?
 
Upvote 0
Ok so this is VBA code ive given. You need to open the workbook and if you need this code more than once then you need to first save the workbook as macro enabled workbook. Open the macro enable workbook and then go to the sheet where you want the replace to happen. Press ALT and holding it down press F11. Press insert then module. Into there paste this:

VBA Code:
Sub ReplaceMacro()

arr = Array("xaa", "time1", "100", "age", "bzb", "time2", "101", "gender", "nrf", "time2", "102", "race")

With ActiveSheet.UsedRange
    For i = LBound(arr) To UBound(arr) - 1 Step 2
        .Replace arr(i), arr(i + 1)
    Next
End With

End Sub

Close the VBA editor. Staying on the page where you need these replacements go to the view menu then macros. Run the ReplaceMacro macro. They should have been replaced. It works by the above array in the code pairing the find what and replace with values. eg xaa will be replaced with time1, 100 will be replaced with age etc etc.
 
Upvote 0
Solution
Thank you so much! The code worked. I did run into an issue where the code for one line was so long that I had to carry it over another line, but hitting the enter button did not work, as I received an error message. Is there a quick fix for this? Thank you again for your time.
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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