2014-02-05 46 views
2

我使用vba宏在some_col找到一個非空的單元格,從第1行開始。當我找到那個時,我想將它分配到pword,在相鄰列的單元格至cword。但似乎在運行此代碼時,循環並沒有終止,因爲即使找到非空單元,它也沒有結束。 (另外,如果我使用pword = Nothing則顯示類型不匹配錯誤,並且在msdn網站上,他們將此作爲用於空字符串的選項。)

任何人都可以請告訴這裏有什麼問題嗎?

類型不匹配錯誤與Nothing和循環沒有終止

Dim pword As String 
Dim cword As String 
Dim present_row As Long 
pword = "" 

Cells(1, some_col).Activate 
MsgBox "Active cell: " & ActiveCell.Address 

Do 
    If Not ActiveCell.value = "" Then 
     pword = ActiveCell.value 
     cword = ActiveCell.Offset(0, 1).value 
    End If 
     ActiveCell.Offset(1, 0).Activate 
     present_row = ActiveCell.Row 
Loop Until pword = "" 

回答

4

你可以使用Is Nothing語句只與對象類型,但String不是一個對象。

變化

Loop Until pword Is Nothing 

Loop Until pword = "" 
+0

@simico其工作:) – Gauranga

+0

@simico你能告訴爲什麼循環沒有結束,爲什麼調試器指向'ActiveCell.Offset(1 ,0)。激活'? – Gauranga

+1

+ 1 @Downey:把這句話放在'If not ActiveCell.value ='「'之前'pword =」「'然後就可以了 –