【问题标题】:Find text in File, Copy and Paste in Cells Excel VBA在文件中查找文本,在单元格中复制和粘贴 Excel VBA
【发布时间】:2013-12-05 16:24:26
【问题描述】:

我正在 VBA 中编写代码以查看文件中 unser 在宏中输入的字符,在宏找到用户提供的字符后,我希望它提取整行并将其粘贴到工作表中在 excel 中,通过获取第一个字符并将它们放在单元格 1a 中,然后获取下一组字符,这些字符由文本中的空格分隔并将它们放入另一个单元格中,这就是我到目前为止所拥有的并希望将第 17 行替换为数据的提取;

    Dim oFSO As Object
    Dim arrData() As String

    Sub test()
    Dim f As Integer
    Dim IngLine As Long
    Dim strLine As String
    Dim bInFound As Boolean
    f = FreeFile
    Tfile = "C:\TAXETI.TXT"
    staffdat = InputBox(Prompt:=" Please enter the staff number", Title:="Load Staff Data")
         Open Tfile For Input As #f
             Do While Not EOF(f)
                  IngLine = IngLine + 1
                  Line Input #f, strLine
                  If InStr(1, strLine, staffdat, vbBinaryCompare) > 0 Then
                     MsgBox "Search string found in line" & IngLine, vbInformation
                     bInFound = True
                     Exit Do
                  End If
             Loop
             Close #f
             If Not bInFound Then
             MsgBox "Search string not found", vbInformation
             End If
    End Sub 

【问题讨论】:

    标签: vba excel visual-studio-macros


    【解决方案1】:

    我建议你看看 Split() 函数。

    MSDN Reference

    Another example

    我认为您的代码将在 #17 中结束(您必须修剪/确定在何处“标记”您的行,具体取决于您的输入文件的样子):

    If InStr(1, strLine, staffdat, vbBinaryCompare) > 0 Then
     foundArr = Split(strLine, " ")'change " " to whatever fits your needs
     For i = LBound(foundArr) to UBound(foundArr)
        'enter each string into a cell, etc
     Next i
    

    【讨论】:

    • 没问题!似乎您已经解决了大部分问题。
    • 嘿 Max,如果 instr 函数找到我希望它向下 3 行并执行 mid() 函数的字符,我会用什么来做这个?
    • 嗨@AlfredoA.,当你找到你的角色时,你可以使用与Line Input #f, strLine行相同的方法在接下来的3行中“阅读” - 从而沿着你的方向走想要。但是请注意,您可能会发现您的角色在文件末尾不到 3 行,因此您可能需要针对这种情况进行错误捕获。此外,您可能想查看 FileSystemObject 的文件 I/O,我发现它更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多