Share via

vba compile error duplicate declaration in current scope

Anonymous
2024-02-24T06:34:28+00:00

It will be very helpful if someone fix the following code. There is an error showing compile error duplicate declaration in current scope.

Function superscript(ByVal value As Variant) As String

Dim superscript As String 

Dim i As Integer 

Dim digits As String 

Dim digit As String 

superscript = "" 

If IsNumeric(value) Then 

    digits = CStr(value) 

    For i = 1 To Len(digits) 

        digit = Mid(digits, i, 1) 

        Select Case digit 

            Case "0": superscript = superscript & ChrW(&H2070) 

            Case "1": superscript = superscript & ChrW(&HB9) 

            Case "2": superscript = superscript & ChrW(&HB2) 

            Case "3": superscript = superscript & ChrW(&HB3) 

            Case "4": superscript = superscript & ChrW(&H2074) 

            Case "5": superscript = superscript & ChrW(&H2075) 

            Case "6": superscript = superscript & ChrW(&H2076) 

            Case "7": superscript = superscript & ChrW(&H2077) 

            Case "8": superscript = superscript & ChrW(&H2078) 

            Case "9": superscript = superscript & ChrW(&H2079) 

        End Select 

    Next i 

End If 

superscript = superscript 

End Function

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-02-24T08:08:46+00:00

    B1: =FormatAsExponent(A1)

    Function FormatAsExponent(ByVal n As Double, Optional ByVal Places As Long = 2) As String
    'Return a nicely formatted text string (for use as UDF in Excel)
    Dim Numbers
    Numbers = Array(&H2070, &HB9, &HB2, &HB3, &H2074, &H2075, &H2076, &H2077, &H2078, &H2079)
    Dim i As Long, e As Long
    Dim es As String
    'Format N as scientific number
    FormatAsExponent = Format(n, "0." & String(Places, "0") & "E+000")
    'Get the exponent as value
    e = Val(Right(FormatAsExponent, 4))
    'Prepare our format
    FormatAsExponent = Left(FormatAsExponent, InStr(FormatAsExponent, "E") - 1) & "x10"
    If e < 0 Then
    'Add the negative sign
    FormatAsExponent = FormatAsExponent & ChrW(&H207B)
    End If
    es = CStr(Abs(e))
    'Add the exponent chars
    For i = 1 To Len(es)
    FormatAsExponent = FormatAsExponent & ChrW(Numbers(CInt(Mid(es, i, 1))))
    Next
    End Function

    Was this answer helpful?

    0 comments No comments