Unwanted empty rows in csv file

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I have a macro that imports data from a .csv file that contains no 'blank' lines. My code copies some of this data to a template sheet to put it into an order that my accounting software is expecting, and then saves that template as a new .csv file.
At the end of the new .csv file I am getting multiple 'blank rows', which are not visible in Excel, but when opened in a text editor shows
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,

I thought maybe my issue was that the template (which is cleared before each run of the code) was retaining blank rows.
To clear the previous data I use
VBA Code:
InvoiceRows = Xero.Sheets("Xero Sales Invoice").Range("A:A").SpecialCells(xlCellTypeLastCell).Row
Xero.Sheets("Xero Sales Invoice").Range("A2", "AA" & InvoiceRows).Delete
InvoiceRows is initially returned as 25, but an immediate re-check of InvoiceRows = Xero.Sheets("Xero Sales Invoice").Range("A:A").SpecialCells(xlCellTypeLastCell).Row after clearing or deleting shows InvoiceRows to still be 25.

How do I remove these extraneous empty rows?
 
The rows are indeed being written by a loop and XeroRow is being updated after each iteration. Each time I run the macro (on a new dataset) the number of rows changes, from 1 row of data to a dozen. I put a breakpoint on this last calculation of XeroRow and it is evaluating correctly.

Adding your ResetLastCell code has fixed the problem. I added these lines myself,
VBA Code:
tmp = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ActiveSheet.Range("A" & tmp + 1, "AA25").EntireRow.Delete
but I can see your solution is 'smart' and mine is 'dumb', or at least inflexible.

Thanks for your help and patience!
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top