Excel VBA code is causing my worksheets to appear as jargon - vba

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)

Related

VBA Worksheets.Copy method changes comma style behaviour

I used the following code to copy two sheets from my workbook to another file:
Worksheets(Array("One", "Two")).Copy
With ActiveWorkbook
.SaveAs Filename:=dir & "New_" & d & ".xlsx", FileFormat:=51
.Close SaveChanges:=False
End With
But it seems to change the way negative numbers are treated by comma style. In the original workbook, pressing the physical "Comma style" button would make a number look like -734,343.54 (with the minus sign at the far left). While in the new file it gives it parentheses, i.e. (734,343.54).
Setting the style as "Accounting" gives the same results for both workbooks, that is with the minus sign, except it includes the currency, which I do not need. How could I make sure that comma style does not change it's behaviour?
Update (more understandable code):
Sub test()
Worksheets(Array("Sheet1")).Copy
With ActiveWorkbook
.SaveAs Filename:="New" & ".xlsx", FileFormat:=51
.Close SaveChanges:=False
End With
End Sub
Adding Local:=True as a parameter to .SaveAs did the trick. From what I found:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-saveas-method-excel
True saves files against the language of Microsoft Excel (including control panel settings). False (default) saves files against the language of Visual Basic for Applications (VBA) (which is typically US English unless the VBA project where Workbooks.Open is run from is an old internationalized XL5/95 VBA project).
It seems that this mostly applies to users who don't use English as the main language for Excel.

Visual Basic: Save in same folder as Excel Sheet

I'm very new to VB, so this is probably a very easy one. I'm creating a Word document from an Excel spreadsheet, and would like the Word doc to save in the same folder location as the spreadsheet.
I'm using the code:
.SaveAs Filename:=ThisWorkbook.Path & Range("C8").Text & ".docx"
Which I though would work, but it saves it in teh directory up from the location.
I.e. the spreadsheet is in C:/User/Documents/MySpreadsheet. But the Word doc would be saved in C:/User/Documents.
I also made a message popup to display ThisWorkbook.Path which comes up with the Spreadsheet path, so I know that's right!
I also don't think I've done the naming right, as I would like it to be named the text in cell C8. But it's actually the 'Documents' folder name with the text in C8 added on.
Thanks in advance.
Activate Immediate Window (Ctrl+G) and and add this line to your code:
debug.print ThisWorkbook.Path & Range("C8").Text & ".docx"
You will see if your path is correct. In particular, if you have "\" between folder path and filename.

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.

UFT-API Testing-How can I can store values of response in an excel file?

I am sending a request two times using LOOP, and in output I am getting a unique value. I want to store these unique values in an excel sheet column so that further I can parameterize all the values.
I have successfully got the values to a TEXT file. But what I need is to store them in separate rows (in a column) in an EXCEL file, which I am not able to do.
Directly, I don't know. But you can always cheat. For example, EXCEL has a secondary format called .CSV, which is plain test...with ";" as separators.
So write your "output.csv" file with the following kind of structure :
Name;Income;Status
Martin;25000;OK
Johnson;32500;KO
And it should be plain openable in EXCEL. As a CSV. Once in EXCEL, save it back as a XLS or XLSX, and you're done, you've got a EXCEL file perfectly usable.
If you need some kind of automation for making this .csv into a .xlsx, here is a sample macro I made quickly & dirty, but works with EXCEL 2013 :
Sub ConvertFile()
On Error Resume Next
Kill "C:\MyPath\output.xlsx"
On Error GoTo 0
Workbooks.Open Filename:= _
"C:\MyPath\output.csv"
ActiveWorkbook.SaveAs Filename:= _
"C:\MyPath\output.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
Beware : this is a EXCEL macro, not vbscript.

Blank worksheets opened up every time Excel starts - accidentally created after splitting Spreadsheet using VBA code

I need a bit of help with Xcel 2010.
I have a spreadsheet budget workbook in Xcel 2010 that had 6 tabs of worksheets. I needed to make a copy of just one of those worksheets to pass on to someone who was not allowed to see the other worksheets.
I used this VBA code shown below to split up the workbook and it worked just fine. But, since doing so, now every time I open up any Xcel file I get three blank worksheets that open up at the same time and they are labeled as Tabelle1.xls, Tabelle2.xls and Tabelle3.xls.
I thought that using another VBA code (see below the other code) to delete the module would take care of that but it did not. Now there is no code/module at all if I open up Visual Basic but I still get three blank worksheets that open up separately from whatever Xcel file I open up. The three blank worksheets all have the .xls file extension but the version of MS Office I have uses .xlsx.
Other info: I am using Windows 7, this is my work computer and do not know anything about VBA coding myself. I was just looking for a quick fix by Googling and got myself in trouble.
The code to split up the Workbook
Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xls"
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub
The code I used to delete the module:
Sub DeleteModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
VBProj.VBComponents.Remove VBComp
End Sub
SectionBreak
Your symptoms are indicative of a workbook being in the XLSTART folder (an 'autorun' folder). The reason for the names being "Tabelle" are that the source of this workbook was likely a German locale.
Any workbook in this folder is opened whenever the Excel application starts up. It is a folder typically used to place your personal macro workbooks so macros within are automatically available when working on different workbooks.
In your case, this appears unintentional. Check the folder at, typically:
Win7/8: C:\Users\[Username]\AppData\Roaming\Microsoft\Excel\XLSTART
WinXP: C:\Documents and Settings\[Username]\Application Data\Microsoft\Excel\XLStart
and clear out any files you were not expecting there.
I refer you to http://office.microsoft.com/en-gb/excel-help/customize-how-excel-starts-HP010197489.aspx for more details over how you can customise the templates and workbooks Excel uses to achieve helpful results.