error 13 type mismatch on Variant Array

GhostEcho

New Member
Joined
Jun 5, 2018
Messages
6
Hey all,



I have a macro that filters a table, and then fills out a few columns of another table with totals based on the filtered table. Most of the time, it works perfectly, and is tremendously much faster than a sumifs in the worksheet. Unfortunately, out of 160 iterations, it errors out 23 times with error 13, at the red text, where it assigns the column to the array. I've hit a dead end, tried everything I can think of, and searched forums for answers without success. Hopefully one of you can figure it out.


Rich (BB code):
Sub test(CID As Long, myerror As Long)

Dim YTDPart() As Variant, YTDTotal() As Variant, YTDCAT() As Variant, CATPart() As Variant, CATCAT() As Variant, CATPrice() As Variant, CATOrdered() As Variant, CATTotes() As Variant, PriceBase() As Variant, PricePart() As Variant, PriceGroup() As Variant

CATPrice = Range("TOP[Price]").Value2

CATOrdered = Range("TOP[Ordered]").Value2

CATTotes = Range("TOP[Totes]").Value2

GoTo TheRest


skip:

If Err.Number = 13 Then
Cells(myerror + 3, 9) = "ERROR"​
Else
Cells(myerror + 3, 9) = "DIFFERENT ERROR"​
End If Exit Sub TheRest: On Error GoTo skip Sheets("REF").ListObjects("YTD").Range.AutoFilter Field:=2, Criteria1:="=" & CID, Operator:=xlAnd 'These need to be visible only... YTDPart = Range("YTD[Item]").SpecialCells(xlCellTypeVisible).Value2 YTDTotal = Range("YTD[Ext Sales]").SpecialCells(xlCellTypeVisible).Value2 YTDCAT = Range("YTD[Prod Group Desc]").SpecialCells(xlCellTypeVisible).Value2 'These need to be all CATPart = Range("TOP[Part]").Value2 CATCAT = Range("TOP[Category]").Value2 PriceBase = Range("Price[Base Price]").Value2 PricePart = Range("Price[Part]").Value2 PriceGroup = Range("Price[PriceList Code]").Value2 For i = 1 To UBound(CATPart) CATOrdered(i, 1) = (MySumif(YTDTotal(), YTDPart(), CATPart(i, 1)) > 0) Next i For i = 1 To UBound(CATPart) CATTotes(i, 1) = MySumif(YTDTotal(), YTDCAT(), CATCAT(i, 1)) Next i For i = 1 To UBound(CATPart) CATPrice(i, 1) = MyVlookup2(CATPart(i, 1), PricePart(), Worksheets(1).Range("AS3").Value, PriceGroup(), PriceBase()) Next i Sheets("REF").ListObjects("YTD").Range.AutoFilter Field:=2 Range("TOP[Ordered]").Value2 = CATOrdered Range("TOP[Totes]").Value2 = CATTotes Range("TOP[Price]").Value2 = CATPrice 'Stop Erase YTDPart, YTDTotal, CATPart, PricePart End Sub


The Mysumif and MyVlookup2 are as follows, but they don't error out, so it's just in case you're curious.

Rich (BB code):
Public Function MySumif(Sumit() As Variant, Searchit() As Variant, Mycrit As Variant)

Dim i As Long


MySumif = 0

For i = 1 To UBound(Searchit)

If Searchit(i, 1) = Mycrit Then MySumif = MySumif + Sumit(i, 1)

Next i

End Function


Public Function MyVlookup2(Mycrit As Variant, Searchit() As Variant, Mycrit2 As Variant, Searchit2() As Variant, Myresult() As Variant) For i = 1 To UBound(Searchit) If Searchit(i, 1) = Mycrit And Searchit2(i, 1) = Mycrit2 Then MyVlookup2 = Myresult(i, 1) Next i End Function

I would truly appreciate any help!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You have declared YTDPart as an array:

Dim YTDPart() As Variant

So you will get a type mismatch error if you try to assign its value as a scalar, i.e. a single cell value. Therefore the following line will error if Range("YTD[Item]").SpecialCells(xlCellTypeVisible) contains only a single cell:

YTDPart = Range("YTD[Item]").SpecialCells(xlCellTypeVisible).Value2

Your code will need to allow for Range("YTD[Item]").SpecialCells(xlCellTypeVisible) containing a single cell, as well as multiple cells.

Incidentally, the error line will also only work properly if the filtered rows are contiguous - you can't load discontiguous ranges into a variant in one code line like this.
 
Upvote 0
That makes sense... on the first instance of the error, although the filter leaves 60 lines visible, they are not continuous, and the first and second visible lines aren't continuous either. Similarly, future instances also have the same situation.

I sorted the data so that it's continuous and ran it again, now it works perfectly!

Thank you so much!
 
Upvote 0
Ok, I'm glad that helped.

Ideally your code should allow for the possibility of only one filtered row, or zero rows. But it sounds like you're happy there will always be 2+ continuous rows.
 
Upvote 0
For this particular application I am, due to the fact that the end result is being used to determine what the best selling categories for each customer is, so if they have no sales or only one line of sales, there's no reason to have it calculate. In those instances, I'll just use the overall best selling categories instead. Thank you again!
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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