When SaveAS through macro, file saves as xlsx with 6mb - vba

I have an Excel Workbook where I have a bunch of macros. One of them is to save that file in .xlsx, deleting other sheets, leaving just one and cleaning out the macros and VBA code.
The problem is, when i "export" it, the file saves as 6mb, even when I save it with no values in columns and rows. I tried to copy the used ranged and to paste it as values but it's not working.
I think my workbook is assuming it as thousands of cells, when I have only one when I use ctrl + shift + right/down.
Can you help me? Even copy and pasting all as values isn't working.

Related

Clear Contents Fail

I have some VBA code written for a macro that copies one sheet from a workbook, pastes the contents of this sheet into the current workbook (the one that holds the macro), and then does a lot of different things to clean the data. Once the data is cleaned, I run a line that clears the contents of a destination sheet, then I have the code cop the contents of the current sheet (with the cleaned data) and paste it onto this destination sheet. The problem I'm having is that the clear contents command keeps pulling up an error. This macro has been around for months and I didn't start running into this problem until a couple of days ago and i can't figure out what's going on. Any help would be appreicated.
Worksheets("DEMAND").Range("A1:CZ500").ClearContents
'This line ^^^ is where the code is breaking.
Worksheets("DEMAND0").Range("A1:CZ500").Copy
Worksheets("DEMAND").Range("A1").PasteSpecial xlPasteValues

VBA: Copy whole word document to excel

I´m trying to simply copy whole word document into excel and keep the source formatting (and images).
Assuming both documents are open.
I tried this code:
Sheets("Nabídka").Range("A" & 87) = Documents("K4E pila.docx").Range.Text
but it only copies the text without formatting and images. Is there a similar command which includes the formatting?
Even If I copy/paste from word to excel (ctrl+c) the formatting is ruined --> The image overlaps the text.
This is the first step I need to figure out to proceed in my project. The outcome should be: Copy all word documents into excel if the name of the word document matches some excel cells values.
Thanks in advance for any help!

copying worksheet values from one workbook to another vb.net without formula links

Using vb.net. we want to copy several worksheets from one file into another file. we have it looping and copying which ones we have going by
xlWorksheetSource.Copy(After:=xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count))
for example... but i noticed when i open up the file that was created.. in some of the columns it has links that reference the original file for formulas... how can you just have it so it copies the values and doesn't reference the original file in formulas that it copied.
You probably need to do something like this
xlWorksheetSource.PasteSpecial(xlPasteValues, xlPasteSpecialOperationNone, False, False)
For more information use this link:
ExcelPasteSpecial

Excel sheet Deletes the formulas present in the sheet when I open it. How to avoid this?

I'm uploading an excel file that contains sheets, to my server which encodes to base 64 so I decode it as required and process it by adding data in sheet 5 as column1 and column2 with certain number of rows. At the time of uploading, this sheet has some specific formulas on sheet 5 that makes changes in other sheets. So on opening the file which I send as response after editing from server, There comes this prompt that reads
"Excel Found unreadable content in 'MyDownloadedExcelData.xlsx'. Do you want to recover the contents of this workbook?If you trust the source of this workbook, click Yes', with Yes and no buttons
and when I click on yes and open the sheet, all the formulas are deleted.
I see something like
Excel was able to open the file by repairing ot removing the unreadable content.
Removed Records :Formula from /xl/calcChain.xml Part
Repaired Records : Cell Information from /xl/worksheets/sheet1.xml part etc
So, How do I make sure my formulas in the sheet are retained?
Using VBA you could have an on close event that pastes values and an on open event that recreates the formulas. Your file would essentially save with static data, but then be used with functions intact.
If this solution is of interest I can help provide some coding framework.

Convert xls File to csv, but extra rows added?

So, I am trying to convert some xls files to a csv, and everything works great, except for one part. The SaveAs function in the Excel interop seems to export all of the rows (including blank ones). I can see these rows when I look at the file using Notepad. (All of the rows I expect, 15 rows with two single quotes, then the rest are just blank). I then have a stored procedure that takes this csv and imports to the desired table (this works on spreadsheets that have been manually converted to csv (e.g. open, File--> Saves As, etc.)
Here is the line of code I am using for my SavesAs in my code. I have tried xlCSV, xlCSVWindows, and xlCSVDOS as my file format, but they all do the same thing.
wb.SaveAs(aFiles(i).Replace(".xls", "B.csv"), Excel.XlFileFormat.xlCSVMSDOS, , , , False) 'saves a copy of the spreadsheet as a csv
So, is there some additional step/setting I need to do to not get the extraneuos rows to show up in the csv?
Note that if I open this newly created csv, and then click Save As, and choose csv, my procedure likes it again.
When you create a CSV from a Workbook, the CSV is generated based upon your UsedRange. Since the UsedRange can be expanded simply by having formatting applied to a cell (without any contents) this is why you are getting blank rows. (You can also get blank columns due to this issue.)
When you open the generated CSV all of those no-content cells no longer contribute to the UsedRange due to having no content or formatting (since only values are saved in CSVs).
You can correct this issue by updating your used range before the save. Here's a brief sub I wrote in VBA that would do the trick. This code would make you lose all formatting, but I figured that wasn't important since you're saving to a CSV anyway. I'll leave the conversion to VB.Net up to you.
Sub CorrectUsedRange()
Dim values
Dim usedRangeAddress As String
Dim r As Range
'Get UsedRange Address prior to deleting Range
usedRangeAddress = ActiveSheet.UsedRange.Address
'Store values of cells to array.
values = ActiveSheet.UsedRange
'Delete all cells in the sheet
ActiveSheet.Cells.Delete
'Restore values to their initial locations
Range(usedRangeAddress) = values
End Sub
Tested your code with VBA and Excel2007 - works nice.
However, I could replicate it somewhat, by formatting an empty cell below my data-cells to bold. Then I would get empty single quotes in the csv. BUT this was also the case, when I used SaveAs.
So, my suggestion would be to clear all non-data cells, then to save your file. This way you can at least exclude this point of error.
I'm afraid that may not be enough. It seems there's an Excel bug that makes even deleting the non-data cells insufficient to prevent them from being written out as empty cells when saving as csv.
http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-bug-save-as-csv-saves-previously-deleted/2da9a8b4-50c2-49fd-a998-6b342694681e
Another way, without a script. Hit Ctrl+End . If that ends up in a row AFTER your real data, then select the rows from the first one until at least the row this ends up on, right click, and "Clear Contents".