VBA Saving Text file ends up with lots of extra commas everywhere.

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 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.

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:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This is a shot of what I'm faced with:

1613662061753.png
 
Upvote 0
check the value of LR. i suspect that way down your sheet somewhere there is a cell with a <space> or other character hiding
 
Upvote 0
I solved the problem. There was extra "stuff" in additional rows and some columns...once they were trashed, all was fine.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,400
Messages
6,119,289
Members
448,885
Latest member
LokiSonic

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