View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Don't want to hard-code file name in macro

Two suggestions right off the top of my head...

In Finfo:
Since you include 'All Files (*.*),*.*' then the others are pointless.

Workbook1:
Since this is the workbook that contains the code, you can use
'ThisWorkbook' in code to get an absolute ref to it regardless of its
name.

Pivot Table sheet:
If you implement a special codename for this sheet you can use it
instead of the tab name in case the tab name gets changed. For example,
select the sheet in the VBE Project Explorer window and rename its
'(Name)' property at the top of the Properties list to "wksPivotTable".
Now use this name in your code instead of its tab name...

ThisWorkbook.Sheets(Get_SheetTabName(ThisWorkbook, "wksPivotTable"))

...which calls the following reusable function that returns the tab name
based on the sheet's codename.

Function Get_SheetTabName(Wkb As Workbook, CodeName As String) As
String
Dim Wks As Worksheet
For Each Wks In Wkb.Worksheets
If Wks.CodeName = CodeName Then Get_SheetTabName = Wks.name: Exit
Function
Next
End Function

--

Other suggestions...

I find it helpful to use absolute object refs for workbooks/worksheets.
For example...

Dim wksTarget As Worksheet
Set wksTarget = ThisWorkbook.Sheets(Get_SheetTabName(ThisWorkbook,
"wksPivotTable"))

...then in code I can simply ref that sheet as follows...

wksTarget.Range("A1")

So, in your workbook that contains the code...

Rename Sheets("ARMS Detailed Scheduling Report").CodeName to
"wksDetails"

Rename Sheets("ARMS Summary Scheduled Hrs").CodeName to
"wksSummary"

...then try this code...

Sub GetARMSExport2()
Dim bEventsEnabled As Boolean
Dim vFilename As Variant, vCalcMode As Variant
Dim wksTarget As Worksheet, wkbSource As Workbook

'Dialog parameters
Const sFinfo As String = "All Files (*.*),*.*"
Const sTitle As String = "Select a File to Import"

'Get the vFilename
vFilename = Application.GetOpenvFilename(sFinfo, , sTitle)
'If user cancels
If vFilename = False Then MsgBox "No file was selected.": Exit Sub

With Application
vCalcMode = .Calculation: bEventsEnabled = .EnableEvents
.Calculation = xlCalculationManual: .EnableEvents = False
.ScreenUpdating = False
End With 'Application

Set wksTarget = ThisWorkbook.Sheets(Get_SheetTabName(ThisWorkbook,
"wksDetails"))
Set wkbSource = Workbooks.Open(vFilename)
ActiveSheet.UsedRange.Copy Destination:=wksTarget.Range("A1")
Application.CutCopyMode = False
' wksTarget.Range("A1").Select '..if necessary

Set wksTarget = ThisWorkbook.Sheets(Get_SheetTabName(ThisWorkbook,
"wksSummary"))
With wksTarget
With .PivotTables("PivotTable4").PivotCache: .Refresh: .Refresh:
End With
.Range("A6").Select
End With 'wksTarget

'Destroy unused objects
Set wksTarget = Nothing: Set wkbSource = Nothing

'Cleanup
With Application
.Calculation = vCalcMode: .EnableEvents = bEventsEnabled:
..ScreenUpdating = False
End With 'Application
End Sub

(watch for line wraps)

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion