Expected Array compiler error in excel VBA -
this code gives me compiler error, "expected array". know why is? want array filled corresponding values within rows fit given parameters.
a bonus question- once figure out, want count unique # of values got entered array. however, concerned first error, , appreciative of or guidance provided.
dim datecheck string dim lastrow long dim l integer dim long dim shipday date dim search string dim myarray variant l = 0 21 ' execute code whole month shipday = worksheets("june canada").cells(l + 10, 10).text 'check specific ship days = 1 worksheets("sheet1").usedrange.rows.count ' check every row of worksheet search = worksheets("sheet1").cells(i, 12).value ' variable check can vs usa if ((instr(1, search, "can", vbbinarycompare) = 1) _ , (worksheets("sheet1").cells(i, 8) = shipday) _ , (worksheets("sheet1").cells(i, 6).text = "invoice")) redim myarray(lb ub) lb = 0 ' lower bound = 0 ub = worworksheets("sheet1").usedrange.rows.count ' upper bound max # of rows myarray() = worksheets("sheet1").cells(i, 10).text ' add variable values dynamic array end if next ' count # of unique values in unique () ' worksheets("june canada").cells(l + 10, 8).value = ??? 'enter # of unique values sheet
next l
to fix initial array issue change this:
myarray() = worksheets("sheet1").cells(i, 10).text
to this:
myarray(i) = worksheets("sheet1").cells(i, 10)
i changed code demonstrate 1 way determine unique values entered in array:
option explicit sub yourfunctionnamehere() dim l long, long dim lb long, ub long dim datecheck string dim lastrow long dim shipday date dim search string dim myarray variant dim uniques object worksheets("sheet1") lb = 0 ub = .usedrange.rows.count redim myarray(lb ub) set uniques = createobject("scripting.dictionary") l = 0 21 shipday = worksheets("june canada").cells(l + 10, 10).value2 = 1 ub search = .cells(i, 12).value2 if ((instr(1, search, "can", vbbinarycompare) = 1) _ , (.cells(i, 8) = shipday) _ , (.cells(i, 6).text = "invoice")) myarray(i) = .cells(i, 10).value2 if not uniques.exists(myarray(i)) uniques.add key:=myarray(i), item:=i end if next next msgbox "unique values: " & uniques.count end end sub
Comments
Post a Comment