[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Dim[/COLOR] adoConn [COLOR=darkblue]As[/COLOR] ADODB.Connection
[COLOR=darkblue]Dim[/COLOR] adoRs [COLOR=darkblue]As[/COLOR] ADODB.Recordset
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] PopulateMastersheet()
[COLOR=darkblue]Dim[/COLOR] sPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] aFiles() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
[COLOR=darkblue]Dim[/COLOR] iFileCount [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
[COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=green]'==========================[/COLOR]
[COLOR=green]'Get file names to process[/COLOR]
[COLOR=green]'==========================[/COLOR]
sPath = "c:\temp2\"
[COLOR=darkblue]If[/COLOR] Dir(sPath, vbDirectory) = "" [COLOR=darkblue]Then[/COLOR]
MsgBox "Directory does not exist!"
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]Else[/COLOR]
[COLOR=green]'populate an array with filenames[/COLOR]
sFile = sPath & Dir(sPath & "*.xls")
[COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] sFile = ""
iFileCount = iFileCount + 1
[COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] aFiles(1 [COLOR=darkblue]To[/COLOR] iFileCount)
aFiles(iFileCount) = sFile
sFile = Dir()
[COLOR=darkblue]Loop[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'==========================[/COLOR]
[COLOR=green]'process the array of files[/COLOR]
[COLOR=green]'==========================[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] errCloseConnection:
[COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](aFiles) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](aFiles)
[COLOR=green]'open the connection[/COLOR]
[COLOR=darkblue]Set[/COLOR] adoConn = [COLOR=darkblue]New[/COLOR] ADODB.Connection
adoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & aFiles(i) & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
[COLOR=green]'open the recordset[/COLOR]
[COLOR=darkblue]Set[/COLOR] adoRs = [COLOR=darkblue]New[/COLOR] ADODB.Recordset
adoRs.Open "SELECT * FROM [Sheet1$];", adoConn, adOpenStatic, adLockReadOnly
[COLOR=green]'process the data[/COLOR]
[COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] (adoRs.BOF [COLOR=darkblue]Or[/COLOR] adoRs.EOF) [COLOR=darkblue]Then[/COLOR]
adoRs.MoveFirst
[COLOR=green]'output to worksheet[/COLOR]
[COLOR=darkblue]With[/COLOR] Sheets("Sheet1")
rw = .Range("A" & .Rows.Count).End(xlUp).Row + 1
[COLOR=darkblue]Do[/COLOR]
[COLOR=green]'NB sample data has four fields[/COLOR]
.Range("A" & rw) = adoRs.Fields(0) [COLOR=green]'1st field[/COLOR]
.Range("B" & rw) = adoRs.Fields(1) '2nd field
.Range("C" & rw) = adoRs.Fields(2) [COLOR=green]'3rd field[/COLOR]
.Range("D" & rw) = adoRs.Fields(3) '4th field
adoRs.MoveNext
rw = rw + 1
[COLOR=darkblue]Loop[/COLOR] [COLOR=darkblue]Until[/COLOR] adoRs.EOF
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
[COLOR=green]' Tidy up[/COLOR]
exitCloseConnection:
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
adoRs.Close
adoConn.Close
[COLOR=darkblue]Set[/COLOR] adoRs = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Set[/COLOR] adoConn = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
errCloseConnection:
MsgBox "Description: " & Err.Description & vbCrLf _
& "Error: " & Err.Number
[COLOR=darkblue]Resume[/COLOR] exitCloseConnection
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]