VB Code to save as XML Spreadsheet

Desu Nota from Columbus

Well-known Member
Joined
Mar 17, 2011
Messages
556
I tried searching google and this website for a code and nothing worked.

I am opening notepad documents in excel and running a macro I recorded, and then I am manually clicking, save as-->xml spreadsheet, using the same file name.

How do you make this happen automatically? I need a code to add to the end of the macro I recorded.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
So what exactly do I add to the end of my macro? I can't make sense of all the different lines of code from this website. I want it to use the parent file's name, but just change the file type to xml without removing the parent file.

Can someone please demystify this for me?

I dont think this keeps the parent file name:

Code:
ActiveWorkbook.SaveAs "C:\ron.xlsm", fileformat:=46

the "C:\ron.xlsm" part needs to say, "use parent file name or use current file name" <----however this is worded with code
 
Last edited:
Upvote 0
This should do what you asked for:
Code:
If InStrRev(ThisWorkbook.Name, ".") > 0 Then
    ActiveWorkbook.SaveAs Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1) & ".xml", FileFormat:=46
Else
    MsgBox "Save with a valid filename before saving as .xml"
End If
 
Upvote 0
First off, thank you for the help but I need one more problem resolved.

When I use this, it asks me to override my worksheet named personal.xls.

I have the macro saved to personal.xls, where should I save the macro instead or how should the code be tweeked?
 
Last edited:
Upvote 0
Concur with Andrew Poulsom. I only tested my code in the workbook that contained the code and was not consistent about how I referenced that workbook!

When the old code is installed in the personal.xls workbook ThisWorkbook refers to the personal.xls workbook. ActiveWorkbook always refers to the workbook that has focus.

Revised code below.

Code:
If InStrRev(ActiveWorkbook.Name, ".") > 0 Then
    ActiveWorkbook.SaveAs Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1) & ".xml", FileFormat:=46
Else
    MsgBox "Save with a valid filename before saving as .xml"
End If
 
Upvote 0
Thanks a bunch. It works.

In the future, is there a way to specify the folder it saves to? Right now (and I am completely ok with it) everything is saved to /My Documents.
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,212
Members
452,895
Latest member
BILLING GUY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top