VBA code request to convert number variables

maddiefitz46

New Member
Joined
Nov 12, 2014
Messages
7
If wondering if someone would be able to generate a macro to convert numbered data sets:

I have large amounts of data in the rough format x-x-x (where each x represents a different number).

I want to convert this data so that they are formatted to an 18 digit form: xx-xxxx-xxxx-xxxx-xxxx where the number value will remain the same, but the rest of the string is populated with zeroes.

Here are some examples:

4-13-3 should become 04-0013-0003-0000-0000

15-7-12 should become 15-0007-0012-0000-0000

Is this possible?
 
UDF?

Row\Col
A​
B​
C​
D​
1​
6-12-206-0012-0002-0000-00006-12-2C1: =Unformat(B1)
2​
13-2-4013-0002-0040-0000-000013-2-40
3​
1-3B-201-003B-0002-0000-00001-3B-2
4​
10-2-3.210-0002-0003-0002-000010-2-3-2
5​
4-6A-5.704-006A-0005-0007-00004-6A-5-7
6​
12-23-4.212-0023-0004-0002-000012-23-4-2
7​
4-9-2.1204-0009-0002-0012-00004-9-2-12
8​
17-5C-22.417-005C-0022-0004-000017-5C-22-4
9​
5-3-405-0003-0004-0000-00005-3-4
10​
1-2-3.401-0002-0003-0004-00001-2-3-4
11​
15-12-23.1015-0012-0023-0010-000015-12-23-10

Code:
Function Unformat(ByVal sInp As String) As String
    sInp = "-" & sInp
    Do While InStr(sInp, "-0")
        sInp = Replace(sInp, "-0", "-")
    Loop
    Unformat = Replace(Trim(Replace(sInp, "-", " ")), " ", "-")
End Function
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Rick - I'm getting a run time error saying "mismatch", do you know how to fix that?
Depends... what line of code did it stop on when it issued that error message and what was in the cell it stopped on?

Do you have your data in Column B like I said I assumed you would?

Note:I tested the code before I posted it and it worked fine for me.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,649
Members
449,462
Latest member
Chislobog

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