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?
 

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
Are the values text or are they numbers with a custom format to show leading 0s
 
Upvote 0
Are the values in the cells you select constants or do the cells have formulas in them?

Are the cells you select formatted as Text or, if not, do they have an apostrophe in front of them when viewed in the Formula Bar?
 
Upvote 0
In that case try
Code:
i = i & Format(rng, "0000") & ";"
Change the number of 0s to match your format
 
Upvote 0
Are the values in the cells you select constants or do the cells have formulas in them?

Are the cells you select formatted as Text or, if not, do they have an apostrophe in front of them when viewed in the Formula Bar?

The numbers are state id's exported from Outlook. For example, 0747717 is the correct id. When exported to excel, its shown as 747717. I then do text to columns and format with the correct amount of numbers (in this case "0000000" but it varies by state. When I run the macro it reverts back to how it was before I formatted it.
 
Upvote 0
Thank you! That works but my issue is that the number of 0's can change. For this file it needs to be "0000000" but for another it could be "0000"
 
Upvote 0
Ok, try
Code:
i = i & rng.Text & ";"
 
Upvote 0
A slightly streamlined version
Code:
Sub BO_ID_Prep()
   Dim i as String
   Dim j As Long
   
   For j = 1 To Selection.Rows.Count - 1
      i = i & Selection(j).Text & ";"
   Next j
   i = i & Selection(j).Text
   ActiveCell.Offset(, 1).Value = i
End Sub
 
Upvote 0
A slightly streamlined version
Code:
Sub BO_ID_Prep()
   Dim i as String
   Dim j As Long
   
   For j = 1 To Selection.Rows.Count - 1
      i = i & Selection(j).Text & ";"
   Next j
   i = i & Selection(j).Text
   ActiveCell.Offset(, 1).Value = i
End Sub

A little bit more streamlined...
Code:
Sub BO_ID_Prep()
  Selection(1).Offset(, 1) = Join(Application.Transpose(Selection), ";")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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