从单元格中提取文本并在vba

时间:2017-10-04 13:34:12

标签: excel string vba excel-vba if-statement

我希望从活动工作表中的单元格中提取文本,将该信息定义为字符串并将其传递给If,ElseIf,Else循环。我面临的问题是即使我拉的文字包含" dw"输出总是"未找到"。

它很奇怪,因为我使用消息框来查看ss是否实际上是正确定义的,并且确实如此,我不认为我的问题是拉动文本或定义字符串。

(这可能不是必要的信息,但是因为下面的代码是更大循环的一部分而导致的变化)

Dim ss As String
Dim iRow As Long

iRow = Cells.find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
lRow = iRow

ss = Cells(lRow, 5).Text
'the message box below shows me that ss is pulling the info from the sheet that I'm looking for
'msgbox (ss)

If InStr(ss, "dw") > 0 Then
   Cells(lRow, 18).Formula = "=MID(E12,SEARCH(""dw""," & Chr(34) & ss & Chr(34) & "),6)"

ElseIf InStr(ss, "lex") > 0 Then
   Cells(lRow, 18).Formula = "=MID(E12,SEARCH(""lex""," & Chr(34) & ss & Chr(34) & "),7)"

ElseIf InStr(ss, "tex") > 0 Then
   Cells(lRow, 18).Formula = "=MID(E12,SEARCH(""tex""," & Chr(34) & ss & Chr(34) & "),7)"

ElseIf InStr(ss, "qex") > 0 Then
   Cells(lRow, 18).Formula = "=MID(E12,SEARCH(""qex""," & Chr(34) & ss & Chr(34) & "),7)"

ElseIf InStr(ss, "qnxr") > 0 Then
   Cells(lRow, 18).Formula = "=MID(E12,SEARCH(""qnxr""," & Chr(34) & ss & Chr(34) & "),8)"

Else
   Cells(lRow, 18).Value = "Not Found"
End If

0 个答案:

没有答案