Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

A huge bug when changing the font of a cell character in VBA

$
0
0

Hi Guys,

I wrote a very simple VBA macro to change the font of a character in a cell.

Here is the simplest VBA code that will illustrate the problem:

Public Sub ChangeFontOnLastChar()
  On Error Resume Next
  Dim oRange      As Excel.Range
  Set oRange = Selection
  oRange.Characters(2, 1).Font.Name = "Impact"
End Sub

I'm in Excel 2010. To reproduce the behaviour, just create a new, blank workbook, and insert this code in a standard VBA module (Alt+F11 to go to the VBA code editor, create a new module and copy/paste the code). Then goto to any cell and type in exactly two characters, for example "AA" and press enter to fix the cell. Then edit the cell (F2 or type in the formula bar), select the first char and set its font height to 72 and its font face to "Arial". Select the second char, set its font height to 48 and its font face to "Times New Roman". Press enter to fix the cell. See this sample result:

Before executing the macroEasy.

And now, the big bug: Let the selection stay on the cell and execute the "ChangeFontOnLastChar" macro (go to the Developer tab and click "Run macro", ...).

The result:

After executing the macro !So, as you can see, we have now three characters in the cell and two in the formula bar.

Happens also using other font faces.

I think Excel is also quite unstable after just that, you may experience a quite funny behaviour, if you try to change the selection.

I badly need this to work 100% rock solid.

Where or what is the problem ?




Viewing all articles
Browse latest Browse all 11829

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>