Concatenation and keeping leading zeros

alexransome

Board Regular
Joined
Jun 10, 2011
Messages
192
Hello,

This is part of the code I have in my sheet which takes the value from each cell and joins them in a third cell.

Code:
' Concatenate 1st Part
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("B2").Select
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    Cells(i, "B").Value = Cells(i, "C").Value & " -- " & Cells(i, "E").Value
Next I

The problem is the cells in 1 and 2 have been formatted with a leading zero, so a "1" becomes "01", but when the concatenation takes place the result generated is "1 -- 1", when it should be "01 -- 01".

For bonus points, the "--" is supposed to be a "/", but when that is added to a cell it is formatted to a date type. I know that if I add a " ' " to the cell then it is accepted, but not sure how to get that to happen within a VBA script.

Many thanks for any assistance.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi there,

This will format your destination cell as TEXT. il will kep leading zero and /


Code:
' Concatenate 1st Part
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("B2").Select
    For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
       Cells(i, "B").NumberFormat = "@"
       Cells(i, "B").Value = Cells(i, "C").Value & " -- " & Cells(i, "E").Value
   Next I

Regards,
 
Upvote 0
Hi, another one for you try:

Code:
Cells(I, "B").Value = "'" & Cells(I, "C").Text & "/" & Cells(I, "E").Text
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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