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

kweaver

Well-known Member
Joined
May 8, 2018
Messages
1,583
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:

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

kweaver

Well-known Member
Joined
May 8, 2018
Messages
1,583
Office Version
  1. 365
  2. 2010
This is a shot of what I'm faced with:

1613662061753.png
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,273
Office Version
  1. 2010
Platform
  1. Windows
check the value of LR. i suspect that way down your sheet somewhere there is a cell with a <space> or other character hiding
 

kweaver

Well-known Member
Joined
May 8, 2018
Messages
1,583
Office Version
  1. 365
  2. 2010
I solved the problem. There was extra "stuff" in additional rows and some columns...once they were trashed, all was fine.
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,612
Messages
5,625,846
Members
416,138
Latest member
Pizzaman22

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
Top