[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

Excel Default File Location

Experts:

In Excel, how can I define both "default file location" and "default filename" through VBA.

I do >> NOT << want to use the  "Tools | Options | General" to set the default location.

Essentially, the spreadsheet (.XLT) would be used by multiple individuals... they all have to save it to the same location with the same filename (thus replacing existing files in the default subfolder).

Ideally, there would be a macro button on the worksheet (.XLT) ... once clicked the content of the .XLT would then be saved as an .XLS without them even having to specify name/location.

Is that doable?  If so, how can this be done via VBA?

EEH
0
ExpExchHelp
Asked:
ExpExchHelp
  • 8
  • 5
3 Solutions
 
FlysterCommented:
Here's a macro that will save your xml file as a xls in the location and name you define in Filename =. It will automatically overwrite previous editions without warnings. If you want the warnings, just remove the first and last lines.
Sub SaveAsXls()

  Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Enter\Path\Here\YourFileName.xls", FileFormat:= _
        xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False
  Application.DisplayAlerts = True
  
End Sub

Open in new window

Flyster
0
 
ExpExchHelpAuthor Commented:
Flyster:

Thanks for the prompt reply.    Just a few clarifications questions...

1. Where in the VBA should I save the code?  
- ThisWorksheet? or
- Sheet1? or
- Modules|Modules1

2. How can I link the code to a command button which should be located, e.g., in cell A1?

Thanks in advance for the continued help.

EEH
0
 
FlysterCommented:
1. This should be placed in a module in your template file.

2. In design mode, right-click your command button. If it's a legacy command button, there will be an option to assign a macro to the button. If it's an active-x command button, select "View Code" and paste it there.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ExpExchHelpAuthor Commented:
Thanks...

When running the macro, I get a compile error.

Please see attached XLT and JPG.

How can I fix the compile error?

EEH
Test.xlt
CompileError.jpg
0
 
FlysterCommented:
My apologizes, I was using Word 2007. Try changing FileFormat:=xlExcel8 to FileFormat:=xlNormal
0
 
ExpExchHelpAuthor Commented:
Great... that works.   Excellent!

I only forgot to mention one thing earlier.   In the XLS that is copied, I need to have the command button removed (i.e., all content will be copied except the command button).

I have something similar in MS-Word where the line (see below) removes the shape (i.e, the command button in Word):

doc.Shapes(1).Delete

Can I use something similar in Excel to remove that shape as well?   If yes, that's line of code?

EEH
0
 
FlysterCommented:
Try this after the first line:

    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Delete

Change"CommandButton1" to the name of your command button.
0
 
ExpExchHelpAuthor Commented:
MOST EXCELLENT!!   Works like a charm!

** Break **

Now I have to do the same for a PowerPoint file.  I'll post another thread tomorrow and will provide the URL here... in case you're willing to assist (tomorrow).

Cheers,
EEH
0
 
FlysterCommented:
I'll keep my eye out for it! Thanks
0
 
ExpExchHelpAuthor Commented:
Flyster:

Actually, I already opened up the thread... pretty much the same approach, except this process is focused on PowerPoint vs. Excel.

Below is the new thread:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Powerpoint/Q_28291456.html

I'd welcome if you have any suggestion on this topic.

EEH
0
 
ExpExchHelpAuthor Commented:
Flyster:

Quick follow-up... what's the VBA code for deleting a command button in MS-Word?

Cheers,
EEH
0
 
ExpExchHelpAuthor Commented:
That is... currently I use a shape (rectangle) vs. actual command button.   The current code is:

Private Sub CommandButton1_Click()
    Dim doc As Document
    Set doc = ActiveDocument
    doc.Shapes(1).Delete
    doc.SaveAs "C:\KTG_Testing\DailyWatchOrder.doc"
End Sub

However, having used a command button in Excel and PowerPoint, this is now the preferred method.   Based on the existing code, deleting the command button in Word does work though.

Any thoughts/recommendations?

EEH
0
 
ExpExchHelpAuthor Commented:
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now