I want to output a bill of materials (BoM) to CSV in a precise format ready for import. I have a functioning solution but it adds blank lines.
Due to the intended way of working, and the precise format of the output, I've had to translate the data across three sheets in Excel.
Firstly using an advanced filter copying to Sheet2, this copies the rows I need but also all of the columns which I don't need, additionally, the columns aren't in the required sequence.
Secondly copying only the required cells using =IF(Sheet2!C20="","",Sheet2!C20) into the sheet "StockIT", in the correct order ready for export. The formula copies from Sheet2 and IF null displays nothing but this hasn't stopped the lines being present in the .csv.
The problem I have is that to allow for different length BoMs I've entered 100 rows of these Excel formulas which are being seen when creating the .csv and turned into blank lines as: ,,,,,,, meaning every .csv output is 100 lines long.
How can I remove them?
Sub test()
Application.DisplayAlerts = False
Dim strSourceSheet As String
Dim strFullname As String
strSourceSheet = "StockIT"
strFullname = ("C:\Users\AlexA\Desktop\") & Range("B1").Text
ThisWorkbook.Sheets(strSourceSheet).Copy
ActiveWorkbook.SaveAs Filename:=strFullname, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
OP's solution:
"I've added the code to my original post, it runs with no warnings."
Sub test()
'Sub HideRowsUsingRanges()
Dim cell As Range, HideRows As Range
Dim StartRow As Long, EndRow As Long, Col As Long
'TARGET RANGE
Col = 9
StartRow = 3
EndRow = 100
'TARGET RANGE
Dim sh As Worksheet
Set sh = Sheets("StockIT")
Dim r As Range
'Saving the target range
Set r = sh.Cells(StartRow, Col).Resize(EndRow)
'Looping through each cell of the range
For Each cell In r
'If val or next val is empty
If Cells(cell.Row, Columns.Count).End(xlToLeft).Column = 1 And Cells(cell.Row, Columns.Count).End(xlToLeft) = "" Then
'If cell.Value = "" And cell.Offset(1).Value = "" Then
'Add the corresponding row to HideRows range
'Union causes an error if HideRows is nothing, so the first iteration cant use Union
If HideRows Is Nothing Then
Set HideRows = cell
Else
Set HideRows = Union(HideRows, cell)
End If
End If
Next cell
'Delete the gathered rows
If Not HideRows Is Nothing Then HideRows.EntireRow.Delete
'End Sub
'This works
Application.DisplayAlerts = False
Dim strSourceSheet As String
Dim strFullname As String
strSourceSheet = "StockIT"
strFullname = ("C:\Users\AlexA\Desktop\") & Range("B1").Text
ThisWorkbook.Sheets(strSourceSheet).Copy
ActiveWorkbook.SaveAs Filename:=strFullname, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub