VBA keep leading zeros

rurounisena

New Member
Joined
Apr 13, 2018
Messages
7
Sub BO_ID_Prep()


Dim rng As Range


Dim i As String


For Each rng In Selection


i = i & rng & ";"


Next rng


ActiveCell.Offset(1, 1).Value = Trim(i)


ActiveCell.Offset(2, 1).Select


ActiveCell.FormulaR1C1 = "=LEFT(R[-1]C,LEN(R[-1]C)-1)"


Selection.Copy


ActiveCell.Offset(1, 0).Select


Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Application.CutCopyMode = False


End Sub


The above script takes all selected cells and puts their data into one cell separated by a ;

23452345;3658;2499
36568
2499

<tbody>
</tbody>



My issue is when the numbers have leading zeros they are dropped when being added.

0234what it does 234;35;2345
0035
2345what I want 0234;0035;2345

<tbody>
</tbody>

Can someone please help me with this?
 
When I try that it looses the format.
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
My experience with Outlook is that Outlook Exports to a CSV or PST file. I would assume you are exporting to a CSV file.
Opening a CSV file with Excel allows too many assumptions.
Instead, use the Import Text File, Data\Get External Data\From Text File, and during that process make sure to designate that field to be imported as Text.
 
Upvote 0
In that case I suspect that you missed post#4
:oops: Yep, missed it. I saw Message#7 and figured they could not be custom formatted given the different overall size... I was wrong.

Okay, given that, here is a macro that, while not as streamlined as my original posted code, is still slightly more streamlined (one code line shorter) than your slightly streamlined macro...
Code:
Sub BO_ID_Prep()
  Dim Cell As Range, Combo As String
  For Each Cell In Selection
    Combo = Combo & ";" & Cell.Text
  Next
  Selection(1).Offset(, 1) = Mid(Combo, 2)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,451
Members
449,161
Latest member
NHOJ

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