2014-02-05 41 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 =”“'然后就可以了 –