Как проверить, пуста ли первая запись массива в VBA - PullRequest
0 голосов
/ 30 мая 2019

Приведенный ниже код 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

1 Ответ

0 голосов
/ 30 мая 2019
Sub CommentArray()

Dim Comments As Range, c As Range
Set Comments = Union(Cells(24, 4), Range(Cells(25, 3), Cells(27, 3)))

Dim commentResults() As Variant
Dim i As Long

i = 0
For Each cell In Comments
    If cell.Value <> "" Then
        ReDim Preserve commentResults(i)
        commentResults(i) = cell.Value
        i = i + 1
    End If
Next cell

Dim debugStr As String

For i = LBound(commentResults) To UBound(commentResults)
    debugStr = debugStr & commentResults(i) & Chr(10)
Next i

MsgBox debugStr

End Sub

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...