LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Fastest way to sort large 2-D arrays?

OK, I include the missing dependencies, so you can test.
The reason I use text files is that people that use my software sometimes
have no Access. The other thing is that it surprisingly appears just as
fast.
If you can convince me that Access is faster I might have another look
at using that.


Function OpenTextFileToArray(ByVal txtFile As String, _
ByRef arr As Variant, _
ByVal LBRow As Long, _
ByVal UBRow As Long, _
ByVal LBCol As Long, _
ByVal UBCol As Long, _
Optional ByVal bSkipFields As Boolean = False)
As Variant

Dim hFile As Long
Dim r As Long
Dim c As Long
Dim varWaste

hFile = FreeFile

Open txtFile For Input As #hFile

On Error Resume Next

If bSkipFields = False Then
For r = LBRow To UBRow
For c = LBCol To UBCol
Input #hFile, arr(r, c)
Next
Next
Else
For c = LBCol To UBCol
Input #hFile, varWaste
Next
For r = LBRow To UBRow
For c = LBCol To UBCol
Input #hFile, arr(r, c)
Next
Next
End If

Close #hFile

OpenTextFileToArray = arr

End Function


Sub SaveArrayToText2(ByVal txtFile As String, _
ByRef arr As Variant, _
Optional ByVal LBRow As Long = -1, _
Optional ByVal UBRow As Long = -1, _
Optional ByVal LBCol As Long = -1, _
Optional ByVal UBCol As Long = -1, _
Optional ByRef fieldArr As Variant)

'this one organises the text file like
'a table by inserting the right line breaks
'------------------------------------------
Dim r As Long
Dim c As Long
Dim hFile As Long

If LBRow = -1 Then
LBRow = LBound(arr, 1)
End If

If UBRow = -1 Then
UBRow = UBound(arr, 1)
End If

If LBCol = -1 Then
LBCol = LBound(arr, 2)
End If

If UBCol = -1 Then
UBCol = UBound(arr, 2)
End If

hFile = FreeFile

Open txtFile For Output As hFile

If IsMissing(fieldArr) Then
For r = LBRow To UBRow
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, arr(r, c)
Else
Write #hFile, arr(r, c);
End If
Next
Next
Else
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, fieldArr(c)
Else
Write #hFile, fieldArr(c);
End If
Next
For r = LBRow To UBRow
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, arr(r, c)
Else
Write #hFile, arr(r, c);
End If
Next
Next
End If

Close #hFile

End Sub


Sub InsertLineAtBeginningTexFile(ByVal strFile As String, ByVal strLine As
String)

Dim FFile As Integer
Dim FileContents As String
Dim NewString As String

FFile = FreeFile
Open strFile For Binary As #FFile
FileContents = Space(FileLen(strFile))
Get #FFile, , FileContents
Close #FFile

NewString = strLine & vbCrLf
FileContents = NewString & FileContents

Open strFile For Binary As #FFile
Put #FFile, , FileContents
Close #FFile

End Sub


Function GetFieldsFromText(ByVal strFile As String, ByVal lCols As Long) As
String

Dim hFile As Long
Dim strTemp As String
Dim strResult As String
Dim c As Long

hFile = FreeFile

Open strFile For Input As #hFile

On Error Resume Next

For c = 1 To lCols
Input #hFile, strTemp
If c = 1 Then
strResult = strTemp
Else
strResult = strResult & ", " & strTemp
End If
Next

Close #hFile

GetFieldsFromText = strResult

End Function



RBS



"onedaywhen" wrote in message
oups.com...

RB Smissaert wrote:
You could try loading your data into an ADO recordset and sorting

it
there.
Google for "disconnected recordset" and ADO


Thanks, but I have tried that already and it turned out to be about

twice
as slow.


To be fair, you are not doing as Tim suggested in your code, which has
too many missing dependencies (e.g. sub procedure SaveArrayToText2) to
test <g.

Rather than fabricating a recordset, your code appears to fabricate a
text file. FWIW I don't think your recordset ever gets opened/populated
because the SELECT..INTO..FROM syntax does not return a rowset.

BTW do you have a business requirement to use text files as databases?
Fabricating an .mdb file would yield better performance but I still
have your PATIENT.txt and ENTRY.txt files from way back when to create
an example JOIN between text files.

Jamie.

--




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Large Arrays Hilton Excel Discussion (Misc queries) 2 September 30th 08 07:25 PM
Fastest way to select large range (e.g. B3:F1002)? [email protected] Excel Discussion (Misc queries) 7 August 31st 07 04:36 PM
Sort a large file then sum like values... jgray Excel Discussion (Misc queries) 2 August 1st 05 09:22 PM
How to sort/update large excel db [email protected] Excel Discussion (Misc queries) 0 February 2nd 05 12:43 AM
Need FASTEST way to get data from a large closed Excel File Dave B[_5_] Excel Programming 13 October 29th 03 09:26 PM


All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"