VBA Code to keep leading zeros when concatenating 2 columns.

Coder119

New Member
Joined
Oct 14, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Looking for VBA Code that can get leading zeros to stay when concatenating 2 columns using VBA? I already used the number format "000000000" in VBA code to get the one column to add the preceding zero when needed or keep the leading zero when removing dashes, but when I do the VBA code to concatenate them it drops the zero. So below I have some different sceniors in Number 1 of what I might see for data and the number 1 column would be concatenated with the items colum in a new column called Concatenate at least that is what my code to concatenate does.

1667304591182.png
 

Attachments

  • 1667303697007.png
    1667303697007.png
    14 KB · Views: 5

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Do you really need VBA to do this?
It is not a VBA issue that is causing the leading zeroes to drop. Concatenating with native Excel formulas will have the same issue.
It is because concatenate only affects the actual values, and does not consider the formatting applied to the cell (formatting just changes the view shown, not the underlying value).

Just like you use Formatting to show the leading zeroes on the sheet, you can use the TEXT function to show those when concatenating, i.e.
Excel Formula:
=TEXT(A1,"000000000") & ...

In VBA, the FORMAT function works the same way as the TEXT function does in Excel, i.e.
VBA Code:
TEXT(Range("A1").Value,"000000000") & ...
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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