如何在vba中找到instr的前面的字符

如何在vba中找到instr的前面的字符

问题描述:找到一个 instr(数字),然后检查 instr 值前面是否带有任何字符。(例如 123、x123)。如果 instr 或带有字符的 instr 与 Sheet 2 中的值匹配,则消息框为是。

问题:我可以找到 instr,但无法检查 instr 前面是否包含任何字符。我该如何检查,是使用 left(chr) 函数还是其他什么。这是我的代码,但它所做的只是查找 instr,而不是查找带有 instr 的 chr。

  Sub tym()

Dim ws1 As Worksheet, wb As Workbook, ws2 As Worksheet
Dim b, c As Range, rngNums As Range, rngText As Range
Dim dNums, dText, rN As Long, rT As Long, t, m

Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet2")
Set c = wb.Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

Set rngNums = ws1.Range(ws1.Range("A1"), ws1.Cells(Rows.Count, 1).End(xlUp))
dNums = rngNums.Value
Set rngText = ws2.Range(ws2.Range("A1"), ws2.Cells(Rows.Count, 1).End(xlUp))
dText = rngText.Value

For rN = 1 To UBound(dNums, 1)
    b = "-" & dNums(rN, 1) & "-" (right here im defining to find instr but how do i add a function to find char front of instr)
    For rT = 1 To UBound(dText, 1)
        If InStr(1, dText(rT, 1), b) > 0 Then

          MsgBox "yes"

        End If
    Next rT
Next rN

End Sub

答案1

在此处输入图片描述

应该没有问题。如果

Instr(1,BigString,SubString)

返回1,那么 SubString 位于 BigString 的开头。

例如,查找子字符串“Α”

Sub dural()
   MsgBox InStr(1, ActiveCell.Value, "Alpha")
End Sub

在此处输入图片描述

相关内容