Microsoft Office Tutorials and References
In Depth Information
Table 11-1: Sorting Times (in Seconds) for Four Sort Algorithms Using Randomly Filled Arrays
Excel Worksheet Sort
VBA Bubble Sort
VBA Quick Sort
VBA Counting Sort
The worksheet sort algorithm is amazingly fast, especially when you consider that the values are
transferred to the sheet, sorted, and then transferred back to the array.
The bubble sort algorithm is reasonably fast with small arrays, but for larger arrays (more than
10,000 elements), forget it. The quick sort and counting sort algorithms are blazingly fast, but
they’re limited to Integer and Long data types.
Processing a series of files
One common use for macros is to perform repetitive tasks. The example in this section
demonstrates how to execute a macro on several different files stored on disk. This example — which
may help you set up your own routine for this type of task — prompts the user for a file
specification and then processes all matching files. In this case, processing consists of importing the file
and entering a series of summary formulas that describe the data in the file.
Dim FileSpec As String
Dim i As Integer
Dim FileName As String
Dim FileList() As String
Dim FoundFiles As Integer
‘ Specify path and file spec
FileSpec = ThisWorkbook.Path & “\” & “text??.txt”
FileName = Dir(FileSpec)
‘ Was a file found?
If FileName <> “” Then
FoundFiles = 1
ReDim Preserve FileList(1 To FoundFiles)
FileList(FoundFiles) = FileName
MsgBox “No files were found that match “ & FileSpec