VBA to save rows as XLSX without empty rows

Jorgi

Board Regular
Joined
Jul 7, 2021
Messages
52
Office Version
  1. 2019
Platform
  1. Windows
HI All,

Could you be so kind and help me with this VBA code. The macro is working perfectly and does what needs to be done apart from saving a file in CSV but I would like to save it in XLSX, please? . If I change this:
"Open ThisWorkbook.Path & "\FilenameABC.csv" For Output As #F" to "Open ThisWorkbook.Path & "\FilenameABC.xlsx" For Output As #F" won't be able to open the saved file. Thank you for your help

Sub Demo1()
Dim F%, Rw As Range
F = FreeFile
Open ThisWorkbook.Path & "\FilenameABC.csv" For Output As #F
With Application
For Each Rw In ActiveSheet.UsedRange.Columns("B:AZ").Rows
If (Rw.Cells.Count - .CountBlank(Rw)) > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
Next
End With
Close #F
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I think you'll need to find a different approach. Here's one approach...

VBA Code:
Option Explicit

Sub Demo1()

    Dim foundCells As Range
    Dim rw As Range
    For Each rw In ActiveSheet.UsedRange.Columns("B:AZ").Rows
        If (rw.Cells.Count - Application.CountBlank(rw)) > 1 Then
            If foundCells Is Nothing Then
                Set foundCells = rw.Cells
            Else
                Set foundCells = Union(foundCells, rw.Cells)
            End If
        End If
    Next rw
    
    If foundCells Is Nothing Then
        MsgBox "No data found!", vbExclamation
        Exit Sub
    End If

    Dim newWorkbook As Workbook
    Set newWorkbook = Application.Workbooks.Add(xlWBATWorksheet)
    
    foundCells.Copy Destination:=newWorkbook.Worksheets(1).Range("A1")
    
    newWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\FilenameABC.xlsx", FileFormat:=xlOpenXMLWorkbook
    
    newWorkbook.Close

End Sub

Hope this helps!
 
Upvote 0
Solution
Thank you Domenic it works thank you so much but is there a possibility to save it without formatting / without colourful background, borders etc.,?
 
Upvote 0
To copy/paste values only, try replacing...

VBA Code:
foundCells.Copy Destination:=newWorkbook.Worksheets(1).Range("A1")

with

VBA Code:
    foundCells.Copy
   
    newWorkbook.Worksheets(1).Range("A1").PasteSpecial xlPasteValues

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,274
Messages
6,123,991
Members
449,137
Latest member
abdahsankhan

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