Skip to content

Commit

Permalink
Improved error message when file is over 2GB.
Browse files Browse the repository at this point in the history
Version	Date	Time	Author	Comment
241	02-Mar-2023	12:08	Philip Swannell	Tweaks to TestImpactOfTypeConversion.
240	02-Mar-2023	11:08	Philip Swannell	Added method TestImpactOfTypeConversion
239	02-Mar-2023	10:41	Philip Swannell	Improved error message when file is over 2GB.
  • Loading branch information
PGS62 committed Mar 2, 2023
1 parent 614ce44 commit 355c736
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 83 deletions.
3 changes: 3 additions & 0 deletions vba/VBA-CSV.xlsm/AuditSheetComments.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Version Date Time Author Comment
241 02-Mar-2023 12:08 Philip Swannell Tweaks to TestImpactOfTypeConversion.
240 02-Mar-2023 11:08 Philip Swannell Added method TestImpactOfTypeConversion
239 02-Mar-2023 10:41 Philip Swannell Improved error message when file is over 2GB.
238 01-Mar-2023 20:18 Philip Swannell Added two more tests - Test270 & Test271, testing "date format conflicts".
237 01-Mar-2023 11:01 Philip Swannell Refactor test code - now easy to run a subset of the tests.
236 28-Feb-2023 10:45 Philip Swannell Re-wrote method CastToDouble for better speed.
Expand Down
Binary file modified vba/VBA-CSV.xlsm/VBA-CSV.xlsm
Binary file not shown.
5 changes: 2 additions & 3 deletions vba/VBA-CSV.xlsm/modCSVPerformance.bas
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Attribute VB_Name = "modCSVPerformance"

' VBA-CSV

' Copyright (C) 2021 - Philip Swannell (https://github.com/PGS62/VBA-CSV )
Expand Down Expand Up @@ -108,7 +107,7 @@ Private Sub RunSpeedTests()
For Each N In ws.Names
If InStr(N.Name, "PasteResultsHere") > 1 Then
If NameRefersToRange(N) Then
Application.GoTo N.RefersToRange
Application.Goto N.RefersToRange
For Each c In N.RefersToRange.Cells
c.Resize(1, NumColsInTFPRet).ClearContents
TestResults = ThrowIfError(TimeParsers(ws.Range("ParserNames").value, c.Offset(0, -3).value, c.Offset(0, -2).value, _
Expand Down Expand Up @@ -330,7 +329,7 @@ Sub AddCharts(Optional Export As Boolean = True)
End If
Next N

Application.GoTo ws.Cells(1, 1)
Application.Goto ws.Cells(1, 1)
ws.Protect , , prot

Exit Sub
Expand Down
143 changes: 73 additions & 70 deletions vba/VBA-CSV.xlsm/modCSVPerformanceLowLevel.bas
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Private Sub SpeedTest_CastToDate()
Debug.Print "'SysDateSeparator = " & SysDateSeparator
Debug.Print "'N = " & Format$(N, "###,###")
Debug.Print "'ComputerName = " & Environ("ComputerName")
Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value

For k = 1 To 12
For j = 1 To 1 'Maybe do multiple times to test for variability or results.
Expand Down Expand Up @@ -202,7 +203,7 @@ Sub SpeedTestCDateVDateSerial()
1 Debug.Print String(80, "-")
2 Debug.Print "Time: ", Now
3 Debug.Print "ComputerName: ", Environ$("ComputerName")
4 Debug.Print "VersionNumber:", shAudit.Range("B6").value
4 Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value

5 tic
6 For i = 1 To N
Expand Down Expand Up @@ -354,6 +355,7 @@ Private Sub SpeedTest_CastISO8601()
Debug.Print "'Running SpeedTest_CastISO8601 " & Format$(Now(), "yyyy-mmm-dd hh:mm:ss")
Debug.Print "'N = " & Format$(N, "###,###")
Debug.Print "'ComputerName = " & Environ("ComputerName")
Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value

For k = 0 To 9
For j = 1 To 1
Expand Down Expand Up @@ -462,79 +464,80 @@ Private Sub SpeedTest_CastToDouble()
2 Debug.Print "'Running SpeedTest_CastToDouble " & Format$(Now(), "yyyy-mmm-dd hh:mm:ss")
3 Debug.Print "'N = " & Format$(N, "###,###")
4 Debug.Print "'ComputerName = " & Environ("ComputerName")
5 SysDecimalSeparator = Application.DecimalSeparator
5 Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value
6 SysDecimalSeparator = Application.DecimalSeparator

6 For k = 1 To 8
7 For j = 1 To 1
8 DblOut = 0
9 Converted = False
10 Select Case k
7 For k = 1 To 8
8 For j = 1 To 1
9 DblOut = 0
10 Converted = False
11 Select Case k
Case 1
11 strIn = "a random string"
12 DecimalSeparator = "."
13 SepStandard = DecimalSeparator = SysDecimalSeparator
14 AscSeparator = Asc(DecimalSeparator)
15 Expected = CDbl(0)
16 Case 2
17 strIn = "1"
18 DecimalSeparator = "."
19 SepStandard = DecimalSeparator = SysDecimalSeparator
20 AscSeparator = Asc(DecimalSeparator)
21 Expected = CDbl(1)
22 Case 3
23 strIn = "9"
24 DecimalSeparator = "."
25 SepStandard = DecimalSeparator = SysDecimalSeparator
26 AscSeparator = Asc(DecimalSeparator)
27 Expected = CDbl(9)
28 Case 4
29 strIn = "-4"
30 DecimalSeparator = "."
31 SepStandard = DecimalSeparator = SysDecimalSeparator
32 AscSeparator = Asc(DecimalSeparator)
33 Expected = CDbl(-4)
34 Case 5
35 strIn = "1e6"
36 DecimalSeparator = "."
37 SepStandard = DecimalSeparator = SysDecimalSeparator
38 AscSeparator = Asc(DecimalSeparator)
39 Expected = CDbl(1000000)
40 Case 6
41 strIn = ".5"
42 DecimalSeparator = "."
43 SepStandard = DecimalSeparator = SysDecimalSeparator
44 AscSeparator = Asc(DecimalSeparator)
45 Expected = 0.5
46 Case 7
47 strIn = "123,4"
48 DecimalSeparator = ","
49 SepStandard = DecimalSeparator = SysDecimalSeparator
50 AscSeparator = Asc(DecimalSeparator)
51 Expected = 123.4
52 Case 8
53 strIn = ",4"
54 DecimalSeparator = ","
55 SepStandard = DecimalSeparator = SysDecimalSeparator
56 AscSeparator = Asc(DecimalSeparator)
57 Expected = 0.4
58 End Select

59 t1 = ElapsedTime()
60 For i = 1 To N
61 CastToDouble strIn, DblOut, SepStandard, DecimalSeparator, AscSeparator, SysDecimalSeparator, Converted
62 Next i
63 t2 = ElapsedTime()
12 strIn = "a random string"
13 DecimalSeparator = "."
14 SepStandard = DecimalSeparator = SysDecimalSeparator
15 AscSeparator = Asc(DecimalSeparator)
16 Expected = CDbl(0)
17 Case 2
18 strIn = "1"
19 DecimalSeparator = "."
20 SepStandard = DecimalSeparator = SysDecimalSeparator
21 AscSeparator = Asc(DecimalSeparator)
22 Expected = CDbl(1)
23 Case 3
24 strIn = "9"
25 DecimalSeparator = "."
26 SepStandard = DecimalSeparator = SysDecimalSeparator
27 AscSeparator = Asc(DecimalSeparator)
28 Expected = CDbl(9)
29 Case 4
30 strIn = "-4"
31 DecimalSeparator = "."
32 SepStandard = DecimalSeparator = SysDecimalSeparator
33 AscSeparator = Asc(DecimalSeparator)
34 Expected = CDbl(-4)
35 Case 5
36 strIn = "1e6"
37 DecimalSeparator = "."
38 SepStandard = DecimalSeparator = SysDecimalSeparator
39 AscSeparator = Asc(DecimalSeparator)
40 Expected = CDbl(1000000)
41 Case 6
42 strIn = ".5"
43 DecimalSeparator = "."
44 SepStandard = DecimalSeparator = SysDecimalSeparator
45 AscSeparator = Asc(DecimalSeparator)
46 Expected = 0.5
47 Case 7
48 strIn = "123,4"
49 DecimalSeparator = ","
50 SepStandard = DecimalSeparator = SysDecimalSeparator
51 AscSeparator = Asc(DecimalSeparator)
52 Expected = 123.4
53 Case 8
54 strIn = ",4"
55 DecimalSeparator = ","
56 SepStandard = DecimalSeparator = SysDecimalSeparator
57 AscSeparator = Asc(DecimalSeparator)
58 Expected = 0.4
59 End Select

60 t1 = ElapsedTime()
61 For i = 1 To N
62 CastToDouble strIn, DblOut, SepStandard, DecimalSeparator, AscSeparator, SysDecimalSeparator, Converted
63 Next i
64 t2 = ElapsedTime()

Dim PrintThis As String
64 PrintThis = "'Calls per second = " & Format$(N / (t2 - t1), "###,###")
65 If Len(PrintThis) < 30 Then PrintThis = PrintThis & String(30 - Len(PrintThis), " ")
66 PrintThis = PrintThis & " strIn = """ & strIn & """, Separator = " & """" & DecimalSeparator & """ "
67 If Len(PrintThis) < 65 Then PrintThis = PrintThis & String(65 - Len(PrintThis), " ")
68 PrintThis = PrintThis & "Result as expected? " & (Expected = DblOut)
65 PrintThis = "'Calls per second = " & Format$(N / (t2 - t1), "###,###")
66 If Len(PrintThis) < 30 Then PrintThis = PrintThis & String(30 - Len(PrintThis), " ")
67 PrintThis = PrintThis & " strIn = """ & strIn & """, Separator = " & """" & DecimalSeparator & """ "
68 If Len(PrintThis) < 65 Then PrintThis = PrintThis & String(65 - Len(PrintThis), " ")
69 PrintThis = PrintThis & "Result as expected? " & (Expected = DblOut)

69 Debug.Print PrintThis
70 DoEvents 'kick Immediate window to life
71 Next j
72 Next k
70 Debug.Print PrintThis
71 DoEvents 'kick Immediate window to life
72 Next j
73 Next k
End Sub

22 changes: 16 additions & 6 deletions vba/VBA-CSV.xlsm/modCSVReadWrite.bas
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Attribute VB_Name = "modCSVReadWrite"
' Copyright (C) 2021 - Philip Swannell
' License MIT (https://opensource.org/licenses/MIT)
' Document: https://github.com/PGS62/VBA-CSV#readme
' This version at: https://github.com/PGS62/VBA-CSV/releases/tag/v0.27
' This version at: https://github.com/PGS62/VBA-CSV/releases/tag/v0.28

'Installation:
'1) Import this module into your project (Open VBA Editor, Alt + F11; File > Import File).
Expand All @@ -25,7 +25,7 @@ Attribute VB_Name = "modCSVReadWrite"
'4) An alternative (or additional) approach to providing help on CSVRead and CSVWrite is:
' a) Install Excel-DNA Intellisense. See https://github.com/Excel-DNA/IntelliSense#getting-started
' b) Copy the worksheet _Intellisense_ from
' https://github.com/PGS62/VBA-CSV/releases/download/v0.27/VBA-CSV-Intellisense.xlsx
' https://github.com/PGS62/VBA-CSV/releases/download/v0.28/VBA-CSV-Intellisense.xlsx
' into the workbook that contains this VBA code.

'5) If you envisage calling CSVRead and CSVWrite only from VBA code and not from worksheet formulas
Expand Down Expand Up @@ -3555,13 +3555,23 @@ Private Function ParseDownloadError(ErrNum As Long) As String
End Function

Private Function GetFileSize(FilePath As String)
1 On Error GoTo ErrHandler

1 On Error GoTo ErrHandler1
2 If m_FSO Is Nothing Then Set m_FSO = New Scripting.FileSystemObject
3 GetFileSize = m_FSO.GetFile(FilePath).Size

4 Exit Function
ErrHandler:
5 Throw "Could not find file '" & FilePath & "'"
4 On Error GoTo ErrHandler2
5 If GetFileSize >= (2 ^ 31) Then
6 Throw "File is too large. It is " & Format(GetFileSize, "###,###") & _
" bytes, which exceeds the maximum allowed size of " & Format((2 ^ 31) - 1, "###,###")
7 End If
8 Exit Function

ErrHandler1:
9 Throw "Could not find file '" & FilePath & "'"

ErrHandler2:
10 ReThrow "GetFileSize", Err
End Function

' -----------------------------------------------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions vba/VBA-CSV.xlsm/modCSVTest.bas
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ Private m_NumFailed As Long
Private m_NumSkipped As Long
Private m_Failures() As String

Sub SwitchAllTrue()
SwitchAllTests True
End Sub

Sub SwitchAllFalse()
SwitchAllTests False
End Sub

Sub SwitchAllTests(NewValue As Boolean)
On Error GoTo ErrHandler
shTest.ListObjects("Tests").ListColumns("RunThisTest").DataBodyRange.value = NewValue
Expand Down
1 change: 0 additions & 1 deletion vba/VBA-CSV.xlsm/modCSVTestDeps.bas
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Attribute VB_Name = "modCSVTestDeps"

' VBA-CSV

' Copyright (C) 2021 - Philip Swannell (https://github.com/PGS62/VBA-CSV )
Expand Down
42 changes: 41 additions & 1 deletion vba/VBA-CSV.xlsm/modCSVTestRDatasets.bas
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Sub TestAgainstLargestFileInRDatasets()
Dim WhatDiffers As String

t1 = ElapsedTime
res1 = CSVRead(FileName)
res1 = CSVRead(FileName, False)
t2 = ElapsedTime
' res2 = ArrayFromCSVfile(FileName, , , RemoveQuotes)
t3 = ElapsedTime
Expand All @@ -48,6 +48,46 @@ Sub TestAgainstLargestFileInRDatasets()

End Sub

'Time for type conversion is trivial (at least for the test file in quesiton)
'====================================================================================================
'Running TestImpactOfTypeConversion 2023-Mar-02 11:19:34
'NumTrials = 20
'FileName = C:\Projects\RDatasets\csv\openintro\military.csv
'ComputerName = DESKTOP-HSGAM5S
'VBA-CSV Audit Sheet Version = 240
'Elapsed time for CSVRead(FileName, False): 9.53321450500225
'Elapsed time for CSVRead(FileName, True): 9.57358807499986

Sub TestImpactOfTypeConversion()
Const FileName As String = "C:\Projects\RDatasets\csv\openintro\military.csv"
Dim res1, res2, t1 As Double, t2 As Double, i As Long

Const NumTrials = 20

Debug.Print "'" & String(100, "=")
Debug.Print "'Running TestImpactOfTypeConversion " & Format$(Now(), "yyyy-mmm-dd hh:mm:ss")
Debug.Print "'NumTrials = " & CStr(NumTrials)
Debug.Print "'FileName = " & FileName
Debug.Print "'ComputerName = " & Environ("ComputerName")
Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value

t1 = ElapsedTime()
For i = 1 To NumTrials
res1 = CSVRead(FileName, False)
Next i
t2 = ElapsedTime()
Debug.Print "'Elapsed time for CSVRead(FileName, False): " & CStr((t2 - t1) / NumTrials)

t1 = ElapsedTime()
For i = 1 To NumTrials
res1 = CSVRead(FileName, True)
Next i
t2 = ElapsedTime()
Debug.Print "'Elapsed time for CSVRead(FileName, True): " & CStr((t2 - t1) / NumTrials)

End Sub


' -----------------------------------------------------------------------------------------------------------------------
' Procedure : TestAgainstRDatasets
' Purpose : Test against the 20 largest files in Rdatasets https://github.com/vincentarelbundock/Rdatasets
Expand Down
2 changes: 1 addition & 1 deletion vba/VBA-CSV.xlsm/modCSVWritePerformance.bas
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Sub TestWriteSpeedUsingMilitary()
2 Debug.Print String(80, "-")
3 Debug.Print "Time: ", Now
4 Debug.Print "ComputerName: ", Environ$("ComputerName")
5 Debug.Print "VersionNumber:", shAudit.Range("B6").value
5 Debug.Print "'VBA-CSV Audit Sheet Version = " & shAudit.Range("Headers").Cells(2, 1).value
6 tic
7 Data = ThrowIfError(CSVRead("C:\Projects\RDatasets\csv\openintro\military.csv", True))
8 toc "CSVRead"
Expand Down
2 changes: 1 addition & 1 deletion vba/VBA-CSV.xlsm/shTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean
Set CSVReadRes = Me.Range("CallToCSVRead")
End If

Application.GoTo Me.Cells(1, 1)
Application.Goto Me.Cells(1, 1)
Me.Range("TheVBACode").Select

protContents = Me.ProtectContents
Expand Down
Binary file modified workbooks/VBA-CSV.xlsm
Binary file not shown.

0 comments on commit 355c736

Please sign in to comment.