Losing formatting using VBA code for concat

Cherhope

New Member
Joined
Dec 16, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi everyone. I'm new to using macros so would appreciate some help. I want to join multiple columns of data and retain the original formatting. Some columns contain text, some text and numbers and one column has the time. The VBA code I'm using joins the columns appropriately but the time is converted to decimals. i've tried all the different Paste Special commands (xl PasteValues, xlPasteValuesandNumberFormats etc.) but nothing seems to work.

Here is the VBA code:
Sub SpecialConcat()
Dim lastRow As Long
Dim wsData As Worksheet

'Where is the data?
Set wsData = Worksheets("RSG New Download")

Application.ScreenUpdating = False
With wsData
'Find where our data ends
lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("J1").EntireColumn.Insert
With .Range("J2:J" & lastRow)
.Formula = "=F2 & "" "" & G2 & "" "" & H2 & "" "" & I2 & "" "" & ""EDT"""
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
.Range("F:I").Delete
End With

Application.ScreenUpdating = True

End Sub

This is the original data:


HMB430H1FLEC0101Wednesday
6:00 PM​
CSB343Lec0101Saturday
1:00 PM​
ENG273Y1 YLEC5101Wednesday
4:00 PM​

I want to join the 4 columns so the entries looks like: HMB430H1F LEC0101 Wednesday 6:00 PM EDT

This is the result I get when I run the code:

HMB430H1F LEC0101 Wednesday 0.75 EDT
CSB343 Lec0101 Saturday 0.541666666666667 EDT
ENG273Y1 Y LEC5101 Wednesday 0.666666666666667 EDT

Thanks!

Cheryl
 

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
Welcome to the Board!

Concatenation does NOT maintain formatting, as it is only concatenating the underlying values, not any formatting applied to the cell.
If you want the formatting, you need to use the TEXT function (for native Excel formulas) or corresponding FORMAT function (in VBA).

See here for help on the TEXT function, for dates and times: Excel formula: Convert date to text | Exceljet
And see here for help on the FORMAT function (which works similarly, but just for VBA): VBA FORMAT Function

So, you would apply the appropriate TEXT/FORMAT function to just the one piece of the concatenation (and not the whole thing), like shown here:
 
Upvote 0
Taking Joe's adviced 1 step further, try this:
VBA Code:
.Formula = "=F2 & "" "" & G2 & "" "" & H2 & "" "" & Text(I2, ""hh:mm AM/PM"") & "" "" & ""EDT"""
 
Upvote 0
Welcome to the Board!

Concatenation does NOT maintain formatting, as it is only concatenating the underlying values, not any formatting applied to the cell.
If you want the formatting, you need to use the TEXT function (for native Excel formulas) or corresponding FORMAT function (in VBA).

See here for help on the TEXT function, for dates and times: Excel formula: Convert date to text | Exceljet
And see here for help on the FORMAT function (which works similarly, but just for VBA): VBA FORMAT Function

So, you would apply the appropriate TEXT/FORMAT function to just the one piece of the concatenation (and not the whole thing), like shown here:

Welcome to the Board!

Concatenation does NOT maintain formatting, as it is only concatenating the underlying values, not any formatting applied to the cell.
If you want the formatting, you need to use the TEXT function (for native Excel formulas) or corresponding FORMAT function (in VBA).

See here for help on the TEXT function, for dates and times: Excel formula: Convert date to text | Exceljet
And see here for help on the FORMAT function (which works similarly, but just for VBA): VBA FORMAT Function

So, you would apply the appropriate TEXT/FORMAT function to just the one piece of the concatenation (and not the whole thing), like shown here:
Thank-you Joe for replying to my post, for the resources and for letting me know that concatenation doesn't retain formatting!

Have a good day!
 
Upvote 0
Taking Joe's adviced 1 step further, try this:
VBA Code:
.Formula = "=F2 & "" "" & G2 & "" "" & H2 & "" "" & Text(I2, ""hh:mm AM/PM"") & "" "" & ""EDT"""
Thanks so much Alex! It worked!!!!! This will really help my team to place students into their study groups much faster.

Have a great day!

Cheryl
 
Upvote 0
Thank-you Joe for replying to my post, for the resources and for letting me know that concatenation doesn't retain formatting!
Did you take a look at the links I provided (there was a lot more to my reply than that).
The links basically explain and show you how to do what Alex posted.
 
Upvote 0
Did you take a look at the links I provided (there was a lot more to my reply than that).
The links basically explain and show you how to do what Alex posted.
Hi Joe. I apologize for not acknowledging that there was much more to your reply than what I acknowledged in my first reply to your post. I read through the links your provided multiple times but I have such a rudimentary knowledge of macros and coding that I couldn't write the code (I tried for several hours last night because I really wanted to learn how to do it). I tried several different versions of coding in several different positions within the VBA code that I already had but I couldn't figure it out. I didn't think to place it within the formula line (I kept placing it in it's own line). With your links explaining & showing and Alex's line of code, I know see what you meant by applying the formatting to only the one piece of the concatenate. It all makes sense know but my brain last night just couldn't get it even though you provided me with what I needed.
 
Upvote 0
Hi Joe. I apologize for not acknowledging that there was much more to your reply than what I acknowledged in my first reply to your post. I read through the links your provided multiple times but I have such a rudimentary knowledge of macros and coding that I couldn't write the code (I tried for several hours last night because I really wanted to learn how to do it). I tried several different versions of coding in several different positions within the VBA code that I already had but I couldn't figure it out. I didn't think to place it within the formula line (I kept placing it in it's own line). With your links explaining & showing and Alex's line of code, I know see what you meant by applying the formatting to only the one piece of the concatenate. It all makes sense know but my brain last night just couldn't get it even though you provided me with what I needed.
Gotcha.

Note that if you get "stuck", simply post back letting us know that you are having trouble (and post what you have tried), and we can help you work through it.
We appreciate that you make the effort, but know that sometimes a little more assistance may be required. But we don't know if you still need additional help unless you let us know.
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,769
Members
448,991
Latest member
Hanakoro

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