VBA: Add formula to a cell's string using VBA

Lefemmenikita

Board Regular
Joined
Jan 28, 2014
Messages
59
Office Version
  1. 2013
Platform
  1. Windows
HI

I am exporting a 6 digit number from a system into Excel. When this is pasted into excel, this is reformatted as a number instead of being left as a text value.

Column A with the code shows what the reformatted number looks like. Column B shows what I want it to look like.

Book1
AB
1CodeOutput Wanted
26571006571
3909856909856
42210002210
598521098521
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=REPT(0,6-LEN(A2))&A2



This is ok when the range of the reformatted cells remains the same and/or I am able to use a helper column like column B to insert a formula.

However, the reformatted numbers are not always in the same place and I am not always able to insert another column to insert a formula.

What I was wanting was a macro which allows me to reformat only the selected active cells.

e.g. If I select cell A4, I want VBA to insert a formula similar to =REPT(0,6-LEN(activecell.value))&Activecell.value

I have seen this done with 'round' formulas though I don't know how to adapt this to my current requirements.

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,143
Messages
6,123,287
Members
449,094
Latest member
GoToLeep

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