How to add zeros at the end

timpepu

Board Regular
Joined
Mar 4, 2008
Messages
89
Hi !

I have text like this 00000058 , I want add 0 so many times that it is 14 character long like this 00000058000000 .
How should I do it with VBA.

Br
Timo
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This one will:
00000000"000000"

but this one won't:
00000000000000

which is the one jasonb was referring to.
 
Upvote 0
It was also established at post #6 that the cell format option was unsuitable for the OP's needs.

Hotpepper, please correct me if I'm wrong.

Formatting the cell only changes the way you see the content of the cell, excel still retains the original value, meaning that the format is lost if the data is exported to another application.
 
Upvote 0
I don't know how the op is exporting the data, but I saved it out as a CSV file and the formatted cell retained its formatting in the output file.
At any rate, I also provided a macro.
 
Upvote 0
<P>Hi !</P>
<P> </P>
<P>Sub test()<BR>Dim c As Range<BR>For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)<BR>    c.NumberFormat = "@"<BR>    c = Format(c, String(8, "0")) & String(6, "0")<BR>Next<BR>End Sub</P>
<P> </P>
<P>This work fine but I want one thing , if the previous column value <> 02 this doesn't do this format and go to next row !</P>
 
Upvote 0
Since you mention a previous column, your numbers obviously aren't in column A.

Is that a text 02 or a 2 formatted that way?
 
Upvote 0
Since you mention a previous column, your numbers obviously aren't in column A.

Is that a text 02 or a 2 formatted that way?


It is text 02 , examble

Column A row 1 "02" Column B row 1 "58" This must format like 00000058000000
Column A row 2 "04" Jump this
etc.
 
Upvote 0
Code:
Sub test()
Dim c As Range
For Each c In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    If c.Offset(, -1) = "02" Then
        c.NumberFormat = "@"
        c = Format(c, String(8, "0")) & String(6, "0")
    End If
Next
End Sub
 
Upvote 0
Code:
Sub test()
Dim c As Range
For Each c In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    If c.Offset(, -1) = "02" Then
        c.NumberFormat = "@"
        c = Format(c, String(8, "0")) & String(6, "0")
    End If
Next
End Sub

Thanks ,it works fine but one thing what I didn't told ,how should it be if this "02" is like 0233456788 examble and I want to check that only first two characters are 02 ?

Br
Timo
 
Upvote 0
Are there any other conditions? I could then write the code once for all your conditions.
 
Upvote 0

Forum statistics

Threads
1,216,170
Messages
6,129,274
Members
449,497
Latest member
The Wamp

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