Excel VBA 中的 InStr 函数可以使用通配符或正则表达式吗?
Can word wildcards or regular expressions be used inside InStr function in Excel VBA?
我这里有这段代码,它为整个文档内容设置了一个范围,并在该范围内搜索文本 = "cenę brutto w kwocie " - 我希望它在那里。
该代码的目的是从 word 文档中查找并提取公寓价格并将其写入 excel 单元格。
我的问题与第 11 行代码有关 endPos = InStr(startPos, rng, ",00zł")
:如何将“,00zł”更改为“[0-9]{3},[0-9]{2}zł”?
我知道这些通配符可以与range.find
方法一起使用,但是这些通配符可以传递到InStr
函数中吗??这些字符串 "cenę brutto w kwocie " 和 ",00zł" 在非常长的 word 文档中彼此非常接近。它们用于将 rng
范围从最初的整个文档缩小到可以找到和检索公寓价格的非常小的范围。我需要更改它,因为有时公寓价格中有货币小数。
'Assigning object variables and values
Set wordApp = GetObject(, "Word.Application")
Set excelApp = GetObject(, "Excel.Application")
Set wordDoc = wordApp.ActiveDocument
Set mySheet = Application.ActiveWorkbook.ActiveSheet
Set rng = wordApp.ActiveDocument.Content
Set scndRng = ActiveSheet.Range("A10:J40").Find("cena", , xlValues)
textToFind1 = "KRS 0000609737, REGON 364061169, NIP 951-24-09-783,"
textToFind2 = "- ad."
textToFind3 = "Tożsamość stawających"
textToFind4 = "PESEL"
TextToFind5 = "cenę brutto w kwocie łącznej"
textToFind6 = "cenę brutto w kwocie "
Set rng = wordApp.ActiveDocument.Content 'this command without "set" will destroy the document's formating;
With rng.Find
.Text = textToFind6 '="cenę brutto w kwocie "
.MatchWildcards = False
.MatchCase = False
.Forward = True
.Execute
If .Found = True Then
Set rng = wordApp.ActiveDocument.Content
startPos = InStr(1, rng, textToFind6) 'we're looking 4 "cenę brutto w kwocie"
endPos = InStr(startPos, rng, ",00zł") 'here we get 47380, we're looking 4 ",00zł"
startPos = startPos + Len(textToFind6) ' + 21 characters 'now start position is reassigned at 47331.
Debug.Print Replace(Mid(rng, startPos, endPos - startPos), ".", "")
price = Replace(Mid(rng, startPos, endPos - startPos), ".", "")
price = Trim(price)
Debug.Print price
End If
End With
Debug.Print scndRng.Address
scndRng.Offset(0, 1) = price
以前所有公寓价格都四舍五入为整数,因此找到“,00zł”并将其多头头寸传递给 endPos
变量。
如何将“[0-9]{3},[0-9]{2}zł”放入 InStr 或任何其他将 long 值传递到 endPos
变量的函数中。
不,您不能在 InStr 中使用正则表达式,在这种情况下 "wildcard-matching" VBA 运算符 "Like" 也无济于事。
您可以考虑使用多个单独的词查找,例如像这样的东西。这个假设如果你有
text_to_find_1
没有金额
text_to_find_1
amount_x
text_to_find_1
amount_y
然后您想为每个 text_to_find_1 找到一个数量(或 "no amount")。如果你不需要,你可以只使用两个范围和一个更简单的循环。
我必须在这里对重音字符使用 chrw()。
Sub getAmountsBetween2EqualTexts()
Dim rng1 As Word.Range
Dim rng2 As Word.Range
Dim rng3 As Word.Range
Dim findtext1 As String
Dim findtext2 As String
Dim wordApp As Word.Application
Set wordApp = Word.Application
findtext1 = "cen" & ChrW(281) & " brutto w kwocie "
findtext2 = "[0-9]{3},[0-9]{2}z" & ChrW(322)
Set rng1 = wordApp.ActiveDocument.Content
Set rng2 = wordApp.ActiveDocument.Content
Set rng3 = wordApp.ActiveDocument.Content
' Set up the middle and end finds
With rng2.Find
.ClearFormatting
.Text = findtext2
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
End With
With rng3.Find
.ClearFormatting
.Text = findtext1
.MatchWildcards = False
.Forward = True
.Wrap = wdFindStop
End With
With rng1.Find
.ClearFormatting
.Text = findtext1
.MatchWildcards = False
.Forward = True
.Wrap = wdFindStop
While .Execute
With rng3
.Start = rng1.End
.End = wordApp.ActiveDocument.Content.End
With rng2
If rng3.Find.Execute Then
.Start = rng1.End
.End = rng3.Start
Else
.Start = rng1.End
.End = wordApp.ActiveDocument.Content.End
End If
If .Find.Execute Then
' process these values as needed
Debug.Print .Text
Else
Debug.Print "Amount not found"
End If
End With
End With
Wend
End With
Set rng3 = Nothing
Set rng2 = Nothing
Set rng1 = Nothing
End Sub
我这里有这段代码,它为整个文档内容设置了一个范围,并在该范围内搜索文本 = "cenę brutto w kwocie " - 我希望它在那里。 该代码的目的是从 word 文档中查找并提取公寓价格并将其写入 excel 单元格。
我的问题与第 11 行代码有关 endPos = InStr(startPos, rng, ",00zł")
:如何将“,00zł”更改为“[0-9]{3},[0-9]{2}zł”?
我知道这些通配符可以与range.find
方法一起使用,但是这些通配符可以传递到InStr
函数中吗??这些字符串 "cenę brutto w kwocie " 和 ",00zł" 在非常长的 word 文档中彼此非常接近。它们用于将 rng
范围从最初的整个文档缩小到可以找到和检索公寓价格的非常小的范围。我需要更改它,因为有时公寓价格中有货币小数。
'Assigning object variables and values
Set wordApp = GetObject(, "Word.Application")
Set excelApp = GetObject(, "Excel.Application")
Set wordDoc = wordApp.ActiveDocument
Set mySheet = Application.ActiveWorkbook.ActiveSheet
Set rng = wordApp.ActiveDocument.Content
Set scndRng = ActiveSheet.Range("A10:J40").Find("cena", , xlValues)
textToFind1 = "KRS 0000609737, REGON 364061169, NIP 951-24-09-783,"
textToFind2 = "- ad."
textToFind3 = "Tożsamość stawających"
textToFind4 = "PESEL"
TextToFind5 = "cenę brutto w kwocie łącznej"
textToFind6 = "cenę brutto w kwocie "
Set rng = wordApp.ActiveDocument.Content 'this command without "set" will destroy the document's formating;
With rng.Find
.Text = textToFind6 '="cenę brutto w kwocie "
.MatchWildcards = False
.MatchCase = False
.Forward = True
.Execute
If .Found = True Then
Set rng = wordApp.ActiveDocument.Content
startPos = InStr(1, rng, textToFind6) 'we're looking 4 "cenę brutto w kwocie"
endPos = InStr(startPos, rng, ",00zł") 'here we get 47380, we're looking 4 ",00zł"
startPos = startPos + Len(textToFind6) ' + 21 characters 'now start position is reassigned at 47331.
Debug.Print Replace(Mid(rng, startPos, endPos - startPos), ".", "")
price = Replace(Mid(rng, startPos, endPos - startPos), ".", "")
price = Trim(price)
Debug.Print price
End If
End With
Debug.Print scndRng.Address
scndRng.Offset(0, 1) = price
以前所有公寓价格都四舍五入为整数,因此找到“,00zł”并将其多头头寸传递给 endPos
变量。
如何将“[0-9]{3},[0-9]{2}zł”放入 InStr 或任何其他将 long 值传递到 endPos
变量的函数中。
不,您不能在 InStr 中使用正则表达式,在这种情况下 "wildcard-matching" VBA 运算符 "Like" 也无济于事。
您可以考虑使用多个单独的词查找,例如像这样的东西。这个假设如果你有
text_to_find_1
没有金额
text_to_find_1
amount_x
text_to_find_1
amount_y
然后您想为每个 text_to_find_1 找到一个数量(或 "no amount")。如果你不需要,你可以只使用两个范围和一个更简单的循环。
我必须在这里对重音字符使用 chrw()。
Sub getAmountsBetween2EqualTexts()
Dim rng1 As Word.Range
Dim rng2 As Word.Range
Dim rng3 As Word.Range
Dim findtext1 As String
Dim findtext2 As String
Dim wordApp As Word.Application
Set wordApp = Word.Application
findtext1 = "cen" & ChrW(281) & " brutto w kwocie "
findtext2 = "[0-9]{3},[0-9]{2}z" & ChrW(322)
Set rng1 = wordApp.ActiveDocument.Content
Set rng2 = wordApp.ActiveDocument.Content
Set rng3 = wordApp.ActiveDocument.Content
' Set up the middle and end finds
With rng2.Find
.ClearFormatting
.Text = findtext2
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
End With
With rng3.Find
.ClearFormatting
.Text = findtext1
.MatchWildcards = False
.Forward = True
.Wrap = wdFindStop
End With
With rng1.Find
.ClearFormatting
.Text = findtext1
.MatchWildcards = False
.Forward = True
.Wrap = wdFindStop
While .Execute
With rng3
.Start = rng1.End
.End = wordApp.ActiveDocument.Content.End
With rng2
If rng3.Find.Execute Then
.Start = rng1.End
.End = rng3.Start
Else
.Start = rng1.End
.End = wordApp.ActiveDocument.Content.End
End If
If .Find.Execute Then
' process these values as needed
Debug.Print .Text
Else
Debug.Print "Amount not found"
End If
End With
End With
Wend
End With
Set rng3 = Nothing
Set rng2 = Nothing
Set rng1 = Nothing
End Sub