2016-07-22 4 views
0

このコードは列Gをチェックし、値が "Test"の場合は列Eに対応する値を取得して次の行に貼り付けます。Not 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

If Not IsEmpty(destRowValue) Then 

を交換し、細胞が値を持っているかどうかを確認するためのチェックではない、それは変数がされているかどうかをチェックしますで初期化されました。質問からのコードで

'Note lack of Option Explicit. 

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

destRowValueDim 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 destRowValue <> "" Then 
+0

...ので、それは動作します!ありがとう!もう説明してもらえますか? –

+0

返事をありがとう。解決策が解決されたら、それを答えとしてマークしてください。 –

関連する問題