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's avatar
    taufiq6121
    Jun 10, 2022

    Dear HansVogelaar 

     

    Thank you very much for your suggestion regarding the language. I managed to get the solution.

     

    By changing the Language for non-Unicode Programs to English (United States) in the Control Panel, the superscript and other symbols can be written in the VBA

     

    My default was Japanese (Japan) and even English (United Kingdom) was unsuccessful.

     

    Here's also the link that I referred to

     

    https://answers.microsoft.com/en-us/msoffice/forum/all/excel-2019-open-excel-2010-vba-with-strange-symbol/99fbad9d-282f-4da5-a19f-12f306363d1b 

     

    I really appreciate your help by responding to my question.

     

    Please have a good day.

     

     

7 Replies

  • 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