2015-11-16 55 views
2

我遇到For Each問題。VBA:對於wRange中的每個wCell

我試圖使用循環:

wRange = Range(Cells(5, countryCol), Cells(lastrow, countryCol)) 
For Each wCell In wRange 
    If wCell = "Unknown" Then 
     wCell.Offset(0, 3).Value = "Follow up with receiving" 
     wCell.Offset(0, followUp - countryCol) = "Send to receiving" 
    End If 
Next 

當我定義變量爲:

Dim wRange As Range 
Dim wCell As Range 

我得到一個運行時錯誤Object variable or With block variable not set上線

wRange = Range(Cells(5, countryCol), Cells(lastrow, countryCol))

當我將變量定義爲:

Dim wRange, wCell As Range

我得到一個運行時錯誤:Object required上線

For Each wCell In wRange


如果我使用像

For Each cell In wRange 
    If cell = "Unknown" Then 
     cell.Offset(0, 3).Value = "Follow up with receiving" 
     cell.Offset(0, followUp - countryCol) = "Send to receiving" 
    End If 
Next 

環路我得到一個運行時間錯誤Object required就行

cell.Offset(0, 3).Value = "Follow up with receiving"

+0

可能需要限定的範圍內。 – findwindow

+4

您需要使用'Set':'Set wRange = Range(Cells(5,countryCol),Cells(lastrow,countryCol))' – Rory

+1

正如@Rory所說的,每次你只需要使用'Set'關鍵字爲對象變量賦值。 – R3uK

回答

1

正如評論原來的問題說,我需要使用Set

Set wRange = Range(Cells(5, countryCol), Cells(lastrow, countryCol)) 
For Each wCell In wRange 
    If wCell = "Unknown" Or wCell = "" Then 
     wCell.Offset(0, reviewStatus - countryCol).Value = "Follow up with receiving" 
     wCell.Offset(0, followUp - countryCol) = "Send to receiving" 
    End If 
Next