2016-07-22 81 views
0

此代碼檢查列G,如果它的值是「Test」,則在E列獲取相應的值並將其粘貼到下一行。如果不是Isempty返回「」值,但繼續下一個語句

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 

回答

4

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?" 
+0

感謝您的解釋。 :)我很喜歡這個網站! –

0

更換

If Not IsEmpty(destRowValue) Then 

If destRowValue <> "" Then 
+0

它的作品!謝謝!請你再解釋一下? –

+0

感謝您的回覆。如果它解決了您的解決方案,請將其標記爲答案。 –