Excel VBA Run Time Error '13' Type MisMatch?

In summary: Type mismatch?In summary, Ronnin suggests that the error might be related to the code trying to concatenate or add the j=j+1 to the element of the array. I agree with him and suggest that the code be rewritten as follows: j = 1For i = 1 to NumElementsReDim Preserve MyNewArray(j)If MyArray(i) <> " " thenMyNewArray(j) = MyArray(i) &j = j + 1Next i
  • #1
Saladsamurai
3,020
7
Excel VBA:: Run Time Error '13' Type MisMatch??

I keep getting this error when I run this code. Nothing is highlighted..and VBA help is not working out for me.

It just started when I added the for loop in bold below:
Code:
Option Explicit
Option Base 1Private Sub MathML_Converter()
Dim i As Integer
Dim j As Integer
Dim NumElements As Integer
Dim Text As String
Dim MyArray() As String
Dim MyNewArray() As String'***************************************************************
   
    'Clear ActiveSheet

    Sheets("Sheet1").Range("1:65536").ClearContents
    Open "C:\Documents and Settings\Owner\Desktop\APC_Related\MathML.txt" _
    For Input As #1    i = 1
        Do While Not EOF(1)
            Text = Input(1, #1)
            ReDim Preserve MyArray(i)
            MyArray(i) = Text
            Cells(i, 1) = Text
            i = i + 1
    
        Loop
    
        Close #1
    
    
    NumElements = i - 1
'    MsgBox "Number of Elements SHOULD be " & NumElements
'    MsgBox "Number of Elements IS " & UBound(MyArray)
    
'*******************************************************************
[b]'Eliminate WhiteSpace From MyArray

    j = 1
    For i = 1 To NumElements
        ReDim Preserve MyNewArray(j)
        If MyArray(i) <> " " Then _
        MyNewArray(j) = MyArray(i) & _
        j = j + 1
        
    Next i[/b]

Any ideas? Am I missing the obvious (I usually do; not my specialty:redface:)?

Thanks
 
Physics news on Phys.org
  • #2


I have not used any VB in a while but I would say on the 3rd line after the for it might be trying to concantenate or add the j=j+1 to the element of the array.
 
  • #3


I agree with Ronnin. I would do it this way:

Code:
j = 1
For i = 1 To NumElements
    ReDim Preserve MyNewArray(j)
    If MyArray(i) <> " " then
        MyNewArray(j) = MyArray(i)
        j = j + 1
    EndIf
Next i

However, if you insist on not using the block IF then you should leave out the ampersand and use the colon.
 
  • #4


I noticed that you redim preserve the array on each pass of the loop. Your code would run faster if you precount the number of elements required beforehand and redim the new array before starting the loop.

Code:
j = 0
For i = 1 to NumElements
    If MyArray(i) <> " " then
        j = j + 1
    EndIf
next i

ReDim MyNewArray(j)

j = 1
For i = 1 to NumElements
    If MyArray(i) <> " " then
        MyNewArray(j) = MyArray(i)
        j = j + 1
    EndIf
Next i
Even though it is more code, the run time will be faster.
 
  • #5


I get the same error when I run the following code. If I comment out the lines in bold, the error goes away, but then the code doesn't do what I want! Any suggestions? Thanks in advance for any help you can give me. I'm no VBA expert and I'm hoping I'm just missing something that's obvious to others.

Code:
Private Sub CommandButton1_Click()
Dim i As Integer          'row index variable
Dim scount As Integer
Dim score As Integer
Dim coffset As Integer
Dim leftcount As Integer
Dim j As Integer          'column index variable
Dim LastCol As Integer

i = 4
Do While i < 6
    coffset = 2
    scount = 0
    leftcount = 1
    With ActiveSheet
        LastCol = .Cells(i, .Columns.Count).End(xlToLeft).column
    End With
    j = LastCol
    Do While coffset < 22
        score = Cells(i, j)
        j = LastCol - leftcount
        If j > 60 Then
            [B]If score > 0 Then[/B]
                Cells(i, coffset) = score
                scount = scount + 1
                coffset = 2 + scount
            [B]End If[/B]
        End If
        leftcount = leftcount + 1
    Loop
i = i + 1
Loop
End Sub
 
  • #6


In the highlighted code above, you are assuming that the value in the score variable is a number, whereas it is probably a string. You may need to convert it.
 

Related to Excel VBA Run Time Error '13' Type MisMatch?

What is a run time error in Excel VBA?

A run time error in Excel VBA is an error that occurs when a program is running, rather than during the coding process. These errors can happen due to a variety of reasons, such as incorrect data types, invalid calculations, or programming errors.

What does "Type Mismatch" mean in a run time error?

"Type Mismatch" in a run time error means that there is a mismatch between the data types being used in the code. This can happen when trying to perform calculations or operations on data that is not compatible with the chosen data type.

Why am I getting a "Type Mismatch" error in my Excel VBA code?

You may be getting a "Type Mismatch" error in your Excel VBA code due to attempting to perform operations on incompatible data types. This can also happen if there is a mistake in the code, such as using the wrong variable or using the wrong data type for a function or formula.

How can I fix a "Type Mismatch" error in Excel VBA?

To fix a "Type Mismatch" error in Excel VBA, you will need to identify where the error is occurring in your code and then check for any mistakes or mismatches in data types. Make sure all variables and functions are using the correct data types, and that any calculations or operations are being performed on compatible data types.

How can I prevent "Type Mismatch" errors in Excel VBA?

To prevent "Type Mismatch" errors in Excel VBA, it is important to carefully check and double check your code before running it. Make sure all variables are declared and initialized correctly, and that all data types are compatible. It may also be helpful to use error handling techniques, such as error handling statements or functions, to catch and handle any potential errors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
4K
  • Math Proof Training and Practice
3
Replies
100
Views
7K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top