Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Subscript Values In VBA

Tags:

excel

vba

I have a string:
Range("T4").Value = "Rule 13s voilation"

I want to write 13s like 13s
i.e 3 and s are a subscript of 1.

Please suggest on how should I go about it in vba

like image 608
james Avatar asked Jun 20 '12 06:06

james


People also ask

What is a subscript in VBA?

Subscript out of range is an error we encounter in VBA when we try to reference something or a variable that does not exist in a code. For example, suppose we do not have a variable named x. Then, if we use the MsgBox function on x, we will encounter a “Subscript out of range” error.

Can you put a sub in a function VBA?

If you want Excel VBA to perform some actions, you can use a sub. Place a sub into a module (In the Visual Basic Editor, click Insert, Module). For example, the sub with name Area.

How do I fix subscript out of range error in VBA?

Explanation: the 'subscript out of range' error pops up because there's no 4th worksheet. To fix this error, change the 4 to a 1, 2 or 3 (or insert a new worksheet by clicking the plus sign).


2 Answers

Try the following:

Range("T4").Value = "Rule 13s voilation"
Range("T4").Characters(Start:=7, Length:=2).Font.Subscript = True

I am not sure how this will work for you with dynamic string lengths.

like image 181
Craig T Avatar answered Oct 31 '22 21:10

Craig T


Try doing it manually while recording a macro, and then looking at the resulting code. That will give you your answer.

Here's a cleaned up answer:

With Range("T4")
    .Value = "Rule 13s voilation" ' (sic)
    .Characters(Start:=7, Length:=2).Font.Subscript = True
End With
like image 28
Jean-François Corbett Avatar answered Oct 31 '22 23:10

Jean-François Corbett