Excel/Macro

Advertisement


Question
I have a report that runs every two weeks with the following macro.  The file starts out as a generic template, but is immediately saved as “Productivity Report “xxxxxx”, with  “xxxxxx” representing yr,mth,day (see line 5 of the macro below).  The filename is created by a formula, and resides in cell “V1”.  

The file is run every two weeks.  Can line 5 be modified so that it can read the variable file name in “V1” instead of the one hard coded in the template?


   Workbooks.Open Filename:="Q:\Productivity\Activity Summary.xls"
   Sheets("Summary").Select
   Sheets("Import").Visible = True
   Range("A1").Select
   Windows("Productivity Report 130401.xlsm").Activate
   Application.Goto Reference:="REPORT"
   Selection.Copy
   Windows("Activity Summary.xls").Activate
   Sheets("Import").Select
   Range("A1").Select
   Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
   Sheets("Import").Select
   ActiveWindow.SelectedSheets.Visible = False

Thanks

Answer
You don't say cell V1 of what sheet.  I will assume whichever sheet is visible when you issue the Range("A1").Select but I doubt that will be the correct sheet.  Also, if you immediately save the file with the new name - that code is not evident in the snippet here - but if you do, why not set a reference to that workbook and you can use the reference instead.  

Workbooks.Open Filename:="Q:\Productivity\Activity Summary.xls"
   Sheets("Summary").Select
   Sheets("Import").Visible = True
   Range("A1").Select
   ' assume cell 1 with the 6 digit date is on the currently active sheet
   sName = Range("V1").Text
   s1 = "Productivity Report XXXXXX.xlsm"
   s1 = Application.Substitute(s1,"XXXXXX", sName)
   'Windows("Productivity Report 130401.xlsm").Activate
   Windows(s1).Activate
   Application.Goto Reference:="REPORT"
   Selection.Copy
   Windows("Activity Summary.xls").Activate
   Sheets("Import").Select
   Range("A1").Select
   Selection.PasteSpecial Paste:=xlPasteValues, _
       Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
   Sheets("Import").Select
   ActiveWindow.SelectedSheets.Visible = False



-  Now if V1 holds the whole string - not just the 6 digits

Workbooks.Open Filename:="Q:\Productivity\Activity Summary.xls"
   Sheets("Summary").Select
   Sheets("Import").Visible = True
   Range("A1").Select
   ' assume cell 1 with the 6 digit date is on the currently active sheet
   sName = Range("V1").Text
   'Windows("Productivity Report 130401.xlsm").Activate
   Windows(sName).Activate
   Application.Goto Reference:="REPORT"
   Selection.Copy
   Windows("Activity Summary.xls").Activate
   Sheets("Import").Select
   Range("A1").Select
   Selection.PasteSpecial Paste:=xlPasteValues, _
       Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
   Sheets("Import").Select
   ActiveWindow.SelectedSheets.Visible = False

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.