Solved

Excel Default File Location

Posted on 2013-11-11
13
545 Views
Last Modified: 2013-11-12
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
Comment
Question by:ExpExchHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 22

Accepted Solution

by:
Flyster earned 500 total points
ID: 39640128
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
 

Author Comment

by:ExpExchHelp
ID: 39640147
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
 
LVL 22

Expert Comment

by:Flyster
ID: 39640190
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:ExpExchHelp
ID: 39640211
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
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 500 total points
ID: 39640282
My apologizes, I was using Word 2007. Try changing FileFormat:=xlExcel8 to FileFormat:=xlNormal
0
 

Author Comment

by:ExpExchHelp
ID: 39640334
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
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 500 total points
ID: 39640398
Try this after the first line:

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

Change"CommandButton1" to the name of your command button.
0
 

Author Comment

by:ExpExchHelp
ID: 39640458
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
 
LVL 22

Expert Comment

by:Flyster
ID: 39640475
I'll keep my eye out for it! Thanks
0
 

Author Comment

by:ExpExchHelp
ID: 39640476
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
 

Author Comment

by:ExpExchHelp
ID: 39641423
Flyster:

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

Cheers,
EEH
0
 

Author Comment

by:ExpExchHelp
ID: 39641447
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
 

Author Comment

by:ExpExchHelp
ID: 39641516
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question