在文件,复制和粘贴单元格Excel VBA中查找文本

时间:2013-12-05 16:24:27

标签: excel vba excel-vba visual-studio-macros

我在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 

1 个答案:

答案 0 :(得分: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