Import data from one excel file to another - excel-formula

I have had some code that has been working really well for me for ages. It searches for and imports data from other files.
=IF(ISERROR(MATCH(1,INDEX((INT('November-2017.csv'!A$2:A$10000)=A107925)*('November-2017.csv'!L$2:L$10000=K107925),),0)),"",INDEX('November-2017.csv'!P$2:P$100000,MATCH(1,INDEX((INT('November-2017.csv'!A$2:A$10000)=A107925)*('November-2017.csv'!L$2:L$10000=K107925),),0)))
All I have ever needed to do is copy the formula into a cell, change the filename and relevant column and/or row references and the data imports into that cell; then I simply copy and paste it right down the destination column and it imports the data. Sometimes I have to increase the range as some source files have more than 10000 rows. The data has always been imported into the same file which has 10 or so different tabs. To avoid the file requesting to be updated all the time, I copy each of the columns and then paste the data back in as values. I merely leave the code in each column right at the top, so I have it handy to copy when needed.
But I have been trying to do the same today into a new file and cannot get it to import anything at all. The code has not changed; only the destination file. The new destination file is an Excel file .xls rather than a .csv file, but can't see that has any bearing on it. The source file has less than 10000 rows, making that all fine; the cell I am pasting into is row 107924 in the destination file. The date is column A and matches perfectly, the name in the source file is in column L and in the destination file it is column K; they also both match. The data I require is in column P in the source file. So everything looks fine to me and it still works if I import data into the file I have always imported into, yet the new destination cell remains blank.
When I try pasting the code into the file I have been using most of the time and change the relevant column and row references, it works fine.
=IF(ISERROR(MATCH(1,INDEX((INT('November-2017.csv'!A$2:A$10000)=A3)*('November-2017.csv'!L$2:L$10000=D3),),0)),"",INDEX('November-2017.csv'!P$2:P$10000,MATCH(1,INDEX((INT('November-2017.csv'!A$2:A$10000)=A3)*('November-2017.csv'!L$2:L$10000=D3),),0)))
The only differences appear to be in the row to check the date (new file date is in A107925 vs A3) and the column and row to check the name (new file is K107925 vs D3).
Any help gladly accepted, as it is doing my head in.
cheers
Shane

Related

How to Append CSV file via VBA without reading empty formatted cells

I have a code that is able to Append a range of data in a worksheet to an existing CSV file. However, I noticed that when the existing CSV file contains empty, but formatted cells beyond the end of the data range, the Append function takes into account those empty cells as well.
For example, the existing CSV file has 10 rows of data, while rows 11 to 20 are empty, but has been formatted (e.g. as "dd-mmm-yy"). So when I append the CSV file, the new data is added at row 21 instead of row 11.
Apart from manually deleting rows 11 to 20 in the CSV file, is there a quick fix to this? I have many existing CSV files, so it is not feasible to do the manual way. As to why some of the rows are empty, but formatted, it is due to some earlier amendments that took place.
Appreciate the help.
I have a code that is able to Append a range of data in a worksheet to an existing CSV file. However, I noticed that when the existing CSV file contains empty, but formatted cells beyond the end of the data range, the Append function takes into account those empty cells as well.
If you've written that code, it needs to account for the formatted cells you want to ignore. Let's say you're exchanging data from a .xlsx file to a .csv file and all of this is being done inside the Excel platform. Then for example, if "General" is the format that works best for you, but some trailing cells in .csv column A, Sheet 1 are not that, then have your code evaluate on a loop whether CSV Worksheets("Book1").Range("A[whatever row]").NumberFormat = "General"
Then have your code change the ones that aren't, to General; then proceed with the append.

OpenXML Excel file corruption, DataValidations

I am creating an excel file using OpenXML SDK 2.0 from a WPF application. The excel file has multiple sheets, data validations for dropdown cells, Formulas, and StyleSheets applied. It works fine creating excel rows of around 17,500 in Sheet1 and 17,500 in Sheet2 as well. However, when the number of rows reaches 25,000 for both Sheet1 and Sheet2 the file gets corrupted i.e. while opening the file it gives an error "We found a problem with the content in file.xlsx. Do you want to recover as much as we can?" If I recover the file the data is still there for all 25,000 rows in both the sheets however the datavalidations i.e. dropdowns are not applied for any of the cells. The recovery log is as follows, while the repair message in the screenshot.
error946360_01.xmlErrors were detected in file 'D:\file.xlsx'Removed Feature: Data validation from /xl/worksheets/sheet.xml partRepaired Records: Cell information from /xl/worksheets/sheet.xml partRepaired Records: Cell information from /xl/worksheets/sheet2.xml part
For me, checking if a row is already existing before adding it did the trick.
Somehow, in the xml schema of the xlsx file (for me it was the sheet1.xml), rows were already existing. After running my code, I added a row with an index, which was already existing.
So try to open your excel with winrar, find sheet1.xml (and sheet2.xml in your case) and see if you have duplicated index for rows.
Generate the excel file with your code and don't open it with excel, otherwise excel will remove the duplicates and you won't see the problem.
Another option would be to validate your excel file (before opening) with the Open Xml SDK 2.5 Productivity Tool

Excel Save CSV without blanks

I have an Excel spreadsheet that generates CSV scripts used in an application. The scripts must be in a very specific format, and I save a master in XLSX format with protected sheets and data validation to save the CSVs from rather than directly edit the CSVs, as directly editing the CSVs can lead to mistakes.
The issue is that the scripts can be of nearly any length. The left column of each line can only be one of a certain set of values, and the last line has to say "END". The only way I can do this without VBA is the following formula in the A column, from row 7 (the first 6 are header information) to row 1048576 (last Excel row) and protect the sheet with column A locked:
=IF(AND(ISBLANK(B368),NOT(ISBLANK(B367))),"END",IF(ISBLANK(B368),"",A367))
This makes the last row say "END" in column A, and all rows after blank, which is what is desired. The problem is that now when the CSV file is saved, it will always have 1048576 rows, with all the bottom rows containing the delimiters ",,,," . This won't work, the CSV file needs to stop after the "END" row. Is there a way to write the formula that will cause Excel to ignore the cells which evaluate to blank when saving to CSV or an alternate way to save to CSV in Excel that will ignore all the rows that evaluate to blank?
Note: I have a solution in VBA already that I can use on my own machine (it copies the data up to "END", pastes in a new sheet in text only format, then saves as CSV with the name of the original worksheet). I want to share this sheet, however, and getting around the security constraints to share macros at my company is a pain. So I'm looking for a way this might be done without Macros, if it's possible at all.
In looking for an answer I found this link, which is similar, but not the same:
Saving Excel data as csv with VBA - removing blank rows at end of file to save
As the "blanks" I have are active rows because they contain formulas, this method will not work.
Manually deleting the rows / columns will work to reset the size, as GSerg noted in the other question. Alternatively, also as suggested by GSserg, you can copy the data to a new sheet before saving.
Otherwise, an easy fix might be to create a small post-excel / pre-processing script - perhaps using a batch file - Batch / Find And Edit Lines in TXT file - or a similar solution in any small scripting language to remove the extra rows.

Import csv to excel with headers in last row

I have daily csv files that I need to create reports from. The headers for the csv are in the last row, the first row is a discard-able comment.
I want to be able to open my workbook and run a macro that will:
Prompt me to select the appropriate csv file
Prompt me for a worksheet name and create a worksheet of that name
Pull in the data to the new worksheet
Cut the headers from the last row and replace the first row with them
(4) has been the sticking point. The files have an unknown number of rows so I need to identify that last row as part of the process. They also can vary the number of columns so I can't used a fixed header set.
Try to use this.
Cells.SpecialCells(xlLastCell).Address
You will get the Address of right bottom cell.

Make excel worksheet mirror worksheet in external file

How can I make a worksheet in my excel file mirror exactly the sheet of a different (closed) worksheet.
Basically, I have around 5 files with a lot of data the gets consolidated into a single dashboard with the important information. I then want to create a file (lets call it report) that has the 'main' sheet from each of these different files.
Ideally, the report file will not have to be altered, and when it is opened it will already have all the data needed.
Note: there are merged cells in the documents that are in variable positions.
response to comment #1:
By be there already, for example you can do a vlookup referencing other workbooks.. and the values of those vlookups will be correct when you open the file. First I thought I could just say cell A1 is file 1 = cell A1 in file 2, but the merged cells throw that off because it will not create merged cells to match
I found the following and it can be done in Excel VBA.
1) open consolidated workbook
2) Assuming that all the files you need to consolidate are in a given directory, you can obtain the list of all these files.
2.1) Traverse the list of files and open them one after the other
2.2) When a file is open, you might want to change the name of the sheet before moving it to the "report" file.
2.3) Close each file without saving them.

Resources