在Excel VBA中的预期数组编译器错误

此代码给我一个编译器错误,“预期数组”。 有人知道这是为什么吗? 我想我的数组填充适合我的给定参数的行内的相应的值。

一个额外的问题 – 一旦我明白了这一点,我想统计input到数组中的唯一值。 但是,我主要关心的是我的第一个错误,我非常感谢任何帮助或指导。

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

要修复最初的数组问题,请改变这个:

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

对此:

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

我更改为代码来演示确定在数组中input的唯一值的一种方法:


 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