Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type mismatch error when referring to array element by location vba

I’m a VBA Noob, and I feel like I’m missing something basic here. I have an array, and I’m trying to access an element at the loc1 + 4th index. I keep getting the type mismatch error. Could someone please help me. Thanks in advance!

Dim atype As Variant
Dim loc1 As Integer

atype = Worksheets("Inputs").Range("B21")

loc1 = InStr(atype, "Loan")
loanpct = atype(loc1 + 4)

My cell value is Bond-61.87% Loan-38.13%, I want to extract the 38.13% part. With the comment below, I understand that its actually not an array. So should I try to convert the string to an array then?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To parse the string we use MID. The string is not an array with items that can be referenced like an array.

Dim atype As String
Dim loc1 As Integer
Dim loanpct As Double

atype = Worksheets("Inputs").Range("B21")

loc1 = InStr(atype, "Loan")
loanpct = Application.Evaluate(Mid(atype, loc1 + 5))

Debug.Print loanpct

Now loanpct is a number and can be used in later code.

But this assumes that everything after Loan- can be converted to a number. If there is any non numeric character after Loan- then it will fail.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading