我正在拼命尝试创建一个允许我执行以下操作的宏:
任何帮助都非常感激。
我真的不确定该怎么做。我的代码如下:
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
答案 0 :(得分:0)
每次都可以选择不同的文件,你可以使用
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
来阅读文本,尽管我从文本文件中完成阅读的方式是将文本写入工作簿中的隐藏工作表(make text=textline
在Do Until EOF(1)
循环中,然后添加[workbookname].sheet([sheetname]).cells(xrowx,1)=text
和xrowx=xrowx+1
。这种格式的信息更易于管理,而不是试图从一个巨大的字符串中读取。给它一个旋转,看看它是否让你更容易。