Have Fun - any questions just post ! One thing that this does really well is to access SHARED excel Workbooks without adding extra users.Dont't forget the reference to ADO
Sub GetDataFromExcel()
'GET DATA FROM NAMED WORKBOOK OVEN RANGE
GetDataFromClosedWorkbook "\\Pc5525\C:\ Test.xls", "NamedRange", Range("B5"), False
End Sub
Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String, _
TargetRange As Range, IncludeFieldNames As Boolean)
' requires a reference to the Microsoft ActiveX Data Objects library
' if SourceRange is a range reference:
' this will return data from the first worksheet in SourceFile
' if SourceRange is a defined name reference:
' this will return data from any worksheet in SourceFile
' SourceRange must include the range headers
'
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
dbConnection.Mode = adModeRead ' read only connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' open the database connection
'Check to see if the target Workbook
On Error Resume Next
Workbooks(Filename).Activate
ErrorNr = Err.Number
If Err.Number = 0 Then Workbooks(Filename).Close False
Err.Number = 0
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' close the database connection
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file cannot be accessed!", _
vbExclamation, "Get data from Excel workbook"
End Sub