Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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,我正在拼命尝试创建一个宏,它将允许我执行以下操作: 选择一个文本文件(每次都不同) 查看数据并确定关键短语的第一个实例,例如“u_Z_1_u:u” 然后提取该短语后面的数据 将其写入Excel 然后确定关键短语的下一个实例 提取数据 写入Excel 非常感谢您的帮助 我真的不知道怎么做。我的代码如下: Private Sub CommandButton1_Click() Dim myFile As String, text As String, textline As String, Day A

我正在拼命尝试创建一个宏,它将允许我执行以下操作:

  • 选择一个文本文件(每次都不同)
  • 查看数据并确定关键短语的第一个实例,例如“u_Z_1_u:u”
  • 然后提取该短语后面的数据
  • 将其写入Excel
  • 然后确定关键短语的下一个实例
  • 提取数据
  • 写入Excel
  • 非常感谢您的帮助

    我真的不知道怎么做。我的代码如下:

    Private Sub CommandButton1_Click()
    Dim myFile As String, text As String, textline As String, Day As String, Zno As String, NetSales As String, Cash As String, Card As String, HotDrinks As String, ColdDrinks As String, Sweets As String, Crisps As String, VAT As String, NextZ As String
    
    myFile = Application.GetOpenFilename()
    
    Dim bffr As String, p As Long
    bffr = (myFile)
    p = InStr(p + 1, bffr, "_ _Z_1_:_", vbTextCompare)
    Do While CBool(p)
    
    '_ _Z_1_:_ was found; process it based upon the starting position p
    
    'see if there are other occurrences of _ _Z_1_:_
    p = InStr(p + 1, bffr, "_ _Z_1_:_", vbTextCompare)
    Loop
    
    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
    Loop
    Close #1
    
    Day = InStr(text, "Welcome Bite")
    Zno = InStr(text, "_ _Z_1_:_")
    NetSales = InStr(text, "NET sales          ")
    Cash = InStr(text, "CASH in ")
    Card = InStr(text, "CREDIT in")
    HotDrinks = InStr(text, "HOT DRINKS         ")
    ColdDrinks = InStr(text, "COLD DRINKS       ")
    Sweets = InStr(text, "Sweets             ")
    Crisps = InStr(text, "Crisps             ")
    VAT = InStr(text, "** Fixed Totaliser Period 1 Totals Reset")
    NextZ = InStr(text, "_ _Z_1_:_")
    
    
    Range("A1").Value = Mid(text, Day + 19, 18)
    Range("A2").Value = Mid(text, Zno + 10, 8)
    Range("A3").Value = Mid(text, NetSales + 30, 7)
    Range("A4").Value = Mid(text, Cash + 30, 7)
    Range("A5").Value = Mid(text, Card + 30, 7)
    Range("A6").Value = Mid(text, HotDrinks + 30, 7)
    Range("A7").Value = Mid(text, ColdDrinks + 30, 7)
    Range("A8").Value = Mid(text, Sweets + 30, 7)
    Range("A9").Value = Mid(text, Crisps + 30, 7)
    Range("A10").Value = Mid(text, VAT - 9, 7)
    
    Range("B2").Value = Mid(text, p + 1, 8)
    
    
    End Sub
    

    只要每次都能选择不同的文件,就可以使用

    with application.filedialog(msoFileDialogOpen)
    'disallows the user from selecting more than one file at a time
    .allowmultiselect=false
    'shows the file open dialog box
    .show
    
     if .selecteditems.count=0 then
     'dismisses dialog box if no selection
     else
     strPath=.selecteditems(1)
     end if
     end with
    
    然后,您可以使用
    strPath
    从中读取文本,尽管我从文本文件中读取文本的方法是将文本直接写入工作簿中的隐藏工作表(在
    Do中使
    text=textline
    直到EOF(1)
    循环并添加
    [workbookname].sheet([sheetname]).cells(xrowx,1)=文本
    xrowx=xrowx+1
    之后)。这种格式的信息更易于管理,而不是试图从一个巨大的字符串中读取。试一试,看看这是否能让事情变得更容易