If Not Isempty возвращает значение "", но переходит к следующему утверждению

Этот код проверяет столбец G и, если его значение равно "Test", получает соответствующее значение в столбце E и вставляет его в следующую строку.

 Sub FindOpcode_Placepart()

Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim destCol_part As Integer, destRow As Integer
Dim currentRowValue As String
Dim destRowValue As String

sourceCol_opcde = 7   ' find last row in column E
rowCount = Cells(Rows.Count, sourceCol_opcde).End(xlUp).Row
destCol_part = 5
destRow = Cells(Rows.Count, sourceCol_opcde).End(xlUp).Row


'for every row, find the Opcode
For currentRow = 1 To rowCount
    If Cells(currentRow, sourceCol_opcde).Value = "Test" Then
        destRowValue = Cells(currentRow, destCol_part).Text


            If Not IsEmpty(destRowValue) Then ' this code returns "" value but proceeds with the next statement.

             destRow = currentRow + 1
             While Cells(destRow, sourceCol_opcde).Value = "Use-Limit"
                    Cells(destRow, destCol_part).Value = destRowValue
                    destRow = destRow + 1
            Wend

        End If
    End If
Next

End Sub

2 ответа

Решение

IsEmpty - это не проверка на наличие значения в ячейке, это проверка на предмет инициализации переменной.

'Note lack of Option Explicit.

Private Sub Example()
    Debug.Print IsEmpty(foo)    'True.
    foo = 42
    Debug.Print IsEmpty(foo)    'False.
End Sub

В коде из вопроса destRowValue инициализируется с Dim destRowValue As String, Чтобы проверить, имеет ли ячейка значение или нет, необходимо проверить vbNullString...

If Cells(currentRow, destCol_part).Text = vbNullString Then

... хотя имейте в виду, что если у вас есть возможность функции в целевой ячейке, вы также можете проверить IsError:

If Not IsError(Cells(currentRow, destCol_part)) And _
       Cells(currentRow, destCol_part).Text = vbNullString Then

Так как...

Cells(1, 1).Value = "=SomefunctionThatDoesntExist"
Debug.Print Cells(1, 1).Text    'Returns "#NAME?"

Замещать

If Not IsEmpty(destRowValue) Then

с

If destRowValue <> "" Then
Другие вопросы по тегам