VBA:对于wRange中的每个wCell

VBA:对于wRange中的每个wCell,vba,excel,foreach,Vba,Excel,Foreach,我对每个循环都有问题 我正在尝试使用循环: 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)

我对每个循环都有问题

我正在尝试使用循环:

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
我得到一个运行时错误
对象变量,或者在行中未设置块变量

wRange=范围(单元格(5,countryCol),单元格(lastrow,countryCol))

当我将变量定义为:

Dim wRange,wCell As Range

我得到一个运行时错误:
objectrequired

用于wRange中的每个wCell


如果我像这样使用循环

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
我得到一个运行时错误
objectrequired


cell.Offset(0,3).Value=“后续接收”

如对原始问题的评论中所述,我需要使用
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

可能需要限定您的范围。您需要使用
Set
Set wRange=range(单元格(5,countryCol),单元格(lastrow,countryCol))
正如@Rory所说,您只需在每次为对象变量赋值时使用
Set
关键字即可。@Rory谢谢您和R3uk。我忘记了一些小细节。