2015-07-07 2 views
0

Этот код дает мне ошибку компилятора «Ожидаемый массив». Кто-нибудь знает, почему это? Я хочу, чтобы мой массив заполнялся соответствующими значениями в строках, которые соответствуют моим заданным параметрам.Ожидаемая ошибка компилятора Array в excel VBA

Бонусный вопрос. Как только я это выясню, я хочу подсчитать уникальные значения, введенные в этот массив. Тем не менее, я в основном обеспокоен своей первой ошибкой, и я очень благодарен за любую помощь или рекомендации.

Dim dateCheck As String 
Dim lastRow As Long 
Dim L As Integer 
Dim I As Long 
Dim shipDay As Date 
Dim search As String 
Dim myArray As Variant 

For L = 0 To 21 ' Execute code for whole month 

shipDay = Worksheets("June Canada").Cells(L + 10, 10).Text 'Check specific ship days 

    For I = 1 To Worksheets("Sheet1").UsedRange.Rows.Count ' Check every row of worksheet 

    search = Worksheets("Sheet1").Cells(I, 12).Value ' Variable to check for CAN vs USA 

    If ((InStr(1, search, "CAN", vbBinaryCompare) = 1) _ 
      And (Worksheets("Sheet1").Cells(I, 8) = shipDay) _ 
      And (Worksheets("Sheet1").Cells(I, 6).Text = "Invoice")) Then 

      ReDim myArray(lb To ub) 
      lb = 0 ' lower bound = 0 
      ub = WorWorksheets("Sheet1").UsedRange.Rows.Count ' upper bound is max # of rows 

      myArray() = Worksheets("Sheet1").Cells(I, 10).Text ' Add the variable values to the dynamic array 

      End If 



    Next I 

' Count # of unique values in unique() 

' Worksheets("JUNE canada").Cells(L + 10, 8).Value = ??? 'Enter # of unique values into sheet 

Следующая L

+0

Вы определили myArray как строку, и вы пытаетесь ее устранить. Redim работает только в массиве – Krishna

ответ

1

Чтобы исправить первоначальный вопрос массива изменить:

myArray() = Worksheets("Sheet1").Cells(I, 10).Text 

к этому:

myArray(I) = Worksheets("Sheet1").Cells(I, 10) 

я изменил код, чтобы продемонстрировать один из способов определить уникальный значения, введенные в массив:


Option Explicit 

Sub yourFunctionNameHere() 

    Dim L As Long, I As Long 
    Dim LB As Long, UB As Long 
    Dim dateCheck As String 
    Dim lastRow As Long 
    Dim shipDay As Date 
    Dim search As String 
    Dim myArray As Variant 
    Dim uniques As Object 

    With Worksheets("Sheet1") 
     LB = 0 
     UB = .UsedRange.Rows.Count 
     ReDim myArray(LB To UB) 
     Set uniques = CreateObject("Scripting.Dictionary") 
     For L = 0 To 21 
      shipDay = Worksheets("June Canada").Cells(L + 10, 10).Value2 
      For I = 1 To UB 
       search = .Cells(I, 12).Value2 
       If ((InStr(1, search, "CAN", vbBinaryCompare) = 1) _ 
         And (.Cells(I, 8) = shipDay) _ 
         And (.Cells(I, 6).Text = "Invoice")) Then 
        myArray(I) = .Cells(I, 10).Value2 
        If Not uniques.Exists(myArray(I)) Then 
         uniques.Add Key:=myArray(I), Item:=I 
       End If 
      Next 
     Next 
     MsgBox "Unique values: " & uniques.Count 
    End With 
End Sub 
Смежные вопросы