我写了一些VBA代码来做几件事: 1)。检索3个范围:字符串范围,输出范围,子字符串(查找)范围 2)。遍历“字符串”范围以找到子字符串列表之一 3)。将发现的子字符串添加到输出范围
我的代码将仅对子字符串范围内的第一个单元格执行这些操作。我是否不允许嵌套每个循环?
Sub ExtrSubString()
Dim sRg As Range
Dim sDRg As Range
Dim sRRg As Range
Dim sFRg As Range
Dim ssF1Rg As Range
Dim cCellLength As Integer
Dim cFindLength As Integer
Dim cNumber As Integer
Dim strList As String
Dim sTitleId As String
Dim nI As Integer
sTitleId = "Substring Extraction"
Set sDRg = Application.InputBox("Please select text strings:",xTitleId,"",Type:=8)
If TypeName(sDRg) = "Nothing" Then Exit Sub
Set sRRg = Application.InputBox("Please select output cell:",Type:=8)
If TypeName(sRRg) = "Nothing" Then Exit Sub
Set sFRg = Application.InputBox("Please select substring cell:",Type:=8)
If TypeName(sFRg) = "Nothing" Then Exit Sub
sI = 0
strNumber = ""
For Each sRg In sDRg
nI = nI + 1
For Each ssF1Rg In sFRg
cCellLength = Len(sRg)
cFindLength = Len(ssF1Rg)
For cNumber = 1 To cCellLength
If ssF1Rg = (Mid(sRg,cNumber,cFindLength)) Then
strList = (Mid(sRg,cFindLength))
End If
Next cNumber
Next ssF1Rg
sRRg.Item(nI) = strList
strList = ""
Next sRg
End Sub