Excel/Excel VBA 2003

Advertisement


Question
QUESTION: Hello Bob,

I'm using Excel 2003 and VBA. In the final steps of a macro which involves opening and closing several Excel worksheets in different workbooks on Drive C, there is one file (the updated master) which I also want to save to a CD (Drive E) as a backup before closing the job. Here's my macro code:

Sub SaveMstrFiles()

‘Currently on Drive C
'Create a new workbook to hold the worksheet containing this week's master file data

   Workbooks.Add       'New workbook to contain the master file data as of end of this week's run
   ActiveWorkbook.SaveAs Filename:= "Week" & Range("CurrentWk") & ".xls"   'Ex: Week17.xls

‘Now, Save the same workbook on a CD on Drive E

  ChDir "E:\MyBackUp"
   Workbooks.Add       'New workbook to contain master backup
   ActiveWorkbook.SaveAs "Week" & Range("CurrentWk") & ".xls"          'Ex: Week17.xls
‘    ActiveWorkbook.SaveAs Filename:= "Week" & Range("CurrentWk") & ".xls"   'Ex: Week17.xls
‘**********************************************************************************
  'Change back to program on Drive C:

    ChDir "C:\My Docs\Excel\Bowl"

The macro works successfully until it gets to one of the two statements shown above the row of stars. I’ve tried each and they both give me the same error:

Method ‘Range’ of object’ _Global’ failed

What am I doing wrong? What statement would do the job? I’ve been trying to solve this for days and have finally given up. Your assistance in resolving this would be most appreciated.

ANSWER: Before you change Directory to another drive, you need to change drive:
ChDrive "E"
ChDir "E:\MyBackUp"

and later,
ChDrive "C"
ChDir "C:\My Docs\Excel\Bowl"

The message indicates there is no range called "CurrentWk", but it does seem like there is. Try Range("CurrentWk").Value in its place  (adding ".Value")

HTH

---------- FOLLOW-UP ----------

QUESTION: Hi Bob,

I know it's been a week since I last contacted you, but my macro problem has had to take low priority for now. Sorry.

I have changed the drive designation as you suggested and that solved that problem. Also, your comment about "CurrentWk" highlighted for me another error in my code which I have also now corrected.

Here's my current revised code:

'Create a workbook on Drive C to hold one sheet containing this week's master file data

   Workbooks.Add       'New workbook for master file data
   ActiveWorkbook.SaveAs Filename:="Week" & CurrentWk & ".xls"   

   ChDrive "E"
   ChDir "E:\Bowling"
     
   Workbooks.Add       'New workbook on Drive E for backup file
   
   ActiveWorkbook.SaveAs Filename:="Week" & CurrentWk & " BU.xls"   'Ex: Week17 BU.xls

  'Change back to bowling program
    
    ChDrive "C"
    ChDir "C:\My Docs\Excel\Bowl"


Problem No. 1 -- Before adding " BU" to the filename, I ran another test and got an error that I couldn't save the file with the same name as another file already open. Why?  I'm placing that new backup file on another drive; why can't I have the same file and filename on two different drives?

Problem No. 2 -- So I modified the backup filename slightly (added " BU") and the Save worked just fine. (The backup filename is "Week17 BU.xls".) However when I tried to open the backed-up file on Drive E to check its contents, I received another error message that said "The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now? Yes/No/Help

I clicked on Yes, the file opened, but the file was totally blank. It contained no data. I am stymied again.

Any suggestions to either of these problems?

A thought: Would it be better to save and close the file on Drive C and then COPY/PASTE that file to Drive E? If so, could you advise me on how to do that within the macro?

Bob, I sincerely appreciate all the help you provide. Thank you very much.

Bob Kunz

Answer
#1 - Even tho the 2 files are on different drives, they can't both be open because Excel's list of open files must be unique. Once you've saved it on the E (or any) drive, you should close it or you'd run into that problem.

#2 - Likely you're mixing xlsx files with xls. If you save a true xls file with xlsx format or vice versa, you'll have issues opening it. You should check Help for  
XlFileFormat Enumeration

A sample valid save would be:
ActiveWorkbook.SaveAs Filename:="Week" & CurrentWk & ".xls", xlExcel8
or
ActiveWorkbook.SaveAs Filename:="Week" & CurrentWk & ".xlsx", xlOpenXMLWorkbook
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


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.