如果Not Isempty返回“"”价值,但继续下一个声明 [英] If Not Isempty returns "" value but continues to the next statement

查看:152
本文介绍了如果Not Isempty返回“"”价值,但继续下一个声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码检查列G,如果其值为Test,则在E列获取相应的值并将其粘贴到下一行。

this codes checks column G and if the value of it is "Test" gets the corresponding value at column E and pastes it to the next row.

 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


推荐答案

IsEmpty 不是检查Cell是否有值,检查是否 变量已初始化

IsEmpty isn't a check to see if a Cell has a value, it's a check to see if the variable has been initialized.

'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 进行测试...

In the code from the question, destRowValue is initialized with Dim destRowValue As String. To check whether a cell has a value or not, you need to test against vbNullString...

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

.. 。记住,如果你有可能在目标单元格中​​有一个函数,你可能也想测试IsError:

...although keep in mind that if you have the possibility of a function in the target cell you might also want to test for 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?"

这篇关于如果Not Isempty返回“"”价值,但继续下一个声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆