2013-05-21 35 views
2

我有一个电子表格,其中包含每行的汇总列(列K)。我需要匹配概要列中的某些单词,以便在新列(第V列)中分配类别名称。Excel使用vbTextCompare遍历单元格以分配类别

我试着用一个正常的excel来做这件事如果陈述,但我有sinc发现有一个限制。所以现在我试图使用下面的VBA代码。

Public Function getCategory() 

V_End_Of_Table = ActiveSheet.UsedRange.Rows.Count 'count the number of rows used' 

Dim cell As Range 


For Each cell In Range("K2:K" & V_End_Of_Table) 'loop through each row until end of table' 


If InStr(1, cell.Value, "Nationalities", vbTextCompare) > 0 Then 
    Range("V" & V_End_Of_Table).Value = "Nationalities" 
Else 
    Range("V" & V_End_Of_Table).Value = "No Match Found" 
End If 

Next 'move onto next cell' 

End Function 

所以我试图循环遍历每一行,匹配文本并赋值。就目前而言,我刚刚获得#VALUE!回。 如果我改变

范围( “V” & V_End_Of_Table).value的

MSGBOX

它将返回正确的字符串。

回答

4

这样的兄弟:

For Each cell In Range("K2:K" & V_End_Of_Table) 
    If InStr(1, cell.Value, "Nationalities", vbTextCompare) > 0 Then 
     Range("V" & cell.Row).Value = "Nationalities" 
    Else 
     Range("V" & cell.Row).Value = "No Match Found" 
    End If 
Next 

,而不是InStr你可以使用StrComp函数来比较2个字

+0

这工作辉煌谢谢:) – jampez77

+0

+1好asnwer daddio –

相关问题