Forum Discussion

taufiq6121's avatar
taufiq6121
Copper Contributor
Jun 09, 2022
Solved

How to type superscript in Excel macro?

Hello,

I am new to writing Excel macros and really appreciate your help.

 

I am trying to write a macro to process all worksheets in an Excel file and import the data into one single worksheet. However, one of my worksheet titles is written with superscript (px²). And when I ran the macro, it will be Runtime Error 9: Subscript Out of Range.

 

When I checked the macro script, the px² was changed to px2 which I suspect is the reason. However, I cannot type px² in the macro script. Therefore, what can I do?

 

Changing the worksheet title will take some time because I have many Excel files exported from another software.

 

I am sorry if the question is hard to understand. I am happy to write it clearly.

  • taufiq6121 

    The Visual Basic Editor should be able to handle ²:

    Screenshot:

    Code:

    Sub Test()
        Dim wsh As Worksheet
        Set wsh = Worksheets("px²")
    End Sub

     

    • taufiq6121's avatar
      taufiq6121
      Copper Contributor

      Dear HansVogelaar 

       

      Thank you for your reply. However, I still cannot insert the superscript even by pasting from your code. Please find the screenshot below.

       

      When I tried copying the Superscript Two symbol into the VBA, the Superscript Two symbol changed to a question mark.

       

       

       

       

      • HansVogelaar's avatar
        HansVogelaar
        MVP

        taufiq6121 

        That's a pity - it might have to do with your language settings, but I'm afraid I don't have a solution for you, except renaming the sheets.

Resources