VBA function to remove decimal AND have a fixed length

john69

New Member
Joined
Dec 19, 2010
Messages
11
Hello all, I am a practicing VBA'er but this one is really tricky for me.

I have a set of weights that may range from .01 lb all the way to 1.5 lbs lbs all with different decimal points..

Example:

Before__________After
0.1845 becomes 001845
1.054 becomes 010540
1.16 becomes 011600
1.116 becomes 011160
0.278 becomes 002780
0.91 becomes 009100

What I am trying to do with this list is remove the decimal and have the results end up padding with zeros to make a fixed length of 6 char and where the decimal is always implied between the 2nd and 3rd char, so I have to left pad and right pad with zeros to make all output 6 characters.

Thank you so much in advance!!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi john69,

A bit clunky but try this with your data in Column A.

In B1 =A1&"000" and pull down.

In C1 =0&LEFT(B1,1)&MID(B1,FIND(".",B1)+1,4) and pull down.

Regards,
Howard
 
Upvote 0
You don't need a vba function, but I have provided one as well as a standard formula.

Excel Workbook
ABC
10.1845001845001845
21.054010540010540
31.16011600011600
41.116011160011160
50.278002780002780
60.91009100009100
Reformat Number



Code:
Function myFormat(s As Double) As String
  myFormat = Replace(Format(s, "00.0000"), ".", "", 1, 1, 1)
End Function
 
Last edited:
Upvote 0
Hello L. Howard. You're right, it is a bit clunky, but it does exactly what I was trying to accomplish! Thank you for your quick response :)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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