Приведенный ниже код VBA устанавливает диапазон ячеек в качестве commentArray, удаляет все пробелы из массива и создает новый пустой пустой массив, который называется commentResults.Затем я хочу объявить массив.
Существует возможность, в зависимости от моих исходных данных, что массив может оставаться пустым, поэтому приведенное ниже не работает для объявления
thisws.Cells(i, 19).Resize(columnsize:=UBound(commentResults) - LBound(commentResults) + 1).Value = commentResults
* 1005.* Поэтому я решил добавить проверку (оператор if после debug.print), который объявлял массив только в том случае, если array (0) не был пустым, но я постоянно получал ошибку 9, которую не могу разрешить.
Dim commentArray(4) As Variant
commentArray(0) = Cells(24, 4).Value
commentArray(1) = Cells(25, 3).Value
commentArray(2) = Cells(26, 3).Value
commentArray(3) = Cells(27, 3).Value
'a and b as array loops
Dim a As Long, b As Long
Dim commentResults() As Variant
'loops through the array to remove blanks - rewrites array without blanks into commentArray
For a = LBound(commentArray) To UBound(commentArray)
If commentArray(a) <> vbNullString Then
ReDim Preserve commentResults(b)
commentResults(b) = commentArray(a)
b = b + 1
End If
Next a
Debug.Print b
If IsError(Application.Match("*", (commentResults), 0)) Then
Else
thisws.Cells(i, 19).Resize(columnsize:=UBound(commentResults) - LBound(commentResults) + 1).Value = commentResults
b = 0
End If
Есть мысли о том, почему это может не сработать?
Я также пытался:
If commentResults(0) <> vbNullString Then
thisws.Cells(i, 27).Resize(columnsize:=UBound(commentResults) - LBound(commentResults) + 1).Value = commentResults
End If