Set column to bold, then combine columns

FredrikNilsen

New Member
Joined
Jan 25, 2021
Messages
26
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
I have a code to combine data from two columns: E and F.

VBA Code:
Sub MoveData()

Columns("E").Font.Bold = True

Application.ScreenUpdating = False
For i = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    Range("A" & i, "E" & i).VerticalAlignment = xlVAlignTop
    Range("E" & i) = Range("E" & i) & vbLf & Range("F" & i).Value
    Range("F" & i).ClearContents
Next
Application.ScreenUpdating = True

End Sub

I have added the "Columns("E").Font.Bold = True" part to make the current data in E bold, but when I run it, it also makes the new data from F bold, which it should not. Any suggestions?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Column E always "bold" with that statement
Try .characters method to set bold to string in E only
it should set for "new" E, start from 1 to "old" E lenght
PHP:
Sub MoveData()
Application.ScreenUpdating = False
Lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lr To 1 Step -1
    Range("E" & i) = Range("E" & i) & vbLf & Range("F" & i).Value
    Range("E" & i).Characters(1, Len(Range("E" & i)) - Len(Range("F" & i))).Font.Bold = True
Next
Application.ScreenUpdating = True
Range("A1:E" & Lr).VerticalAlignment = xlVAlignTop
Range("F1:F" & Lr).ClearContents
End Sub
 
Upvote 0
A slightly different approach. This does all the text combining in a single go (as well as the alignment and clearing like the previous code) and also provides a simpler way to calculate the number of characters that need to be bolded using the inserted vbLf character.

BTW, I also highly recommend always declaring all variables and forcing your self to do so by making this setting in your vba window.

1638242246456.png


VBA Code:
Sub MoveData_v2()
  Dim c As Range
  
  Application.ScreenUpdating = False
  With Range("E1:E" & Cells(Rows.Count, "A").End(xlUp).Row)
    .Value = Evaluate(.Address & "&char(10)&" & .Offset(, 1).Address)
    .EntireRow.Resize(, 5).VerticalAlignment = xlVAlignTop
    .Offset(, 1).ClearContents
    For Each c In .Cells
      c.Characters(1, InStr(1, c.Value, vbLf)).Font.Bold = True
    Next c
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
A slightly different approach. This does all the text combining in a single go (as well as the alignment and clearing like the previous code) and also provides a simpler way to calculate the number of characters that need to be bolded using the inserted vbLf character.

BTW, I also highly recommend always declaring all variables and forcing your self to do so by making this setting in your vba window.

View attachment 52293

VBA Code:
Sub MoveData_v2()
  Dim c As Range
 
  Application.ScreenUpdating = False
  With Range("E1:E" & Cells(Rows.Count, "A").End(xlUp).Row)
    .Value = Evaluate(.Address & "&char(10)&" & .Offset(, 1).Address)
    .EntireRow.Resize(, 5).VerticalAlignment = xlVAlignTop
    .Offset(, 1).ClearContents
    For Each c In .Cells
      c.Characters(1, InStr(1, c.Value, vbLf)).Font.Bold = True
    Next c
  End With
  Application.ScreenUpdating = True
End Sub
Works like a charm, thank you! And thank you for good advice, I'm learning. :)
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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