Adding a Specific Number of Zeros To A Material Number

SonicBoomGolf

Active Member
Joined
Aug 7, 2004
Messages
325
I am making a small macro to convert materials outside of SAP into materials that can be recognized by SAP. Here is a sample material transition I am looking to do.

Convert material 1031560 to 000000000001031560

Obviously, outside of SAP those extra zeros have no value so I remove them. However, sometime I have to add them back in when I want to query specific materials in BW.

The code below is getting me very close to where I want to go. All I need to figure out is how to add in a specific number of zeros to the front of a material number.

In the example above, I would have to add 11 zeros and format the number as text in order to get a legit 18 digit material number (in SAP, all our materials are 18 digits long).

Sample material numbers are in column A and the number of characters in the current material are identified in column b. How can I add in the number of zeros needed as I describe above? Thanks for the help in advance.

Sub SAPMaterial()
Dim x As Integer, rng As Range, c As Range
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("A1:A" & LastRow)
For Each c In rng.Cells
'Materials in SAP are 18 characters long, so 18-number of characters in current material number will identify how many zeros I have to add to get a legit material.
c.Offset(0, 1).Formula = "=len(" & c.Address & ")"
'Not sure what to do here to add in the zeros needed to get to a legit 18 digit material
Next c
End Sub
 

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).
One of my many mottos is, "Never send a macro to do a formula's job." Unless you have a really good reason for a macro in this instance, this formula will work.

value 1031560 in A2
formula in whatever cell you choose:
Code:
=RIGHT("000000000000000000"&FIXED(A2,0,TRUE),18)
The formula transforms your material number to alpha, with no decimals and no commas, then concatenates 18 zeroes to the front. Finally it selects the rightmost 18 characters.
 
Upvote 0
I was writing a macro to insert the formula, but I was using this which I think would suffice:

=RIGHT("000000000000000000"&A1,18)

Code:
Sub SAPMaterial()
Dim LastRow As Long, rng As Range
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("B1:B" & LastRow)
rng.Formula = "=RIGHT(" & Chr(34) & "000000000000000000" & Chr(34) & "&A1,18)"
End Sub
 
Upvote 0
Question:

Is there a reason you don't just want to use a formula:

Code:
=Text(A1,rept(0,11))

with this you can specify how many zeroe's you need
 
Upvote 0
Good call.

With that you would want 18:

=TEXT(A1,REPT(0,18))

or you could use 18 zeros:

=TEXT(A1,"000000000000000000")
 
Upvote 0
Looking at this problem, I would have suggested

=text(A1,"000000000000000000")

Just out of interest, does text take considerably more processing time?

I'm stuck with a couple of really slow spreadsheets and was wondering if there's a way to improve performance
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,153
Latest member
JazzSingerNL

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