If you want to achieve this by VBA then there are plenty of option. However, I am trying to describe the cheapest solution here. Please follow the steps below
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 4/9/2009 by sarfaah
Dim str As String
'This line is added by me Sarfaraz
str = InputBox("Enter the file path")
'
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" _
& str & _
";Mode=Share Deny Write;Extend" _
, _
"ed Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine" _
, _
" Type=35;Jet OLEDB:Database 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:Don't Copy Locale on Co" _
, _
"mpact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("Sheet1$")
.Name = "TED Month End Reports"
.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
End Sub
The above code will be able to take care of your issue. I have added str variable which will ask you to enter the path of MS Excel file.
Regards,
Sarfaraz Ahmed
<a href="http://findsarfaraz.blogspot.com">MS Excel help</a>
Last Wiki Answer Submitted: April 9, 2009 8:09 am by Findsarfaraz1,395 pts.
All Answer Wiki Contributors: Findsarfaraz1,395 pts.
If you live outside the United States, by submitting your email address you consent to having your personal data transferred to and processed in the United States.
The above works great unless the Str file string is long. If I get to 140 or so characters (deeply buried file) than I get a type mismatch 13 error.
Any way around that?