kweaver
Well-known Member
- Joined
- May 12, 2009
- Messages
- 2,934
- Office Version
- 365
- 2010
I've use the same save routing for other files but now it's not working on my current file.
I have a sheet that I'm saving to a new workbook. No problem.
Then I save that workbook as a text file. No errors generated.
When I open the text file, there are hundreds of rows with no data but just commas and hundreds of columns (fields?) with commas including training commas after the existing real data.
When I start the application, I am deleting the existing rows created in the file that eventually is copied to a new workbook and then saved as a text file.
What's happening and how do I get around this?
This is the code I've been using and it's been working numerous times except now.
I have a sheet that I'm saving to a new workbook. No problem.
Then I save that workbook as a text file. No errors generated.
When I open the text file, there are hundreds of rows with no data but just commas and hundreds of columns (fields?) with commas including training commas after the existing real data.
When I start the application, I am deleting the existing rows created in the file that eventually is copied to a new workbook and then saved as a text file.
What's happening and how do I get around this?
This is the code I've been using and it's been working numerous times except now.
Code:
Sub saveSheetAsTextFile()
Dim ans As Long
Dim sSaveAsFilePath As String
Dim zNewWB As Workbook
Dim lastrow As Long
Dim Rng As Long
Dim LR As Long
ThisWBnew = ActiveWorkbook.Name
Application.ScreenUpdating = False
Worksheets("Reformatted").Visible = True
Worksheets("Reformatted").Activate
Range("A1").CurrentRegion.Select
LR = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo ErrHandler:
ThisWorkbook.Activate 'start macro in THIS workbook
Sheets("Reformatted").Range("A1:J" & LR).Select
sSaveAsFilePath = Sheets("Master").Range("M1").Value
If Dir(sSaveAsFilePath) <> "" Then
ans = MsgBox("File " & sSaveAsFilePath & " exists. Overwrite?", vbYesNo + vbExclamation)
If ans <> vbYes Then
Exit Sub
Else
Kill sSaveAsFilePath
End If
End If
Set zNewWB = Workbooks.Add 'create new workbook, ready for sheets to be added
ThisWorkbook.Sheets("Reformatted").Copy before:=zNewWB.Sheets(1) 'add sheet to workbook
'Sheets("Reformatted").Visible = "xlSheetVeryHidden"
' zNewWB.SaveAs sSaveAsFilePath, xlTextWindows '//Save as text (tab delimited) file
zNewWB.SaveAs sSaveAsFilePath, xlCSV '//Save as text (comma separated values) file
' zNewWB.SaveAs sSaveAsFilePath
' zNewWB.SaveAs sSaveAsFilePath
zNewWB.Close False
ThisWorkbook.Activate 'switch back to THIS file
My_Exit:
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume My_Exit
Application.ScreenUpdating = True
End Sub
Last edited: