Copy cells with part of the text bolded using VBA

mmetev

New Member
Joined
Jun 2, 2021
Messages
4
Office Version
  1. 365
Hello there. I have a sheet where part of the contents of some cell is bold. I want to copy on the values from this sheet to new sheet and keep that specific formatting with a macro.

I am currently using a macro that copies the sheet to new sheet (part of the code: "Act_Sheet.Copy before:=Sheets(a)") and then I am removing all formulas with this script:
VBA Code:
 ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value

The thing is that any cells containing text that is partially bold is converted to non-bold text.

So, is there a way to copy only the values but also have that specific formatting?

Thank you for your support.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
A working solution for you might be:
VBA Code:
Sub mmetev()
    Dim shtSource   As Worksheet
    Dim shtDest     As Worksheet
    Dim r           As Range
    
    Set shtSource = ActiveSheet
    shtSource.Copy After:=shtSource
    Set shtDest = ActiveSheet
    For Each r In shtDest.UsedRange
        If r.HasFormula Then
            r.Value = r.Value
        End If
    Next r
End Sub
 
Upvote 0
A working solution for you might be:
VBA Code:
Sub mmetev()
    Dim shtSource   As Worksheet
    Dim shtDest     As Worksheet
    Dim r           As Range
   
    Set shtSource = ActiveSheet
    shtSource.Copy After:=shtSource
    Set shtDest = ActiveSheet
    For Each r In shtDest.UsedRange
        If r.HasFormula Then
            r.Value = r.Value
        End If
    Next r
End Sub

Thanks a lot. Worked like a charm though the script execution is a bit slow as it goes through every single cell so I will test how it handles large files.
 
Upvote 0
Another option would be
VBA Code:
Dim Cl As Range

For Each Cl In ActiveSheet.UsedRange.SpecialCells(xlFormulas).Areas
   Cl.Value = Cl.Value
Next Cl
 
Upvote 0
the script execution is a bit slow as it goes through every single cell so I will test how it handles large files.
I'm aware of that, but it's a matter of making choices...
The additions in the code below may provide a little more relief in terms of performance. I've commented out two lines. See what happens if they're included for execution.
VBA Code:
Sub mmetev_r2()
    Dim shtSource   As Worksheet
    Dim shtDest     As Worksheet
    Dim r           As Range
    Dim i           As Long
    
    Set shtSource = ActiveSheet
    shtSource.Copy After:=shtSource
    Set shtDest = ActiveSheet
    
    Application.ScreenUpdating = False
    Application.Calculation = False
    Application.EnableEvents = False
    
    For Each r In shtDest.UsedRange
        If r.HasFormula Then
            r.Value = r.Value
        End If
'        i = i + 1
'        If i Mod 10 = 0 Then DoEvents
    Next r
    
    Application.EnableEvents = True
    Application.Calculation = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option would be
VBA Code:
Dim Cl As Range

For Each Cl In ActiveSheet.UsedRange.SpecialCells(xlFormulas).Areas
   Cl.Value = Cl.Value
Next Cl

This worked even better. I updated my code and deleted some columns with formulas which were not relevant to the end result before applying that part of the script.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

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