Import csv to excel with headers in last row - excel

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.

Related

Import data from one excel file to another

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

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.

Column header validation with different sheets using macros

In Excel, I have 4 column headers (Id, Name, Age, Place).
I have 3 Excel worksheets with same column headers and different data.
I want to make a consolidated worksheet and the constraint is I have to check whether the column header is the same in all 3 worksheets and move it into the consolidated worksheet.
In the 1st worksheet I have values in the first row.
In 2nd worksheet, the first row is empty and the second row has data.
In 3rd worksheet, the first and second rows are empty and the 3rd row has data.
The 4th worksheet is the consolidated one. I want all 3 worksheet's data in this sheet.
How to do it with macros in vba?
There's built-in functionality for data consolidation.
You'll need to move your data so it's all on the same row but in the end will probably be much easier than writing code from scratch.
How to consolidate
Follow these steps to consolidate several worksheets into a master worksheet:
If you haven't already, set up the data in each constituent sheet by doing the following:
Ensure that each range of data is in list format. Each column must have a label (header) in the first row and contain similar data. There must be no blank rows or columns anywhere in the list.
Put each range on a separate worksheet, but don't enter anything in the master worksheet where you plan to consolidate the data. Excel will do this for you.
Ensure that each range has the same layout.
In the master worksheet, click the upper-left cell of the area where you want the consolidated data to appear.
Note: To avoid overwriting existing data in the master worksheet, ensure that you leave enough cells to the right and below this cell for the consolidated data.
Click Data → Consolidate (in the Data Tools group).
In the Function box, click the summary function that you want Excel to use to consolidate the data. The default function is SUM.
Here is an example in which three worksheet ranges have been chosen:
Select your data.
More information and the remaining instructions are at the source.
If you want to automate the process, record a macro as you performs the necessary steps and then you can edit as required.
See: Recording a Macro to Generate Code as well as Revising Recorded Visual Basic Macros. If you're not familiar with Excel (or whatever you're planning on automating with VBA), do that before attempting VBA.
This VBA tutorial is highly recommended: Excel VBA For Beginners and Microsoft's Documentation.

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.

VBA- Delete entire row in sheet 1

I have been using the following to delete a row.
Rows([2]).EntireRow.Delete
But this was when my workbook had only one sheet. A command button has been added in sheet2 which calls the macro present in sheet1.
How should I modify my above code so that the VBA deletes the second row of sheet1 ?
You only need to specify which which sheet the row is in, in addition to specifying the row as you already do. I.e.
Worksheets("Sheet1").Rows(2).EntireRow.Delete
Should do what you wish. If you have called the worksheet something different than "Sheet1", change what is inside the quotation marks. Strictly speaking you don't need to specify that it is the entire row you want to delete, as you've already said it is the row you are dealing with with Rows(2), but it shouldn't do any harm in there either.
If you additionally want to do the deletion from a different workbook (or just make sure that the deletion takes place in the correct workbook), you can also specify the workbook-name:
Workbooks("Workbookname.xlsm").Worksheets("Sheet1").Rows(2).EntireRow.Delete
If your button is in any other sheet and you wants to delete a row from another sheet, you simply have to activate that sheet. For example, you are trying to delete row from Sheet1 then you can activate the sheet by this one-liner.
Sheets(0).Activate

Resources