Type mismatch error, how to quick fix it?

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
395
Office Version
  1. 365
Platform
  1. Windows
The highlighted part went wrong. Not sure if I was checking the bug correctly but I checked the local window:
arraydic(1,2) is variant/double and arraychess(1,2) is null but type variant/variant(0 to 1)
is that the root cause and how do I solve it? Thanks in advance!

Rich (BB code):
Sub jaisdj()
Dim arraydic
Dim arraychess(1 To 20, 1 To 3)
Dim d As New Dictionary
Dim x, k, y

arraydic = Range("a2:c" & Cells(1000, "c").End(xlUp).Row)

For x = 1 To UBound(arraydic)
If d.Exists(arraydic(x, 1)) Then
y = d(arraydic(x, 1))
arraychess(y, 2) = arraychess(y, 2) + arraydic(x, 2)
arraychess(y, 3) = arraychess(y, 3) + arraydic(x, 3)
Else
k = k + 1
d(arraydic(x, 1)) = k
arraychess(k, 1) = Array(x, 1)
arraychess(k, 2) = Array(x, 2)
arraychess(k, 3) = Array(x, 3)
End If
Next x
Range("f2").Resize(k, 3) = arraychess
End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Not sure what you're trying to achieve, but arraydic and arraychess don't have the same number of dimensions. That's caused in another part of your code.
If you replace this part
VBA Code:
arraychess(k, 1) = Array(x, 1)
arraychess(k, 2) = Array(x, 2)
arraychess(k, 3) = Array(x, 3)

with this part
VBA Code:
arraychess(k, 1) = arraydic(x, 1)
arraychess(k, 2) = arraydic(x, 2)
arraychess(k, 3) = arraydic(x, 3)

the dimensions of both arrays will be equal again.
 
Upvote 0
Solution
You can simplify it this way.
It is important to redim the arraychess variable, because if your data input is greater than 20, then you will have an error.


VBA Code:
Sub jaisdj()
  Dim arraydic As Variant
  Dim arraychess As Variant
  Dim d As New Dictionary
  Dim x, k
 
  arraydic = Range("a2:c" & Cells(1000, "c").End(xlUp).Row)
  ReDim arraychess(1 To UBound(arraydic), 1 To 3)
  For x = 1 To UBound(arraydic)
    If Not d.Exists(arraydic(x, 1)) Then
      k = k + 1
      d(arraydic(x, 1)) = k
    End If
    k = d(arraydic(x, 1))
    arraychess(k, 1) = arraydic(x, 1)
    arraychess(k, 2) = arraychess(k, 2) + arraydic(x, 2)
    arraychess(k, 3) = arraychess(k, 3) + arraydic(x, 3)
  Next x
  Range("f2").Resize(k, 3) = arraychess
End Sub
 
Upvote 0
Not sure what you're trying to achieve, but arraydic and arraychess don't have the same number of dimensions. That's caused in another part of your code.
If you replace this part
VBA Code:
arraychess(k, 1) = Array(x, 1)
arraychess(k, 2) = Array(x, 2)
arraychess(k, 3) = Array(x, 3)

with this part
VBA Code:
arraychess(k, 1) = arraydic(x, 1)
arraychess(k, 2) = arraydic(x, 2)
arraychess(k, 3) = arraydic(x, 3)

the dimensions of both arrays will be equal again.
fml that's the stupidest mistake
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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