Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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 读取.txt文件并将特定数据复制/粘贴到满足特定条件的Excel中_Vba_Excel_Vbscript - Fatal编程技术网

Vba 读取.txt文件并将特定数据复制/粘贴到满足特定条件的Excel中

Vba 读取.txt文件并将特定数据复制/粘贴到满足特定条件的Excel中,vba,excel,vbscript,Vba,Excel,Vbscript,文件sample.txt: something ------ -------- ------ xyz 12 34 56 78 90 10 11 12 1ds3 14 15 16 17 abc something ------ -------- ------ xyz 14 34 566 785 490 10 113 142 1ds3 143 155 616 17 abc 假设你不想读XYZ或ABC,那就来吧 Sub test1() Dim ReadEn As Boolean

文件
sample.txt

something ------ -------- ------ xyz 12 34 56 78 90 10 11 12 1ds3 14 15 16 17 abc something ------ -------- ------ xyz 14 34 566 785 490 10 113 142 1ds3 143 155 616 17 abc
假设你不想读XYZ或ABC,那就来吧

Sub test1()

Dim ReadEn As Boolean
Dim fso As FileSystemObject
Dim strMerge As String
Dim tStr As String
Set fso = New FileSystemObject
Set txtStream = fso.OpenTextFile("c:\projects\sample.txt", ForReading, False)

ReadEn = False
Do While txtStream.AtEndOfStream <> True

   tStr = txtStream.ReadLine

   If InStr(tStr, "abc") > 0 Then ReadEn = False

   ' do not read "xyz"
   If ReadEn Then
      strMerge = strMerge & tStr & Chr(13)
   End If

   If InStr(tStr, "xyz") > 0 Then ReadEn = True

Loop

MsgBox (strMerge)
txtStream.Close

End Sub
子测试1()
Dim ReadEn为布尔值
将fso设置为文件系统对象
作为字符串的Dim strMerge
Dim tStr As字符串
Set fso=新文件系统对象
设置txtStream=fso.OpenTextFile(“c:\projects\sample.txt”,ForReading,False)
ReadEn=False
当txtStream.AtEndOfStream为True时执行
tStr=txtStream.ReadLine
如果仪表(tStr,“abc”)>0,则ReadEn=False
'不读取“xyz”
如果ReadEn那么
strMerge=strMerge&tStr&Chr(13)
如果结束
如果InStr(tStr,“xyz”)>0,则ReadEn=True
环
MsgBox(strMerge)
txtStream,关闭
端接头
我打开ReadEn以启用将文件读取到
tStr
我不
ReadLine
,因为我可能在循环中通过EOF。
不确定是否也要读取空白,但它们将被读取并输出。如果您想输出
xyz
abc
sawp它们的If语句的位置。

假设您不想读取xyz或abc,现在开始

Sub test1()

Dim ReadEn As Boolean
Dim fso As FileSystemObject
Dim strMerge As String
Dim tStr As String
Set fso = New FileSystemObject
Set txtStream = fso.OpenTextFile("c:\projects\sample.txt", ForReading, False)

ReadEn = False
Do While txtStream.AtEndOfStream <> True

   tStr = txtStream.ReadLine

   If InStr(tStr, "abc") > 0 Then ReadEn = False

   ' do not read "xyz"
   If ReadEn Then
      strMerge = strMerge & tStr & Chr(13)
   End If

   If InStr(tStr, "xyz") > 0 Then ReadEn = True

Loop

MsgBox (strMerge)
txtStream.Close

End Sub
子测试1()
Dim ReadEn为布尔值
将fso设置为文件系统对象
作为字符串的Dim strMerge
Dim tStr As字符串
Set fso=新文件系统对象
设置txtStream=fso.OpenTextFile(“c:\projects\sample.txt”,ForReading,False)
ReadEn=False
当txtStream.AtEndOfStream为True时执行
tStr=txtStream.ReadLine
如果仪表(tStr,“abc”)>0,则ReadEn=False
'不读取“xyz”
如果ReadEn那么
strMerge=strMerge&tStr&Chr(13)
如果结束
如果InStr(tStr,“xyz”)>0,则ReadEn=True
环
MsgBox(strMerge)
txtStream,关闭
端接头
我打开ReadEn以启用将文件读取到
tStr
我不
ReadLine
,因为我可能在循环中通过EOF。 不确定是否也要读取空白,但它们将被读取并输出。如果要输出
xyz
abc
sawp其If语句的位置。

有:

  • a
    下一个i
    不在“范围内”

  • Readline()

试试这个

Option Explicit

Sub test1()
    Dim fso As FileSystemObject
    Dim strMerge As String
    Dim txtStream  As Variant
    Dim textLine As String

    Set fso = New FileSystemObject
    Set txtStream =    fso.OpenTextFile("filepath", ForReading, False)
    Do While txtStream.AtEndOfStream <> True
        textLine = txtStream.ReadLine
        If InStr(textLine, "xyz") <> 0 Then
            textLine = txtStream.ReadLine
            Do While Not InStr(textLine, "abc") <> 0
                strMerge = strMerge & textLine
                textLine = txtStream.ReadLine
           Loop
        End If
    Loop
    MsgBox strMerge
    txtStream.Close
End Sub
选项显式
子测试1()
将fso设置为文件系统对象
作为字符串的Dim strMerge
Dim txtStream作为变量
将文本行变暗为字符串
Set fso=新文件系统对象
设置txtStream=fso.OpenTextFile(“filepath”,ForReading,False)
当txtStream.AtEndOfStream为True时执行
textLine=txtStream.ReadLine
如果InStr(文本行,“xyz”)为0,则
textLine=txtStream.ReadLine
不安装时执行(文本行,“abc”)0
strMerge=strMerge&textLine
textLine=txtStream.ReadLine
环
如果结束
环
MsgBox strMerge
txtStream,关闭
端接头
当然,您必须将“filepath”更改为包含所需txt文件的实际完整路径的字符串,其中有:

  • a
    下一个i
    不在“范围内”

  • Readline()

试试这个

Option Explicit

Sub test1()
    Dim fso As FileSystemObject
    Dim strMerge As String
    Dim txtStream  As Variant
    Dim textLine As String

    Set fso = New FileSystemObject
    Set txtStream =    fso.OpenTextFile("filepath", ForReading, False)
    Do While txtStream.AtEndOfStream <> True
        textLine = txtStream.ReadLine
        If InStr(textLine, "xyz") <> 0 Then
            textLine = txtStream.ReadLine
            Do While Not InStr(textLine, "abc") <> 0
                strMerge = strMerge & textLine
                textLine = txtStream.ReadLine
           Loop
        End If
    Loop
    MsgBox strMerge
    txtStream.Close
End Sub
选项显式
子测试1()
将fso设置为文件系统对象
作为字符串的Dim strMerge
Dim txtStream作为变量
将文本行变暗为字符串
Set fso=新文件系统对象
设置txtStream=fso.OpenTextFile(“filepath”,ForReading,False)
当txtStream.AtEndOfStream为True时执行
textLine=txtStream.ReadLine
如果InStr(文本行,“xyz”)为0,则
textLine=txtStream.ReadLine
不安装时执行(文本行,“abc”)0
strMerge=strMerge&textLine
textLine=txtStream.ReadLine
环
如果结束
环
MsgBox strMerge
txtStream,关闭
端接头

当然,您必须使用正则表达式将“filepath”更改为包含所需txt文件的实际完整路径的字符串

Sub test1()
    Dim fso As FileSystemObject
    Dim strMerge As String
    Dim txtStream  As Variant
    Dim textLine As String

    Set fso = New FileSystemObject
    Set txtStream = fso.OpenTextFile("C:\Users\pankaj.jaju\Desktop\test.txt", ForReading, False)
    txt = txtStream.ReadAll

    Dim objRegEx, oRegResults
    Set objRegEx = CreateObject("VBScript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .MultiLine = True
        .Global = True
        .Pattern = "xyz\s*([\S\s]*?)\s*?\S*?abc"
    End With

    Set oRegResults = objRegEx.Execute(txt)
    For Each txtMatch In oRegResults
        MsgBox txtMatch.Submatches(0)
    Next
    txtStream.Close
End Sub

使用正则表达式的解决方案

Sub test1()
    Dim fso As FileSystemObject
    Dim strMerge As String
    Dim txtStream  As Variant
    Dim textLine As String

    Set fso = New FileSystemObject
    Set txtStream = fso.OpenTextFile("C:\Users\pankaj.jaju\Desktop\test.txt", ForReading, False)
    txt = txtStream.ReadAll

    Dim objRegEx, oRegResults
    Set objRegEx = CreateObject("VBScript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .MultiLine = True
        .Global = True
        .Pattern = "xyz\s*([\S\s]*?)\s*?\S*?abc"
    End With

    Set oRegResults = objRegEx.Execute(txt)
    For Each txtMatch In oRegResults
        MsgBox txtMatch.Submatches(0)
    Next
    txtStream.Close
End Sub

只需使用一些一次性物品和妥善放置的裂口

Sub test1()
    Dim ecA, ec(): ReDim ec(0)
    ecA = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\testenv\test.txt", 1).ReadAll, "xyz")
    For I = 1 To UBound(ecA): ec(UBound(ec)) = Split(ecA(I), "abc")(0): ReDim Preserve ec(UBound(ec) + 1): Next
    ReDim Preserve ec(UBound(ec) - 1)
End Sub

只需使用一些一次性物品和妥善放置的裂口

Sub test1()
    Dim ecA, ec(): ReDim ec(0)
    ecA = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\testenv\test.txt", 1).ReadAll, "xyz")
    For I = 1 To UBound(ecA): ec(UBound(ec)) = Split(ecA(I), "abc")(0): ReDim Preserve ec(UBound(ec) + 1): Next
    ReDim Preserve ec(UBound(ec) - 1)
End Sub

您好,行“strMerge=strMerge&txtStream.ReadLine”在两种情况下都会执行:“InStr(txtStream.ReadLine,“xyz”)0”是真是假。您的文本表示VBScript,但您的代码实际上是VBA,那么这两种语言是哪种?他们不一样。还有,你“尝试了以下”。。。然后呢?请尽可能准确地描述您希望代码做什么,以及实际行为与您期望的不同。包括您收到的所有错误消息。是否要将
xyz
abc
之间的部分作为单个字符串?阵列?完全是别的吗?一旦你有了数据,你想对它做什么?它应该被退回吗?(您需要一个函数而不是子函数)写入文件?您好,行“strMerge=strMerge&txtStream.ReadLine”在两种情况下都会执行:“InStr(txtStream.ReadLine,“xyz”)0”是真是假。您的文本显示VBScript,但您的代码实际上是VBA,那么这两种语言是哪一种?他们不一样。还有,你“尝试了以下”。。。然后呢?请尽可能准确地描述您希望代码做什么,以及实际行为与您期望的不同。包括您收到的所有错误消息。是否要将
xyz
abc
之间的部分作为单个字符串?阵列?完全是别的吗?一旦你有了数据,你想对它做什么?它应该被退回吗?(你需要一个函数而不是子函数)写入文件?@Nuke,你通过了吗?@Nuke,你通过了吗?