我在單個工作簿中有一系列動態工作表,每個表示一個工作周。 這些工作表有幾個驅動程序,我從中抓取數據並將其存儲在多個數組0-13中。這些數組然後將其數據轉儲到排序列表以輸出到彙總表。數據按日期細分,以便可以進行月度統計。將數據發送到數組:類型不匹配VBA/Excel
偶爾會有「額外」的驅動程序。這些驅動程序還需要捕獲並總結其數據。與專用駕駛員不同,「額外駕駛員」需要根據車型分類數據。
我的情況,檢查其他司機和檢查卡車類型,工作。我的數組爲額外的驅動程序,沒有。當數組嘗試存儲數據時,我收到類型不匹配錯誤。我很困擾;正如我遵守相同的格式前述陣列0-9爲(其中所有的功能和正常輸出 - 並從下面的代碼中省略)
我已經檢查了單元格的內容和這些值是準確的。但是,當我嘗試將這樣的數組存儲在數組中時,我得到的類型不匹配。
什麼是在這一個數組導致類型不匹配錯誤,然而其他人完美地運行?
的代碼生成該錯誤的實際行下面的代碼註釋中說明:
Dim arTemp
Dim arTemp1
Dim arTemp10
Dim list As Object, list1 As Object, list10 As Object
Dim d As Date
Dim x As Long, y As Integer
Dim ws As Worksheet
Set list = CreateObject("System.Collections.SortedList") 'Driver A
Set list1 = CreateObject("System.Collections.SortedList") 'Driver B
Set list10 = CreateObject("System.Collections.SortedList") '5-Ton Extra Drivers
Dim Truck5T As String, Truck3T As String, TruckC As String, StringCheck As String
Truck5T = "5-Ton"
For Each ws In Worksheets
If ws.Name <> "Summary" And ws.Name <> "WE Jan 08" Then
With ws
For y = 3 To 7
'Check for dates for each route
d = DateSerial(Year(.Cells(3, y)), Month(.Cells(3, y)), 1)
If list.containskey(d) Then
arTemp = list(d)
arTemp1 = list(d)
arTemp10 = list10(d)
Else
ReDim arTemp(13)
ReDim arTemp1(13)
ReDim arTemp10(13)
End If
arTemp(0) = arTemp(0) + .Cells(4, y) 'Grab # Deliveries (AM)
arTemp(1) = arTemp(1) + .Cells(5, y) 'Grab # of Pick Ups (AM)
arTemp(2) = arTemp(2) + .Cells(6, y) 'Grab # of Tires (AM)
arTemp(3) = arTemp(3) + .Cells(7, y) 'Grab Kilometres (AM)
arTemp(4) = arTemp(4) + .Cells(8, y) 'Grab # of Deliveries (PM)
arTemp(5) = arTemp(5) + .Cells(9, y) 'Grab # of Pick Ups (PM)
arTemp(6) = arTemp(6) + .Cells(10, y) 'Grab # of Tires (PM)
arTemp(7) = arTemp(7) + .Cells(11, y) 'Grab Kilometres (PM)
arTemp(8) = arTemp(8) + .Cells(12, y) 'Grab # of Deliveries (Total)
arTemp(9) = arTemp(9) + .Cells(13, y) 'Grab # of Pick Ups (Total)
arTemp(10) = arTemp(10) + .Cells(14, y) 'Grab # of Tires (Total)
arTemp(11) = arTemp(11) + .Cells(15, y) 'Grab Kilometres (Total)
arTemp(12) = arTemp(12) + .Cells(16, y) 'Grab Hours
arTemp(13) = arTemp(13) + 1
list(d) = arTemp
arTemp1(0) = arTemp1(0) + .Cells(4, y) 'Grab # Deliveries (AM)
arTemp1(1) = arTemp1(1) + .Cells(5, y) 'Grab # of Pick Ups (AM)
arTemp1(2) = arTemp1(2) + .Cells(6, y) 'Grab # of Tires (AM)
arTemp1(3) = arTemp1(3) + .Cells(7, y) 'Grab Kilometres (AM)
arTemp1(4) = arTemp1(4) + .Cells(8, y) 'Grab # of Deliveries (PM)
arTemp(5) = arTemp1(5) + .Cells(9, y) 'Grab # of Pick Ups (PM)
arTemp1(6) = arTemp1(6) + .Cells(10, y) 'Grab # of Tires (PM)
arTemp1(7) = arTemp1(7) + .Cells(11, y) 'Grab Kilometres (PM)
arTemp1(8) = arTemp1(8) + .Cells(12, y) 'Grab # of Deliveries (Total)
arTemp1(9) = arTemp1(9) + .Cells(13, y) 'Grab # of Pick Ups (Total)
arTemp1(10) = arTemp1(10) + .Cells(14, y) 'Grab # of Tires (Total)
arTemp1(11) = arTemp1(11) + .Cells(15, y) 'Grab Kilometres (Total)
arTemp1(12) = arTemp1(12) + .Cells(16, y) 'Grab Hours
arTemp1(13) = arTemp1(13) + 1
list1(d) = arTemp1
If .Cells(84, 2) <> "" Then
StringCheck = Left(.Cells(84, 2), 5)
If StringCheck = Truck5T Then
'5-ton (UpperL)
'first line below generates the type mismatch (cell value is what is expected from the worksheet)
arTemp10(0) = arTemp10(0) + .Cells(84 + 2, y) 'Grab # Deliveries (AM)
arTemp10(1) = arTemp10(1) + .Cells(84 + 3, y) 'Grab # of Pick Ups (AM)
arTemp10(2) = arTemp10(2) + .Cells(84 + 4, y) 'Grab # of Tires (AM)
arTemp10(3) = arTemp10(3) + .Cells(84 + 5, y) 'Grab Kilometres (AM)
arTemp10(4) = arTemp10(4) + .Cells(84 + 6, y) 'Grab # of Deliveries (PM)
arTemp10(5) = arTemp10(5) + .Cells(84 + 7, y) 'Grab # of Pick Ups (PM)
arTemp10(6) = arTemp10(6) + .Cells(84 + 8, y) 'Grab # of Tires (PM)
arTemp10(7) = arTemp10(7) + .Cells(84 + 9, y) 'Grab Kilometres (PM)
arTemp10(8) = arTemp10(8) + .Cells(84 + 10, y) 'Grab # of Deliveries (Total)
arTemp10(9) = arTemp10(9) + .Cells(84 + 11, y) 'Grab # of Pick Ups (Total)
arTemp10(10) = arTemp10(10) + .Cells(84 + 12, y) 'Grab # of Tires (Total)
arTemp10(11) = arTemp10(11) + .Cells(84 + 13, y) 'Grab Kilometres (Total)
arTemp10(12) = arTemp10(12) + .Cells(84 + 14, y) 'Grab Hours
arTemp10(13) = arTemp10(13) + 1
list10(d) = arTemp10
End If
End If
Next
End With
End If
Next
With Worksheets("Summary")
.Cells.Delete
For x = 0 To list.Count - 1 'Driver A
d = list.getkey(x)
.Cells(x + 43, 1) = Year(d)
.Cells(x + 43, 2) = Month(d)
.Cells(x + 43, 3) = list(d)(0)
.Cells(x + 43, 4) = list(d)(1)
.Cells(x + 43, 5) = list(d)(2)
.Cells(x + 43, 6) = list(d)(3)
.Cells(x + 43, 7) = list(d)(4)
.Cells(x + 43, 8) = list(d)(5)
.Cells(x + 43, 9) = list(d)(6)
.Cells(x + 43, 10) = list(d)(7)
.Cells(x + 43, 11) = list(d)(8)
.Cells(x + 43, 12) = list(d)(9)
.Cells(x + 43, 13) = list(d)(10)
.Cells(x + 43, 14) = list(d)(11)
.Cells(x + 43, 15) = list(d)(12)
Next
For x = 0 To list.Count - 1 'Driver A
d = list.getkey(x)
.Cells(x + 43, 1) = Year(d)
.Cells(x + 43, 2) = Month(d)
.Cells(x + 43, 3) = list1(d)(0)
.Cells(x + 43, 4) = list1(d)(1)
.Cells(x + 43, 5) = list1(d)(2)
.Cells(x + 43, 6) = list1(d)(3)
.Cells(x + 43, 7) = list1(d)(4)
.Cells(x + 43, 8) = list1(d)(5)
.Cells(x + 43, 9) = list1(d)(6)
.Cells(x + 43, 10) = list1(d)(7)
.Cells(x + 43, 11) = list1(d)(8)
.Cells(x + 43, 12) = list1(d)(9)
.Cells(x + 43, 13) = list1(d)(10)
.Cells(x + 43, 14) = list1(d)(11)
.Cells(x + 43, 15) = list1(d)(12)
Next
For x = 0 To list.Count - 1 'Additional Drivers: 5-Ton
d = list.getkey(x)
.Cells(x + 193, 1) = Year(d)
.Cells(x + 193, 2) = Month(d)
.Cells(x + 193, 3) = list10(d)(0)
.Cells(x + 193, 4) = list10(d)(1)
.Cells(x + 193, 5) = list10(d)(2)
.Cells(x + 193, 6) = list10(d)(3)
.Cells(x + 193, 7) = list10(d)(4)
.Cells(x + 193, 8) = list10(d)(5)
.Cells(x + 193, 9) = list10(d)(6)
.Cells(x + 193, 10) = list10(d)(7)
.Cells(x + 193, 11) = list10(d)(8)
.Cells(x + 193, 12) = list10(d)(9)
.Cells(x + 193, 13) = list10(d)(10)
.Cells(x + 193, 14) = list10(d)(11)
.Cells(x + 193, 15) = list10(d)(12)
Next
End With
嘗試調試您的代碼。例如。 arTemp10(0)'的原始值是多少? –
'list10(d)'的價值是什麼?通過我的閱讀,它應該是一個空的'Variant',而不是一個數組。 – Comintern
arTemp10(0)的原始值爲空 - 類型不匹配爲: arTemp10(0)= arTemp10(0)+ .Cells(84 + 2,y) 這是數組中的第一個條目。單元格中的數據很好。它根本沒有傳遞給數組。 list10(0)的值將成爲arTemp10(0)到arTemp10(13)的值,其中13是計數器,餘數是工作表單元格中的任何數據。 – WarOrdos