VBA Type mismatch

Nervatos

New Member
Joined
Dec 19, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello there

I have tried to debug this code, but get this error type mismatch, I can't see where I falied.

VBA Code:
Sub testt()
    Dim i As Long
    Dim myArray, asArray, X As String
    X = "12k"

    myArray = Array("Y", "Z", "E", "P", "T", "G", "k", "h", "da", "d", "c", "m", "µ", "n", "p", "f", "a", "z", "y")
    asArray = Array(X * WorksheetFunction.Power(10, 24), _
                    X * WorksheetFunction.Power(10, 21), _
                    X * WorksheetFunction.Power(10, 18), _
                    X * WorksheetFunction.Power(10, 15), _
                    X * WorksheetFunction.Power(10, 12), _
                    X * WorksheetFunction.Power(10, 9), _
                    X * WorksheetFunction.Power(10, 6), _
                    X * WorksheetFunction.Power(10, 3), _
                    X * WorksheetFunction.Power(10, 2), _
                    X * WorksheetFunction.Power(10, 1), _
                    X * WorksheetFunction.Power(10, -1), _
                    X * WorksheetFunction.Power(10, -2), _
                    X * WorksheetFunction.Power(10, -3), _
                    X * WorksheetFunction.Power(10, -6), _
                    X * WorksheetFunction.Power(10, -9), _
                    X * WorksheetFunction.Power(10, -12), _
                    X * WorksheetFunction.Power(10, -15), _
                    X * WorksheetFunction.Power(10, -18), _
                    X * WorksheetFunction.Power(10, -21), _
                    X * WorksheetFunction.Power(10, -24))
   
    For i = LBound(myArray) To UBound(myArray)
        If Right(X, 1) Like myArray(i) Then
            Debug.Print asArray(i)
        End If
    Next i
End Sub

My error comes in
VBA Code:
asArray = Array(X * WorksheetFunction.Power(10, 24), _

                    X * WorksheetFunction.Power(10, 21), _

                    X * WorksheetFunction.Power(10, 18), _

                    X * WorksheetFunction.Power(10, 15), _

                    X * WorksheetFunction.Power(10, 12), _

                    X * WorksheetFunction.Power(10, 9), _

                    X * WorksheetFunction.Power(10, 6), _

                    X * WorksheetFunction.Power(10, 3), _

                    X * WorksheetFunction.Power(10, 2), _

                    X * WorksheetFunction.Power(10, 1), _

                    X * WorksheetFunction.Power(10, -1), _

                    X * WorksheetFunction.Power(10, -2), _

                    X * WorksheetFunction.Power(10, -3), _

                    X * WorksheetFunction.Power(10, -6), _

                    X * WorksheetFunction.Power(10, -9), _

                    X * WorksheetFunction.Power(10, -12), _

                    X * WorksheetFunction.Power(10, -15), _

                    X * WorksheetFunction.Power(10, -18), _

                    X * WorksheetFunction.Power(10, -21), _

                    X * WorksheetFunction.Power(10, -24))

I have tried to make this: SI Prefixes Unit Conversion Table - Tutorial, Definition, Example

Hope some can resolve it for me.
Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You assigned "12k" to the X variable which is not a number and then attempted to multiply by it in the next statements. Maybe assign 12000 to the variable instead? Also, you should then declare X as a Long instead of a String.
 
Upvote 0
Hello guys

My error comes in the whole line with asArray.
I have tried to make X as A Long instead of a String. Now I got the same error in this X = "12k".
Thanks for your replies.
 
Upvote 0
"12k" is not a numerical, it is a string.
Change to X = 12000
 
Upvote 0
As I said in my previous post, 12K is not a number so you cannot multiply by it. I think you want it to be 12000.
 
Upvote 0
But I need it to paste a letter after the number, so it calculate after what letter it is.
 
Upvote 0
This is the only thing you need

VBA Code:
Sub jec()
 Dim ar1, ar2, sq As Variant
 Dim X As String
 
 X = "12k"
 ar1 = Array("Y", "Z", "E", "P", "T", "G", "k", "h", "da", "d", "c", "m", "µ", "n", "p", "f", "a", "z", "y")
 ar2 = Array(24, 21, 18, 15, 12, 9, 6, 3, 2, 1, -1, -2, -3, -6, -9, -12, -15, -18, -21, -24)
       
 sq = Application.Match(Right(X, 1), ar1, 0)
 
 If IsNumeric(sq) Then
     Debug.Print Val(X) * (10 ^ ar2(sq - 1))
 Else
    MsgBox "no match"
 End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top