Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Save a document with same filename but different extension in VBA.  The filename will be different everytime.

Posted on 2014-09-24
1
Medium Priority
?
2,157 Views
Last Modified: 2014-09-26
Hi,

I want to save a .csv file as a xlsx with the same filename.  I am using the following code:

Public Function s_as(xlsx As String) As String
s_as = ThisWorkbook.FullName
s_as = Left(s_as, InStrRev(s_as, ".")) & xlsx
End Function


The function is being called out in another module using this code:

Sub FORMAT_INV()

format_invsh

ThisWorkbook.SaveAs FileName:=s_as("xlsx")

End Sub




2 problems & 1 extra enhancement:

1) When I run this macro I get the error: "Run-time error 1004, this extension can not be used with the selected file type. CHange the file extension in the File name text box or select a different file type by changing the Save as type".    
 

2)  The following  happens if I change every instance of "xlsx" to "xls" in the code examples above:  

 the code lives in a spreadsheet called "macros.xlsm" that happens to be in the same directory.  The code is saving the filename as "macros" instead of the filename of the active worksheet that it is formatting and saving.    This will be problematic,  especially since I want to eventually transfer this code to the user's "personal.xlsm" in a completely different directory.  

TO CLARIFY: i want the code to save as with the same filename, different extension in the same directory of the active worksheet that the code is formatting and saving.

3) extra enhancement:  I want another output that will save the resulting .xlsx as the current filename, the text "_PROD", and .xlsx

In all three of the requests, could you explain the usage on how the code will be called out maybe from another module as well as the functions etc.  

thanks
0
Comment
Question by:tike55
[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
1 Comment
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 40342693
You will need to specify the save format.
ThisWorkbook.SaveAs FileName:=s_as("xlsx"),  FileFormat:=xlOpenXMLWorkbook

Open in new window

However be careful. ThisWorkbook means the one containing the code. It might, depending on the context, be better to use ActiveWorkbook.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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‚Ķ

664 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