Converting IQy files to XLSX using VBA - vba

I currently wrote a Pyhon script that extract metadata from SharePoint online using the "Export to Excel" button. When the data saves it saves as a IQy files (internet query file) and I need to save it as an XLSX. Theres about 30 files that I want to loop through and save.
These IQy files can be opened with excel and then I can use saveas to save it as an XLSX. Is there a way to automate this? I found some code in another forum but I don't completely understand it.
ThisWorkbook.Sheets.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
ActiveWorkbook.Close
the line I don't understand is ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
Would it be possible to get an explanation of this line? or is there another way to use the saveas to convert these files into XLSX? I'm open to using any other language as well.

This seems pretty clear. It's calling SaveAs to save the workbook to a particular path, using the FileFormat argument to save it as an XLSX file, which is type 51.

Related

The file format and extension of filename.csv don't match

I saved a file in .csv format using VBA. Below is the code:
ActiveWorkbook.SaveAs "FilePath" & Format(Now() - 3, "mm.dd.yy") & ".csv"
The file is saved successfully. However, while opening the file, it is throwing the error "The file format and extension of filename.csv don't match. The file could be corrupted or unsafe". I am trying to suppress this warning message. It would be great if someone helps me to know what I am missing here.
i guess you are missing the fileformat.
ActiveWorkbook.SaveAs Filename:="FilePath" & Format(Now() - 3, "mm.dd.yy") & ".csv", _
FileFormat:=xlCSV
If this Fileformat is not working, you can have a look in the following link,
where you can find more versions for using CSV:)
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel

Why is VBA creating a folder on ActiveWorkbook.SaveAs

I have this VBA:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls"
ActiveWorkbook.Close
This is supposed to save the currently active workbook or worksheet to a path and file name provided. The extension ".xls" is used so that the exported file is a Excel 97-2003 Workbook.
It saves the Workbook or Worksheet but has a folder included in them. Let's say the file name was "Master.xls", ActiveWorkbook.SaveAs works but the VBA seem to include a folder named "Master_files" next to it as if it were saved as a web file. How can I disable this?
Note that the sheet came from an online database, when I click the link to export the records into an Excel sheet it doesn't download it but instead opens it straight from web to my Excel application which is why I created this SaveAs code.
I believe you need to expressly specify the format:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls", FileFormat:=xlExcel8
Otherwise, it is saving as HTML, along with the _files, because you got it from the Web (I think). MSDN says:
For an existing file, the default format is the last file format specified
which I am guessing is HTML for your situation.
xlExcel8 is the constant to use for .xls files per Ron de Bruin's reference.

Save Excel table in current folder

I want to save my current table as a text file in the same folder as the current workbook (that is open).
I use this code:
ActiveWorkbook.SaveAs ThisWorkbook.path & "\" & filename
For some reason it save the text file in the same folder as my personal.xlsb.
I use Office 2010
If this is not possible to do easily then maybe one can force Excel to open a browse window where I can pick where I want the file saved.
ThisWorkbook points to the location where the code is written. In your case personal.xlsb.
If you want to save the table in the same directory as the active workbook, use ActiveWorkbook.path instead.
You may try this
ChDir "C:\yourfolder"
ActiveWorkbook.SaveAs Filename:= _
"c:\yourfolder" & filename & ".txt", FileFormat:=xlText, _
In this example I have created a folder name "yourfolder" in my C drive. The created text file will save in C\yourfolder\filenane.txt
Hope this is helpful

How to save as with VBA in current location

I'm currently saving my excel file with this command:
ActiveWorkbook.SaveAs Filename:="pleasework", FileFormat:=52
But when it saves it, it saves it in documents.
I want to save it in the current location of the macro (of the file, where it's activated).
Any advice? Changing it to:
Filename:="C:/pleasework"
Won't work...
Thanks!
Give the following a try
...
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\pleasework", FileFormat:=52
...
In addition you may first want to check whether Application.ActiveWorkbook.Path returns a valid path so in case your currently open file wasn't saved yet you won't try to save it to an invalid path.
Try this
Option Explicit
Sub Book_Path()
Debug.Print ThisWorkbook.Path
ActiveWorkbook.SaveAs FileName:=ThisWorkbook.Path & "\" & "pleasework", FileFormat:=52
End Sub

Excel VBA code is causing my worksheets to appear as jargon

I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)