I am desperately trying to create a macro which will allow me to do the following:
- Select a text file (different one everytime)
- Look at the data and identify the first instance of key phrases such as "_ _Z_1_:_"
- Then extract data following that phrase
- Write it to Excel
- Then identify the next instance of the key phrase
- Extract data
- Write to Excel
Any help much appreciated.
I'm really not sure how to do it. My code is as follows:
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
Aucun commentaire:
Enregistrer un commentaire