Hello both,
After having searched for hours I finally found your thread with exactly what I needed. I'm no VBA expert as I'm not using it on a regular basis, but I can understand it fine. I tried to apply your solution here, also recording my own macro at some point, however it doesn't work and I've tried many ways. The answer must be simple but I just can't find it. I'd be really grateful if you could help me with it.
Here is my code ( part of a much longer one )
Private Sub Cmdyes_Click()
Dim PctDone As Single
'-------------------------------------TOTAL ELECTRIC CONSUMPTION--------------------
Dim sFileName As Variant
Dim Filter As String
Filter = "Excel Files(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;*.xlsb"
sFileName = Application.GetOpenFilename(filefilter:=Filter)
If sFileName = False Then
End 'user hit cancel
End If
'--------------------THAT IS THE ORIGINAL RECORDED MACRO, WHICH WORKS ALONE-------------
' With ActiveSheet.QueryTables.Add(Connection:=Array( _
' "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=""P:\Departmental Folders\FCM - Facilities Management\270_" _
, _
' "Interns folders\60_Pierre-Nils's Files\Energy Consumption Report\Total Consumption Data\Bosch - Warndon - HHD - 29.10.10 to 04.1" _
, _
' "1.10.XLS"";Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB" _
, _
"
atabase Password="""";Jet OLEDB:Engine Type=35;Jet OLEDB
atabase Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:G" _
, _
"lobal Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=F" _
, _
"alse;Jet OLEDB
on't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("A115"))
' .CommandType = xlCmdTable
' .CommandText = Array("'Sheet #1$'")
' .Name = "Bosch - Warndon - HHD - 29.10.10 to 04.11.10"
' .FieldNames = True
' .RowNumbers = False
' .FillAdjacentFormulas = False
' .PreserveFormatting = True
' .RefreshOnFileOpen = False
' .BackgroundQuery = True
' .RefreshStyle = xlInsertDeleteCells
' .SavePassword = False
' .SaveData = True
' .AdjustColumnWidth = True
' .RefreshPeriod = 0
' .PreserveColumnInfo = True
' .SourceDataFile = _
' "P:\Departmental Folders\FCM - Facilities Management\270_Interns folders\60_Pierre-Nils's Files\Energy Consumption Report\Total Consumption Data\Bosch - Warndon - HHD - 29.10.10 to 04.11.10.XLS"
' .Refresh BackgroundQuery:=False
End With
'--------------------THIS IS THE ONE I TRIED TO REPLACE WITH sFileName-------------
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" _
& sFileName & _
";Mode=Share Deny Write;Extend" _
, _
"ed Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB
atabase Password="""";Jet OLEDB:Engine" _
, _
" Type=35;Jet OLEDB
atabase Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Ne" _
, _
"w Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB
on't Copy Locale on Co" _
, _
"mpact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("D115"))
..CommandType = xlCmdTable
..CommandText = Array("Sheet1$")
..Name = "Total Consumption"
..FieldNames = True
..RowNumbers = False
..FillAdjacentFormulas = False
..PreserveFormatting = True
..RefreshOnFileOpen = False
..BackgroundQuery = True
..RefreshStyle = xlInsertDeleteCells
..SavePassword = False
..SaveData = True
..AdjustColumnWidth = True
..RefreshPeriod = 0
..PreserveColumnInfo = True
..SourceDataFile = "D:\TED Month End Reports.xls"
..Refresh BackgroundQuery:=False
End With
The "With....." line doesn't work. In debug mode "identifier under cursor not recognized" and the whole "With... thing" is yellow.
Thanks for your help,
Pierre