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 ;

[TABLE="width: 500"]
<tbody>[TR]
[TD]2345[/TD]
[TD]2345;3658;2499[/TD]
[/TR]
[TR]
[TD]36568[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2499[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]



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

[TABLE="width: 500"]
<tbody>[TR]
[TD]0234[/TD]
[TD]what it does 234;35;2345[/TD]
[/TR]
[TR]
[TD]0035[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2345[/TD]
[TD]what I want 0234;0035;2345[/TD]
[/TR]
</tbody>[/TABLE]

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

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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