Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA搜索文本文件中出现多次的字符串_Vba_Excel - Fatal编程技术网

使用VBA搜索文本文件中出现多次的字符串

使用VBA搜索文本文件中出现多次的字符串,vba,excel,Vba,Excel,我有一个文本文件,其内容如下: The breakdown of MMS submissions by interface is... | MM1 to MM1: 522245 messages (10.0% of submissions) | MM1 to MM3: 99360 messages (1.9% of submissions) | MM1 to MM4: 2393327 messages (46.0% of submissions) | MM3 to M

我有一个文本文件,其内容如下:

 The breakdown of MMS submissions by interface is... 
|    MM1 to MM1: 522245 messages (10.0% of submissions)
|    MM1 to MM3: 99360 messages (1.9% of submissions)
|    MM1 to MM4: 2393327 messages (46.0% of submissions)
|    MM3 to MM1: 14948 messages (0.3% of submissions)
|    MM4 to MM1: 2171419 messages (41.7% of submissions)
 ------------------
| The breakdown of MMS retrievals by interface is... 
|    MM1 to MM1: 2488980 messages (93.3% of retrievals)
|    MM3 to MM1: 11453 messages (0.4% of retrievals)
|    MM4 to MM1: 166323 messages (6.2% of retrievals)
我想获取值
522245
99360
2393327
14948
2171419
2488980
11453
166323
,并填充到另一张表中。
请提供您的输入,以执行相同的操作,因为我不知道搜索功能可以在文本文件上工作,就像在本例中一样。

如果您的数据在A列中,每个单元格都以“MM”开头,那么请尝试在B2中复制以适应:

=MID(A2,FIND(": ",A2)+2,FIND(" ",MID(A2,FIND(": ",A2)+2,10))-1)

对于VBA,首先打开宏记录器,必要时将文本导入Excel。选择ColumnB,复制,粘贴特殊值,并将结果复制到所需的表格中。

以下是您问题的具体解决方案:

Sub GetNumbers()
'1. grab text from txt file using VBA stream reader
Dim txtpath As String: txtpath = "d:\t.txt"
Open txtpath For Input As #1
'2. read stream into a string type
Dim str As String
Do Until EOF(1)
Line Input #1, txtLine
str = str & txtLine
Loop
Close #1
Dim regex As Object
'3. regex
Set regex = CreateObject("Vbscript.regexp")
 With regex
.IgnoreCase = True
.MultiLine = True
.Pattern = "\s(\d+)\s"
.Global = True
End With
'4. paste values in the active sheet
Dim i As Integer:  i = 1
If regex.test(str) Then
For Each Match In regex.Execute(str)
ActiveSheet.Range("A" & i) = Replace(Match," ","")
i = 1 + i
Next
End If
End Sub
结果:

一个可能的解决方案:

Sub ReadFromTextFile()
    Dim strFile As String: strFile = "C:\Users\Desktop\Tests\ReadFromText\ReadFile.txt"
    Dim strLine As String
    Dim strExtract As String
    Dim nLastRow As Long
    Dim nFnd As Long
    Dim nFndSpace As Long

    nLastRow = Cells(Rows.Count, 1).End(xlUp).Row

    Close #1
    Open strFile For Input As #1

    Do Until EOF(1)
        Line Input #1, strLine
        If InStr(1, strLine, ":") > 0 Then
            nFnd = (InStr(1, strLine, ":")) + 2
            nFndSpace = InStr(nFnd, strLine, " ") - nFnd
            Cells(nLastRow, 1).Value = Mid(strLine, nFnd, nFndSpace)
            nLastRow = nLastRow + 1
        End If
    Loop


End Sub

不,我也想获取这些值。我只给出了示例值。请让我知道如何使用正则表达式轻松地完成该操作。@JLILI Aman您能给出一个示例吗?我不知道如何搜索与MM1到MM1对应的值的第二个实例,因为InStr将MM1到MM1的第一个输入的位置返回给MM1。您能提供导入文本文件的代码吗导入excel。目前我正在浏览文本文件并直接处理它。是否有方法将文件内容导入excel