BalloutMoe
Board Regular
- Joined
- Jun 4, 2021
- Messages
- 137
- Office Version
- 365
- Platform
- Windows
Hello, I have this code that reads a Text file and puts some data in an array and writes it to the sheet. I am having issues with safety inspection and obd inspection arrays as sometimes the whole line is missing in the text file. I was wondering how can I handle the error mismatch when writing to the sheet. I tried the following but still no luck. this is where the error happens even without the if as its writing to write a blank. Can I have it write zero if its empty? Thank you
any help would be appreciated on how to handle this error
VBA Code:
If Not Not arrSI(1) = "" Then
' sh.Range("G" & lastR + 1).Resize(1, 1).Value = arrSI(1) 'Insp Safety Amount
' End If
any help would be appreciated on how to handle this error
VBA Code:
Sub extractDatafromTextFile()
Dim sh As Worksheet, txtFileName As String, lastR As Long, i As Long, j As Long, FF As Long
Dim arrTxt, arrPay1, arrPay2, arrSI, arrDF, arrOBD, arrAF, SI As Long, val1 As Double
todaysdate = ThisWorkbook.Worksheets("Sheet1").Range("C1").Value 'Date
sdayname = Format(todaysdate, "dddd") 'Reformat Date
'Checks if its a sunday
If sdayname = "Sunday" Then
MsgBox "The Date entered is a Sunday, the shop is closed! Please enter another Date and Click the button again!"
Exit Sub
End If
'Clear content and Set WorksheetName
Set sh = ThisWorkbook.Worksheets("Sheet1")
sh.Range("A7:T20000").ClearContents
'FileName
txtFileName = sh.Range("M1").Value
'StoreID and Date Combined
todaysdate2 = sh.Range("L1").Value & Format(todaysdate, "mmddyy")
'Array and open Textfile
arrTxt = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(txtFileName, 1).ReadAll, vbCrLf)
For i = 0 To UBound(arrTxt)
If InStr(arrTxt(i), todaysdate2) > 0 Then
Do While InStr(arrTxt(i + j), "END OF DAY") = 0
j = j + 1
If Left(arrTxt(i + j), 3) = "12 " Then 'Gets First Payment Type Row
arrPay1 = Split(WorksheetFunction.Trim(arrTxt(i + j)), " ")
End If
If Left(arrTxt(i + j), 4) = "13A " Then 'Gets Sales Tax/NetSales/Fleets
arrPay2 = Split(WorksheetFunction.Trim(arrTxt(i + j)), " ")
End If
If InStr(arrTxt(i + j), "SAFETY INSPECTION") > 0 Then 'Safety Inspection
SI = InStr(arrTxt(i + j), "SAFETY INSPECTION")
arrSI = Split(WorksheetFunction.Trim(Left(arrTxt(i + j), SI - 1)), " ")
val1 = arrSI(1):
arrSI(0) = "SAFETY INSPECTION": arrSI(1) = arrSI(2): arrSI(2) = val1
End If
If InStr(arrTxt(i + j), "OBD INSPECTIONS") > 0 Then 'OBD Inspection
SI = InStr(arrTxt(i + j), "OBD INSPECTIONS")
arrOBD = Split(WorksheetFunction.Trim(Left(arrTxt(i + j), SI - 1)), " ")
val1 = arrOBD(1):
arrOBD(0) = "OBD INSPECTIONS": arrOBD(1) = arrOBD(2): arrOBD(2) = val1
End If
If InStr(arrTxt(i + j), "DISPOSAL FEE") > 0 Then 'Total Cars
SI = InStr(arrTxt(i + j), "DISPOSAL FEE")
arrDF = Split(WorksheetFunction.Trim(Left(arrTxt(i + j), SI - 1)), " ")
val1 = arrDF(1):
arrDF(0) = "DISPOSAL FEE": arrDF(1) = arrDF(2): arrDF(2) = val1
End If
Loop 'For Do
End If 'If Instr
Next i
'Get Last row
lastR = sh.Range("A" & sh.Rows.Count).End(xlUp).Row
sh.Range("B" & lastR + 1).Resize(1, 1).Value = arrSI(2) 'Safety Inspections Sold
sh.Range("B" & lastR + 1).Resize(1, 1).Value = sh.Range("B" & lastR + 1).Value + arrOBD(2) 'OBD Inspections Sold
sh.Range("C" & lastR + 1).Resize(1, 1).Value = arrPay2(13) 'SalesTax
sh.Range("D" & lastR + 1).Resize(1, 1).Value = arrPay1(2) 'Cash
sh.Range("E" & lastR + 1).Resize(1, 1).Value = arrPay1(3) 'Check
sh.Range("F" & lastR + 1).Resize(1, 1).Value = WorksheetFunction.Sum(arrPay1(4), arrPay1(5), arrPay1(6), arrPay1(9), arrPay1(13)) 'CreditCard Totals "MC,Visa,Amex,Discover,Debit"
'I am having issues here
sh.Range("G" & lastR + 1).Resize(1, 1).Value = arrSI(1) 'Insp Safety Amount
' and here
sh.Range("G" & lastR + 1).Resize(1, 1).Value = sh.Range("G" & lastR + 1).Value + arrOBD(1) 'Insp OBD Amount
MsgBox "Ready..."
End Sub