Anyone know how to create a macro or VBA to modify part of a cell format ?
I very often need to use Greek symbols and subscripts within a cell for writing mathematical equations.
(Example: tmin = MAX (a, t, tn) where the min is a subscript, and the a should be the Greek alpha)
What I do is F2 the cell to edit it, select the text to change using the keyboard arrows (the mouse is too slow), then use the mouse to select the font drop-down list, type “s” to get near to the Symbols font to get the Greek equivalent of the letter selected. Then I use the keyboard to move to the desired subscript text, select it, move my hand back to the mouse to click on the “Font point-size decrease” button twice, and while I’m there, click on the Italics button. And so on, and so on…
I’ve tried writing a macro, but it only applies to the full cell content. Once in Edit mode, it doesn’t activate.
Any ideas ???
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Modify format of part of cell (2002)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Modify format of part of cell (2002)
- This topic has 21 replies, 8 voices, and was last updated 22 years, 12 months ago.
AuthorTopicWSkevinkdb
AskWoody LoungerMarch 27, 2002 at 8:19 pm #368838Viewing 2 reply threadsAuthorReplies-
notbrl
AskWoody LoungerMarch 27, 2002 at 8:51 pm #578913 -
WSunkamunka
AskWoody Lounger -
WSpieterse
AskWoody LoungerMarch 28, 2002 at 6:03 am #579003Hi Unkamunka,
<>
No it doesn’t.
Activecell.characters(5,10).Font.Size=24
Sets the size of characters 5 to 14 to 24Unfortunately one cannot run any code while editing a cell.
But one could use special characters to delimit the text that needs special formatting, e.g. _this_ causes the word This to be subscript, ^this^ to make it superscript, etcetera. Then use a macro that searches for these special characters, removes them and formats the area they enclosed appropriately.
Beats all the clicking, selecting and dragging the original poster experiences.I believe there is an insert symbol utility out there somewhere. maybe at:
-
-
WSAndrew Cronnolly
AskWoody LoungerMarch 27, 2002 at 10:25 pm #578934On the example you provided the following should convert the min part to subscript for each cell in the selection. Actually what it does is work on characters 2 to 4 inclusive regardless of what they are. You may be able to adapt it for your needs.
Dim oCell As Range For Each oCell In Selection With oCell.Characters(Start:=2, Length:=3).Font .Subscript = True End With Next oCell
Andrew C
-
WSunkamunka
AskWoody Lounger -
WSAndrew Cronnolly
AskWoody Lounger -
WSkevinkdb
AskWoody LoungerMarch 28, 2002 at 1:57 am #578953It’s not quite as easy as that unfortunately. Because the ‘min’ part is just an example, and the position of the letters to be subscripted or converted to Greek is never the same from one line to the next.
I think the solution for me would be to have a short-cut key for each of the 3 types of conversion.
One exists already. I can use Ctrl-I for converting the subscript to italics.
I need another for reducing the size of the subscript to 8,
and a third for converting font to Symbol.
With those 3 shortcuts, I could convert all I need without touching the mouse.
That’s the only way to do it when the text to convert is never in the same place.But how can I associate a shortcut key with an existing button? The button to reduce the font size by one point is already on my tool bar, but doesn’t have an associated short-cut (I think).
-
WSunkamunka
AskWoody LoungerMarch 28, 2002 at 7:20 am #579007If you read Andrew’s second post more carefully – the same point was made this morning by Jan Karel – you will see that you can set up a Macro using the Instr and InstrRev functions. (Look in the VBA Help files – the functions will search for “_” – or whatever delimiter you choose.) You then attach that macro to toolbar icons (Customise|Attach Macro) – linking the macro to a keyboard shortcut. Someone else may come up with some code before I get the chance. HTH
-
WSpieterse
AskWoody LoungerMarch 28, 2002 at 7:25 am #579009Here it is.
Option Explicit
Dim iStart() As Integer
Dim iEnd() As Integer
Dim iCount As Integer
Dim sChar() As String
Const sChars As String = “_^%&#”Sub ChangeLocalFormats()
Dim sCellTxt As String
Dim sTemp As String
Dim iLoop As Integer
iCount = 1
ReDim sChar(1)
ReDim iStart(1)
ReDim iEnd(1)
sCellTxt = ActiveCell.Value
If Left(ActiveCell.Formula, 1) = “=” Then Exit SubFor iLoop = 1 To Len(sCellTxt)
sTemp = Mid(sCellTxt, iLoop, 1)
If iLoop > Len(sCellTxt) Then Exit For
If InStr(sChars, sTemp) > 0 And Not sTemp = “” Then
ReDim Preserve sChar(iCount)
ReDim Preserve iStart(iCount)
ReDim Preserve iEnd(iCount)
sChar(iCount) = sTemp
GetStartEnd sCellTxt, sTemp
ActiveCell.Replace sTemp, “”, xlPart, , True
sCellTxt = ActiveCell.Value
iCount = iCount + 1
iLoop = 0
End If
Next
For iLoop = 1 To iCount – 1
If iStart(iLoop) > 0 Then
With ActiveCell.Characters(iStart(iLoop), iEnd(iLoop) – iStart(iLoop))
Select Case InStr(sChars, sChar(iLoop))
Case 1
.Font.Subscript = True
Case 2
.Font.Superscript = True
Case 3
.Font.Italic = True
Case 4
.Font.Bold = True
Case 5
.Font.Name = “Symbol”
End Select
End With
End If
Next
End Sub
Sub GetStartEnd(sCellTxt As String, sChar As String)
iStart(iCount) = InStr(1, sCellTxt, sChar)
iEnd(iCount) = InStr(iStart(iCount) + 1, sCellTxt, sChar) – 1
End SubSo in this:
_ sets subscript
^ sets superscript
% sets italic
& sets Bold
# sets Symbol fontTest it on this text:
^Super^_sub_%italic%&bold&#greek#
You can even nest formatting characters:
^super&boldsuper&^
-
WSkevinkdb
AskWoody Lounger -
WSpieterse
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSkevinkdb
AskWoody LoungerMarch 28, 2002 at 4:39 pm #579074Jan, sometimes I have more than one place in the equation where I need a subscript or a Greek symbol.
Only the first of each seems to get processed.
Example: f_r_ + #a# + S_n_ + #b#If I edit a second time to get the second ocurrence of each type right, the first occurrence gets undone.
-
WSpieterse
AskWoody Lounger -
WSkjktoo
AskWoody LoungerMarch 30, 2002 at 4:34 pm #579314Kevin,
I thought this would be an interesting project, but Jan had a solution before I had a chance to try anything. Anyway, since you still needed an improvement to the code, I (borrowing liberally from Jan
) wrote this code which should detect and format all instances of a pair of formatting codes.
Sub MyAttempt() Dim iChar() As Long Dim sCodeChar As String Dim sTestChar As String Dim sWork As String Dim i As Long Dim j As Long Dim k As Long Dim Index As Long Dim TextLength As Long Dim bFlag As Boolean Const sChars As String = "_^%&#" If Left(ActiveCell.Formula, 1) = "=" Then Exit Sub 'Find number of pairs of formatting codes and redim array TextLength = Len(ActiveCell.Value) For i = 1 To TextLength If InStr(sChars, Mid(ActiveCell.Value, i, 1)) > 0 Then j = j + 1 Next i If j 2 j / 2 Then MsgBox "Code pair is incomplete. Halt formatting procedure" Exit Sub End If j = j / 2 + 1 ReDim iChar(j, 3) Index = 1 'initialize 'loop through each formatting character For i = 1 To 5 'remove all code chars except the relevant code character from text string 'i is the index number for the formatting code sWork = "" sCodeChar = Mid(sChars, i, 1) For j = 1 To TextLength sTestChar = Mid(ActiveCell.Value, j, 1) If InStr(sChars, sTestChar) = 0 Then 'the char is not a formatting char sWork = sWork + sTestChar ElseIf sTestChar = sCodeChar Then 'the char is the relevant formatting char sWork = sWork + sTestChar End If Next j 'Save the starting and ending positions for each actual segment of text to 'be formatted and the formating character's index. Use k to count the instances 'of the formatting character. k = 0 For j = 1 To Len(sWork) If Mid(sWork, j, 1) = sCodeChar Then If Not bFlag Then 'First instance in a pair of codes iChar(Index, 1) = j - k bFlag = True k = k + 1 Else 'Second instance in a pair of codes iChar(Index, 2) = j - k - 1 bFlag = False k = k + 1 iChar(Index, 3) = i Index = Index + 1 End If End If Next j Next i 'Remove all formatting characters from active cell contents sWork = "" For i = 1 To Len(ActiveCell.Value) If InStr(sChars, Mid(ActiveCell.Value, i, 1)) = 0 Then sWork = sWork + Mid(ActiveCell.Value, i, 1) End If Next i ActiveCell.Value = sWork 'Apply formatting For i = 1 To Index - 1 With ActiveCell.Characters(iChar(i, 1), iChar(i, 2) - iChar(i, 1) + 1) Select Case iChar(i, 3) Case 1 ' Underscore .Font.Subscript = True Case 2 ' ^ .Font.Superscript = True Case 3 ' % .Font.Italic = True Case 4 ' & fixed per JohnBF .Font.Bold = True Case 5 ' # Greek .Font.Name = "Symbol" End Select End With Next i End Sub
-
WSkevinkdb
AskWoody LoungerMarch 29, 2002 at 10:33 pm #579413Overjoyed. I didn’t expect to get such a quick solution to this problem which has been irking me for about a year now. You guys are the gurus of the gurus.
I am completely satisfied with this now.
But, if you want to go one step further, and only if you want to (because I’m happy we got this far and it is completely functional for me, but there may be others out there who find this useful too):
When you add another term to an equation, with a subscript say, all the previous formatting gets undone.
I know I know I know, we’re getting too complicated here, and it’s really not necessary.Thanks a million Jan and kjktoo. Wonderful job. You have really impressed me.
-
WSkjktoo
AskWoody LoungerMarch 30, 2002 at 2:19 pm #579488Sorry, but the “next step” is beyond my knowledge. Perhaps Jan, a true excel guru, can help.
– edited –
What you might consider is keeping a copy of the original formula with the formatting codes in a nearby cell and modify that cell with your new term, copy to the desired location and run the macro, then.Ken
-
WSJohnBF
AskWoody LoungerMarch 31, 2002 at 6:36 pm #579497Very nice, KJToo.
FWIW I wrote myself an “entry” msgbox as a reminder of what the macro does and what the codes are:
If MsgBox(“Underscore_ = Subscript,” & vbLf & “Caret^ = Superscript,” & vbLf & _
“Percent% = Italic,” & vbLf & “Ampersand& = Bold,” & vbLf & “Pound # = Greek Symbol” _
& vbLf & vbLf & “Continue?”, vbYesNoCancel) vbYes Then Exit SubI put it after the other errorchecking routines.
-
WSpieterse
AskWoody Lounger -
WSpieterse
AskWoody Lounger -
WSfburg
AskWoody LoungerApril 2, 2002 at 1:49 am #579800that was an awesome piece of code. added to my bag of tricks.
FYI-Another solution was posted in post #88824 last Nov using a slightly different approach with a slightly different problem. It actually started out as a very different problem but the originator asked a 2nd question that was similar to this one. I’ve saved that one too.
Fred
-
-
-
-
Viewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Updating Windows 10 to Windows 11: 23H2 or 24H2?
by
Still Anonymous
1 hour, 37 minutes ago -
How can I update “Explorer Patcher”
by
WSplanckster
3 hours, 31 minutes ago -
Check out the home page for Signal
by
CAS
1 hour, 23 minutes ago -
Windows 11 and Trial version of MS Office
by
Tex265
26 minutes ago -
Windows 11 Insider Preview build 26120.3585 (24H2) released to BETA
by
joep517
7 hours, 54 minutes ago -
Windows 11 Insider Preview build 26200.5510 released to DEV
by
joep517
7 hours, 57 minutes ago -
Windows 11 Insider Preview Build 26100.3624 (24H2) released to Release Preview
by
joep517
8 hours, 1 minute ago -
Limits on User Names
by
CWBillow
4 hours, 43 minutes ago -
MS-DEFCON 4: Mixed bag for March
by
Susan Bradley
23 minutes ago -
Non Apple Keyboards
by
pmcjr6142
2 hours, 6 minutes ago -
How to delete your 23andMe data – The Verge
by
AJNorth
2 hours, 41 minutes ago -
7 common myths about Windows 11 (Microsoft AD)
by
EyesOnWindows
17 minutes ago -
Error updating to Win11 0x8024a205
by
bmeacham
1 day, 2 hours ago -
default apps
by
chasfinn
1 day, 1 hour ago -
Will MS Works 4 work in MS Win 11?
by
MileHighFlyer
1 day, 9 hours ago -
Adding links to text in Word 2000
by
sgeneris
6 hours, 57 minutes ago -
FBI warnings are true—fake file converters do push malware
by
Nibbled To Death By Ducks
1 day, 3 hours ago -
Classic and Extended Control Panel — no need to say goodbye
by
Deanna McElveen
5 hours, 40 minutes ago -
Things you can do in 2025 that you couldn’t do in 2024
by
Max Stul Oppenheimer
1 day, 14 hours ago -
Revisiting Windows 11’s File Explorer
by
Will Fastie
23 hours, 16 minutes ago -
Planning ahead for migration
by
Susan Bradley
18 hours, 15 minutes ago -
Yahoo mail getting ornery
by
Tom in Az
1 day, 2 hours ago -
Is Spectrum discontinuing email service?
by
Peobody
1 day, 5 hours ago -
Practice what you preach! A cautionary tale.
by
RetiredGeek
1 day, 2 hours ago -
Looking for Microsoft Defender Manuals/Tutorial
by
blueboy714
1 day, 6 hours ago -
Win 11 24H2 Home or Pro?
by
CWBillow
1 day, 2 hours ago -
Bipartisan Effort to Sunset the ‘26 Words That Created the Internet’..
by
Alex5723
3 days, 12 hours ago -
Outlook new and edge do not load
by
cHJARLES a pECKHAM
4 days ago -
Problem using exfat drives for backup
by
Danmc
4 days ago -
I hate that AI is on every computer we have!
by
1bumthumb
3 days, 2 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.