VBA如何在一定范围内查看并计算第一个连字符前面的字符数“ - ”如果它是3然后运行一个sub,我写了else if
它是4然后运行另一个子?
我不在乎什么文字只是在第一个“ - ”之前有多少个字符
范围内的两种文本示例D10-AA-02A-P2 = 3 '**D10**
C047-AB-V34 AD-P2 > V34-AD-C047 AB-P14 = 4 '**C047**
答案 0 :(得分:1)
这样的事情会起作用:
Sub SwitchSub()
Dim rng as Range, cl as Range, CharsBeforeHyphen As String
Set rng = Worksheets("PAR_import").Range("M3:M100")
For each cl in rng
CharsBeforeHyphen = VBA.Left$(cl, Application.WorksheetFunction.Find("-", cl, 1) - 1)
If VBA.Len(CharsBeforeHyphen) = 3 Then
Call SubX
ElseIf VBA.Len(CharsBeforeHyphen) = 4 Then
Call SubY
End If
next cl
End Sub
您可以根据需要修改代码以传入范围参考。
答案 1 :(得分:0)
您可以使用WorksheetFunction.Search
或WorksheetFunction.Find
Sub test()
Dim Cl As Range
For Each Cl In Sheets("PAR_import").[M3:M100]
If Cl.Text Like "*-*" Then
If WorksheetFunction.Search("-", Cl.Text) - 1 = 3 Then
Call SubX
ElseIf WorksheetFunction.Search("-", Cl.Text) - 1 = 4 Then
Call SubY
End If
End If
Next Cl
End Sub