2016-10-20 2 views
-1

하나의 통합 문서에서 개별 작업 주를 나타내는 일련의 동적 워크 시트가 있습니다. 이 워크 시트에는 데이터를 가져와 여러 배열 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 
+2

코드 디버깅을 시도하십시오. 예 : 'arTemp10 (0)'의 원래 값은 얼마입니까? –

+1

'list10 (d)'의 값은 무엇입니까? 내 읽기에 의해 배열이 아닌 빈'Variant'이어야합니다. – Comintern

+0

arTemp10 (0)의 원래 값은 아무 것도 없습니다. 형식 불일치는 다음 위치에 있습니다. arTemp10 (0) = arTemp10 (0) + .Cells (84 + 2, y) 배열의 첫 번째 항목은 어느 것입니까? 셀의 데이터는 문제가 없습니다. 단순히 배열로 전달되지 않습니다. list10 (0)의 값은 arTemp10 (0)부터 arTemp10 (13)의 값이되며 그 중 13은 카운터이고 나머지는 워크 시트 셀에있는 데이터입니다. – WarOrdos

답변

1

오류 여기

If List.containskey(d) Then 
    arTemp10 = list10(d) 
Else 
    ReDim arTemp10(13) 
End If 
을 :

이 오류를 생성 코드의 실제 행 아래 코드 코멘트에 주목 과제 arTemp10 = list10(d). 당신은

Set list10 = CreateObject("System.Collections.SortedList") '5-Ton Extra Drivers 

... 그러나 결코 그것에 어떤 항목을 추가 ... 여기에 새로운 SortedList 만듭니다.

코드 줄을 의미
Set foo = CreateObject("System.Collections.SortedList") 
Debug.Print TypeName(foo(42)) 'Prints "Empty" 

...

arTemp10(0) = arTemp10(0) + .Cells(84 + 2, y) 'Grab # Deliveries (AM) 

... 노력하는 것과 같습니다 빈 SortedList은 당신이 어떤 인덱스 을 통과 할 때 Empty를 반환합니다 배열이 아닌 변수의 색인을 생성하려면 다음을 수행하십시오.

Dim arTemp10 As Variant 'arTemp10 is implicitly Variant in your code. 
Debug.Print arTemp10(0) 'Type mismatch. 

귀하의 질문에 무엇이 이되어야하는지 명확하지 않지만 list10에 있어야합니다. 그러나 멤버를 배열로 사용하기 전에 초기화해야합니다.

편집 :

지금 코드의 더 사용할 수 있는지, 오류가 여전히 여기에 있습니다 :

If list.containskey(d) Then 
    arTemp = list(d) 
    arTemp10 = list10(d) 
Else 
    ReDim arTemp(13) 
    ReDim arTemp10(13) 
End If 

list10가 포함되어 있는지 확인하기 위해 시험이 없습니다 상품 d. 이는 대부분 다음 두 조건으로 나눌 필요가 있습니다.

If list.containskey(d) Then 
    arTemp = list(d) 
Else 
    ReDim arTemp(13) 
End If 

If list10.containskey(d) Then 
    arTemp10 = list10(d) 
Else 
    ReDim arTemp10(13) 
End If 
+0

나는 이해하지 못한다. arTemp에서 arTemp9에 이르기까지, 같은 방식으로 (위 코드에서 잘못되었음을 나타냄) 모든 데이터가 정의되고로드된다. 이것은 직장에서 또 다른 문제가 있음을 암시합니다. list10의 항목은 일반적으로이 시점에서로드됩니다 : – WarOrdos

+0

@WarOrdos -이 경우'Set list10 = CreateObject ("System.Collections.SortedList")'줄을 제거해야합니다. 그것은'list10'을 *** *** SortedList로 설정합니다. – Comintern

+0

실수로 입력하십시오. list10 (d) = arTemp10 '은 데이터가 배열의 목록에로드되는 곳입니다. 그러나이 배열과이 배열에서만 타입이 일치하지 않습니다. 저장된 데이터로 변환 한 다음 목록으로 전송합니다. – WarOrdos