Save certain rows to a csv file

Elliottj2121

Board Regular
Joined
Apr 15, 2021
Messages
56
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello I am trying to copy rows to a new workbook and save that workbook as a CSV file. I am somewhat familiar with VBA and know how to copy rows to a different sheet within the same workbook but am stuck on figuring out how to move them to a new workbook and then save the new workbook as a CSV file.

Clarifying information:
If there is a value in column B then copy the entire row to the new workbook including the header row which is "A4:R4"
Columns are static between A & H but the number of rows can change.
The CSV file path can change so saving it to environ"USERPROFILE" is needed.

Thank you in advance.

VBA Code:
Private Sub CommandButton2_Click()
Dim xRg As Range, xCell As Range
Dim i As Long, j As Long, k As Long
Dim wsI As Worksheet, wsO As Worksheet
Dim wbI As Workbook, wbO As Workbook
Set wsI = Worksheets("CheckImport")
Set wbO = Workbook.Add
wsO = wbO.Worksheets("Sheet1")
i = wsI.Cells(Rows.Count, 1).End(xlUp).Row
j = wbO.worsheets("Sheet1").UsedRange.Rows.Count


Set xRg = wsI.Range("B5:B" & i)
On Error Resume Next
Application.ScreenUpdating = False

For k = 1 To xRg.Count
    If CStr(xRg(k).Value) <> "" Then
    xRg(k).EntireRow.Copy Destination:=wb0.Worksheets("Sheet1").Range("A" & j + 1)
    End If
Next
Application.ScreenUpdating = True

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

your code below with a few modifications / typo corrections. See if this gets you where you want to be. You might want to use a pathname to know where your new file will be saved also.
cheers
Rob

VBA Code:
Private Sub CommandButton2_Click()
Dim xRg As Range, xCell As Range
Dim i As Long, j As Long, k As Long
Dim wsI As Worksheet, wsO As Worksheet
Dim wbI As Workbook, wbO As Workbook

Set wsI = Worksheets("CheckImport")
Set wbO = Application.Workbooks.Add
Set wsO = wbO.Worksheets("Sheet1")

i = wsI.Cells(Rows.Count, 2).End(xlUp).Row
j = wbO.Worksheets("Sheet1").UsedRange.Rows.Count


Set xRg = wsI.Range("B5:B" & i)
On Error Resume Next
Application.ScreenUpdating = False

For k = 1 To xRg.Count
    If CStr(xRg(k).Value) <> "" Then
    'xRg(k).EntireRow.Copy Destination:=wb0.Worksheets("Sheet1").Range("A" & j + 1)
    wsO.Range("A" & j + 1).EntireRow.Value = wsI.Rows(i - xRg.Count + k).Value
    j = j + 1
    End If
Next
Application.ScreenUpdating = True

ActiveWorkbook.SaveAs Filename:="Output_File", FileFormat:=xlCSV

End Sub
 
Upvote 0
Solution
Hi,

your code below with a few modifications / typo corrections. See if this gets you where you want to be. You might want to use a pathname to know where your new file will be saved also.
cheers
Rob

VBA Code:
Private Sub CommandButton2_Click()
Dim xRg As Range, xCell As Range
Dim i As Long, j As Long, k As Long
Dim wsI As Worksheet, wsO As Worksheet
Dim wbI As Workbook, wbO As Workbook

Set wsI = Worksheets("CheckImport")
Set wbO = Application.Workbooks.Add
Set wsO = wbO.Worksheets("Sheet1")

i = wsI.Cells(Rows.Count, 2).End(xlUp).Row
j = wbO.Worksheets("Sheet1").UsedRange.Rows.Count


Set xRg = wsI.Range("B5:B" & i)
On Error Resume Next
Application.ScreenUpdating = False

For k = 1 To xRg.Count
    If CStr(xRg(k).Value) <> "" Then
    'xRg(k).EntireRow.Copy Destination:=wb0.Worksheets("Sheet1").Range("A" & j + 1)
    wsO.Range("A" & j + 1).EntireRow.Value = wsI.Rows(i - xRg.Count + k).Value
    j = j + 1
    End If
Next
Application.ScreenUpdating = True

ActiveWorkbook.SaveAs Filename:="Output_File", FileFormat:=xlCSV

End Sub
Thank you so much! This worked. I added a path and it works perfectly!!
 
Upvote 0
Great, thanks for the feedback. Glad we could help.

Rob
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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