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.
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