Clear Range.PrefixCharacter without losing formatting in VBA

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
How can I remove Range.PrefixCharacter from a cell without removing all of its formatting? Note that Range.PrefixCharacter is readonly.

VBA Code:
Sub Test()
    Dim cell As Range
    Set cell = Range("$A$1")
    
    'Set some example formatting that we ideally don't want to be removed
    cell.Font.Bold = True
    cell.Interior.Color = RGB(255, 0, 0)
    
    'Set initial cell value including prefix character
    cell.Value = "'abc"
    
    Call SetNewCellValue(cell, "def", False)
    'Call SetNewCellValue(cell, "def", True)
    
    If cell.PrefixCharacter = "'" Then
        MsgBox "Cell still has PrefixCharacter"
    End If
End Sub

Sub SetNewCellValue(cell As Range, newValue As Variant, clearFormats As Boolean)
    If clearFormats Then
        'This will clear cell.PrefixCharacter but will also remove all other cell formatting
        cell.clearFormats
    End If
    
    cell.Value = newValue
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
In playing around with this, it looks to me like there is a flag in the cell's format data structure, that once set, is always set for non-numeric cell data, until the format is cleared.

My question would be: under what circumstances does this matter? All the string functions appear to make no distinction.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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