M
Markus Stolle
PROBLEM
I need to somehow calculate the name of the first sheet of a workboo
from which I get external data through the QueryTables.add method
BACKGROUND INFORMATION
I am receiving several reports that I need to consolidate.
They are from different sources, so I can't dictate how the sheets hav
to be formatted, named etc.
Therefore I am building a tool that allows me to "open" the sheets a
external data, and storing the information in a temoray sheet.
That temp sheet I then modify heavily to get the data in the shape
need.
CODE EXAMPLE
to get to my code I started by recording a macro, and luckily for me
the workbooks of type "raw" have their one and only sheet called jus
that: "RAW"
So all worked perfectly for my first 11 sample reports.
This is the Code that I used for the first set of Reports of Typ
"Raw".
Code
-------------------
'Variable list:
'FullNameOfReport: Filename complete with Path
'NameOfReport() is a function that returns only the filename
With Worksheets("Temp Import").QueryTables _
.Add(Connection:= _
Array("OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;" _
, "Password="""";" _
, "User ID=Admin;" _
, "Data Source=" & FullNameOfReport & ";" _
, "Mode=Share Deny Write;" _
, "Extended Properties=""HDR=YES;"";" _
, "Jet OLEDB:System database="""";" _
, "Jet OLEDB:Registry Path="""";" _
, "Jet OLEDBatabase Password="""";" _
, "Jet OLEDB:Engine Type=35;" _
, "Jet OLEDBatabase Locking Mode=0;" _
, "Jet OLEDB:Global Partial Bulk Ops=2;" _
, "Jet OLEDB:Global Bulk Transactions=1;" _
, "Jet OLEDB:New Database Password="""";" _
, "Jet OLEDB:Create System Database=False;" _
, "Jet OLEDB:Encrypt Database=False;" _
, "Jet OLEDBon't Copy Locale on Compact=False;" _
, "Jet OLEDB:Compact Without Replica Repair=False;" _
, "Jet OLEDB:SFP=False") _
, Destination:=Sheets("Temp Import").Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("RAW$")
.Name = NameOfReport(FullNameOfReport)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = FullNameOfReport
.Refresh BackgroundQuery:=False
End With
Worksheets("Temp Import").QueryTables(1).Delete
-------------------
My *BIG* Problem is that other reports unfortunately do not have th
consistant naming of their sheets. I now wanted to import Reports o
type "DIR" and their sheets are named anything but "DIR".
So my code fails at the *-.commandtext -* parameter wher
*-Array("RAW$")*- is still hard coded from when I created that cod
first while recording a Macro.
HELP NEEDE
How can I replace Array("RAW$") with a calculated expression tha
refers to the name of the first sheet (or better sheet x) in the sourc
workbook.
YOU KNOW A BETTER METHOD FOR WHAT I AM DOING
If you know a better method to import a worksheet in my temp sheet
without opening and showing the source workbook, let me know.
Many thanks in advanc
I need to somehow calculate the name of the first sheet of a workboo
from which I get external data through the QueryTables.add method
BACKGROUND INFORMATION
I am receiving several reports that I need to consolidate.
They are from different sources, so I can't dictate how the sheets hav
to be formatted, named etc.
Therefore I am building a tool that allows me to "open" the sheets a
external data, and storing the information in a temoray sheet.
That temp sheet I then modify heavily to get the data in the shape
need.
CODE EXAMPLE
to get to my code I started by recording a macro, and luckily for me
the workbooks of type "raw" have their one and only sheet called jus
that: "RAW"
So all worked perfectly for my first 11 sample reports.
This is the Code that I used for the first set of Reports of Typ
"Raw".
Code
-------------------
'Variable list:
'FullNameOfReport: Filename complete with Path
'NameOfReport() is a function that returns only the filename
With Worksheets("Temp Import").QueryTables _
.Add(Connection:= _
Array("OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;" _
, "Password="""";" _
, "User ID=Admin;" _
, "Data Source=" & FullNameOfReport & ";" _
, "Mode=Share Deny Write;" _
, "Extended Properties=""HDR=YES;"";" _
, "Jet OLEDB:System database="""";" _
, "Jet OLEDB:Registry Path="""";" _
, "Jet OLEDBatabase Password="""";" _
, "Jet OLEDB:Engine Type=35;" _
, "Jet OLEDBatabase Locking Mode=0;" _
, "Jet OLEDB:Global Partial Bulk Ops=2;" _
, "Jet OLEDB:Global Bulk Transactions=1;" _
, "Jet OLEDB:New Database Password="""";" _
, "Jet OLEDB:Create System Database=False;" _
, "Jet OLEDB:Encrypt Database=False;" _
, "Jet OLEDBon't Copy Locale on Compact=False;" _
, "Jet OLEDB:Compact Without Replica Repair=False;" _
, "Jet OLEDB:SFP=False") _
, Destination:=Sheets("Temp Import").Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("RAW$")
.Name = NameOfReport(FullNameOfReport)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = FullNameOfReport
.Refresh BackgroundQuery:=False
End With
Worksheets("Temp Import").QueryTables(1).Delete
-------------------
My *BIG* Problem is that other reports unfortunately do not have th
consistant naming of their sheets. I now wanted to import Reports o
type "DIR" and their sheets are named anything but "DIR".
So my code fails at the *-.commandtext -* parameter wher
*-Array("RAW$")*- is still hard coded from when I created that cod
first while recording a Macro.
HELP NEEDE
How can I replace Array("RAW$") with a calculated expression tha
refers to the name of the first sheet (or better sheet x) in the sourc
workbook.
YOU KNOW A BETTER METHOD FOR WHAT I AM DOING
If you know a better method to import a worksheet in my temp sheet
without opening and showing the source workbook, let me know.
Many thanks in advanc