Saveas Text but Remove Blank Rows?

shellp

Board Regular
Joined
Jul 7, 2010
Messages
188
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
Hello

I have a template where VBA imports the .csv file, filters accordingly and then VBA saves the specific worksheet as text file in a specified folder. The code below is the export code and it works well EXCEPT the resulting text file has lots of empty rows after the last line of data. How do I save only the rows of data? I've tried to copy only the rows with data by identifying 'last row' but it doesn't work either. Note that this can be achieved manually by selecting only the area of data you want and then saveas text but I would like to accomplish this via VBA. Thanks for any and all assistance.

VBA Code:
Sub copywsfromtemplate()
Dim newname As String
Dim nm As Name
Dim ws As Worksheet
Dim Nm_mnth As String
Dim Nm_year As String
Dim Nm_min As String
Dim Nm_max As String
Dim stype As String
Dim messtxt As String
Dim path_name As String
Dim chDrive_str As String
Dim dir_st As String

chDrive_str = "O:\"
dir_st = Sheets("General").Range("B2").Value

With Application
.ScreenUpdating = False
    
Nm_mnth = Sheets("General").Range("B6:B6").Value
Nm_year = Sheets("General").Range("B7:B7").Value
Nm_min = Sheets("General").Range("B11:B11").Value
Nm_max = Sheets("General").Range("B12:B12").Value

stype = Sheets("General").Range("B9:B9").Value

If Sheets("General").Range("B9:B9").Value = "MATIP" Then
path_name = Sheets("General").Range("B2:B2").Value
Else: path_name = Sheets("General").Range("B3:B3").Value
End If

newname = "Hospital" & "_" & stype & "_" & Nm_year & Nm_mnth & Nm_min & "-" & Nm_max

messtxt = stype & " survey exported to " & path_name

If IsEmpty(Sheets("Raw").Range("B2")) Then
MsgBox "No records have been imported", vbOKOnly
Else

Sheets("Raw").Copy
On Error GoTo 0

For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Copy
Application.CutCopyMode = False
ws.Activate
Next ws

ActiveWorkbook.SaveAs path_name & "\" & newname & ".txt", FileFormat:=xlText, _
        CreateBackup:=False
ActiveWorkbook.Close savechanges:=False
MsgBox messtxt, vbOKOnly
.ScreenUpdating = True
End If
End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,203,396
Messages
6,055,158
Members
444,766
Latest member
bryandaniel5

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