Import raw data from excel into another excel - excel

I'm using the following Code to import a raw data extract (mysql; output format *.xlsx) from an excel sheet (source) into a sheet (target).
Importing Excel spreadsheet data into another Excel spreadsheet containing VBA
The target sheet is a excel-template contains some macros e.g. to allow users to easily sort data.
All is working very well, except that some data are malformed after import. Its really strange but due to any reason target is not an exact copy of the source sheet. Issues I've for example with date format and decimals.
I've tried several things to tweak the code but failed.
I believe that the way how the import in target sheet is handled by code is the reason why it doesnt work.
Now my question is if there exits any other way to import data from excel to excel.
Also I'm looking for a solution how I could solve the problem that excel formats all values automatically as text?
Any help would be highly appreciated.
Kind regards

Try replacing this line
targetSheet.Range("A1", "C10").Value = sourceSheet.Range("A1", "C10").Value
With this
sourceSheet.Range("A1", "C10").Copy
targetSheet.Range("A1", "C10").PasteSpecial
The former only copies the values. The latter copies both values AND its formatting, so things like date formats and number of decimals should be included as well.

Related

Excel chart adapting to table with formula-generated rows -- without named ranges?

I have table-like data, and I'm looking to make a chart that displays that data. Since the data is that formula-generated from other data in the workbook, I don't know in advance how many lines it will have. I want to make a chart that adapts to this data.
Up to this part of the question, I could use named ranges to solve this.
The thing with the solution with named ranges is that it does not scale well when I have many of these charts. I have a Python script that generates CSV files that I import into my workbook as a sheet, and I don't want to have to know in advance how many of them there will be, or what they will be named. I only want to be able to import the CSV files into a new or existing sheet, and copy-paste the formulas from another working sheet, as well as be able to replace the existing CSV data with new data.
With named ranges, I would have to manually create a named range for each series of each sheet, and I would have to use the sheet's name so that the named range can be visible to the whole workbook (in order to analyze the data in a global sheet) without any name conflict. This is (more or less) acceptable when I do these steps myself now, but if I want to redo this in a few months, or if I pass my workbook to someone else, we wouldn't know why it's not working with a new sheet.
So is there any way to get this done without delving into VBA stuff? I'm using a recent version of Excel.
Per the comments, try using pivot tables, making your range extend far beyond expected results and filtering out "blanks" in your pivot and generate your chart from that. The default pivot chart is ugly but you can remove buttons and format as needed. Just remember to refresh your pivot during every update period.

Adding formulas to excel spreadsheet using python

I am attempting to insert formulas into an excel spreadsheet using python.
Examples of the formulas are:
=VLOOKUP(B3|"Settlement Info"!$B$2:$R$2199|17|FALSE)
=SUMIFS("Payment and Fees"!$I$2:$I$6445|"Payment and Fees"!$B$2:$B$6445|Summary!$B3)
=+E3-F3-G3-I3
=IF(AND(I3>0|I3-N3>=-0.1|I3-N3<=0.1)|"Yes"|"No")
I tried using xlsxwriter and when opening the ss in excel it repairs by removing the "unreadable" content and those cells show as 0. I've seen the comment that the recalculation should be done on the reopening of the sheet when using xlsxwriter but that does not look like is is being done (https://xlsxwriter.readthedocs.io/working_with_formulas.html)
Is there some way to get these formulas into excel without them being removed by excel on opening?
Thanks for any pointers.
I simplified this down to a simple as possible:
When I run the below code and then attempt to open the excel spreadsheet I get an error saying "We found a problem with some content in ...Do you want us to try to recover..If you trust the source select yes"
If I select yes then I get an error " Removed Records: Formula from /xl/worksheets/sheet1.xml part"
And if I continue the sheet opens and there is a 0 in the field.
from xlsxwriter.workbook import Workbook
workbook = Workbook('test.xlsx')
worksheet = workbook.add_worksheet('Summary')
worksheet.write_formula('A2', '=VLOOKUP(B3,"Settlement Info"!$B$2:$R$2199,17,FALSE)')
workbook.close()
If I look at the information at https://xlsxwriter.readthedocs.io/working_with_formulas.html there is the information:
XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.
This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas will only display the 0 results. Examples of such applications are Excel Viewer, PDF Converters, and some mobile device applications.
Which I may not be understanding as I believe that the formula should be left in the sheet.
You can XlsxWriter to create any formula that Excel can handle. However, you need to be careful with the formatting of the formula to make sure that it matches the US version of Excel (which is the default format that formulas are stored in).
So your formulas should probably work as expected if you use a comma instead of a pipe:
=VLOOKUP(B3,"Settlement Info"!$B$2:$R$2199,17,FALSE)
=SUMIFS("Payment and Fees"!$I$2:$I$6445,"Payment and Fees"!$B$2:$B$6445,Summary!$B3)
=IF(AND(I3>0,I3-N3>=-0.1|I3-N3<=0.1),"Yes","No")
This one should work without modification:
=+E3-F3-G3-I3
See this section of the XlsxWriter docs on Working with Formulas.
Update in relation to the updated question:
The formula still has an error. You need to use single quotes instead of double quotes. You also need to add another worksheet for the formula to refer to. Like this:
from xlsxwriter.workbook import Workbook
workbook = Workbook('test.xlsx')
worksheet = workbook.add_worksheet('Summary')
worksheet.write_formula('A2', "=VLOOKUP(B3,'Settlement Info'!$B$2:$R$2199,17,FALSE)")
workbook.add_worksheet('Settlement Info')
workbook.close()
There is still an #N/A error in Excel but that is related to not having data in the VLOOKUP range:
Output:

Downloading File with importrange function failing - think it's a bug

I've been saving Google Sheets to Excel without any problems for a while. These sheets have always successfully saved and opened in Excel with the importrange function. However, recently it hasn't been successfully saving correctly.
It used to just have the static value (e.g, 40). There used to be an IFERROR in the first cell in the header row but now it exists in every single cell.
E.g, each cell would have something like this:
=IFERROR(__xludf.DUMMYFUNCTION(importrange(blahblah)),"40").
DUMMYFUNCTION throws an error and "40" is returned as a result. but "40" is a string, not an integer which messes up all my formulas.
I also know this isn't an Excel issue because OpenOffice is doing the same thing with the file.
I'm pretty sure this would be a bug because why would it be working for months and then suddenly stop working?
What should I do?
I'm thinking it's a bug too.
Workarounds
On Excel
Copy and paste as values only the ranges with IFERROR(__xludf.DUMMYFUNCTION(..., then use Excel's UI tools to convert numbers shown as text to numbers.
Selectively remove quotes on the IFERROR second argument of the cells causing problems
Remove =IFERROR(__xludf.DUMMYFUNCTION(),"value") except value (we could use Excel's built-in FIND & REPLACE for this)
On Google Sheets
Use Copy > Paste as values only on the range areas having formulas with non-compatible functions like IMPORTRANGE, QUERY, FILTER, etc.
If you only need the values, download it as CSV instead of XLSX
IMPORTANT
In order to help to prioritize this issue, send feedback to Google. To do this open a Google Sheets spreadsheet, click on Help > Report a problem, then fill the feedback form and submit it.
Related stuff
I posted 5 small articles about this in Spanish. You could find them listed on https://www.rubenrivera.mx/p/descargar-hcg-excel.html.
We accidentally created a workaround for this bug with a different sheet that was just set up like this.
This works when you IMPORTRANGE into another Google Sheet. We are doing it into a Google Sheet with a single worksheet - haven't tried it with multiple.
It's going to sound a little nuts but it works for us.
In the first cell of your import range put a hyperlink in the original document you are importing from. This is in the first cell of the import range. We linked it to a worksheet in the original document. It has worked and failed with an external link. With an external link it worked when I linked it to an internal link, then changed it. But when I deleted the cell and just straight linked it to an external URL it didn't work.
Then #timbo was right - put data validation in. This can be in part of the document that isn't being imported into the second sheet. I put it in the first line of the import range but outside what I was importing. It might have to be the first line. I just put a date in one cell, then in the next cell data > data validation > then choose that one date as the data range.
For aesthetics I have hidden the first row in one Google Sheet I am importing into. In another I made the first cell link the title of the sheet and put the data validation outside the import range. Both of these work.
Let me know if this works for you.
Until this bug is fixed, a workaround is to put a data validation (Data > Data Validation) on the imported data (Any kind of data validation will do).

Problems viewing large numbers in Excel (Scientific Notation) in 'Text' formatted cells

I'm having a problem and havnt managed to find a solution online and was hoping I'd get lucky and someone could help.
I have a database application that exports a large dataset to .xlsx
A VBA application then maps this data into another Excel application.
When the data is exported out of the original database application, this process is outside of my control. All the cells have a 'General' cell format and we have some large numbers such as 172627108914 which is the serial number for a piece of equipment. In the exported xlsx file, this serial number is represented as 1.72627E+11.
The next stage of the process copies this data into another worksheet which has all cells formatted as text. The value is copied over but the value stays the same and the format of the cell changes from Text to General.
Does anyone know what I have to do to change to remove the scientific notation?
I'm using Microsoft Excel 2010.
Thanks
Append a single apostrophe to the front of the number. That will force Excel to read the number in 'text' format automatically, and the apostrophe will not show up at the front of the number when it's displayed.
Thanks for the help everyone, in the end some VBA code was written to convert all fields in the original export to text then iterate over each cell and rewriting the value from the formula line. This has resulted in a correctly formatted worksheet to pass to the second application

Macro to Split Excel Data into into Existing Tabs

thanks for looking at this problem, I hope I can get some help, as I am not very experienced with VBA syntax in excel.
Background:
I will be receiving a large (1000's of lines) CSV file that will contain data entries of various lengths. Each line will begin with a code (eg, 01, 02,..., 50) and have a series of data entries following it based on that code.
So, for example
01,data,data,data
01,data,data,data
02,data,data,data,data
etc...
I need to import all of this data into an existing excel workbook that already has separate tabs and headers created to correspond with the data type.
What I believe needs to be done, is to import the csv to a new, blank sheet, then run a vba program to check the data code, and move the line to the corresponding tab. I would also like to preserve the formatting on the destination sheet.
Ultimately, what I think I need is a VBA program to read the code cell, and move the line to an existing tab based on that code, and loop through the whole column.
Most of the existing solutions I have found involve the creation of new tabs, but I wish to parse the raw data into existing tabs with headers and formatting. I am aware this may require me to manually type in the code and destination tab names in the program's logic - That will not be an issue as long as I have a base to start with!
Thanks again for your help, and let me know if I can provide any more information.

Resources