Converting Worksheets to Text File

TemiU

Board Regular
Joined
Dec 11, 2014
Messages
52
Hello Experts!

I am trying to convert several of the worksheets in my workbook to text files.

I tried saving each as a prn file, but some of the data was getting cut off.

Next I tried this:

Code:
'loop worksheets one by one, open file "WkshtName.prn" as #1 for output and call:
Sub WriteToFile(mySheet As Worksheet, intColCount As Integer, path As String)


Dim intRowNum As Integer, j As Integer
Dim strWholeLine As String
Dim myCell As Range
Dim prnFile2 As String




Application.ScreenUpdating = False
           For intRowNum = 1 To mySheet.UsedRange.Rows.Count
                    'only write this row if not hidden
                    
                    If Range("A1").Offset(intRowNum - 1, 0).Rows(1).Hidden = False Then
                      For j = 1 To intColCount
                                
                                'only write this column if not hidden
                                If Range("A1").Offset(, j - 1).Columns(1).Hidden = False Then
                                    Set myCell = Range("A1").Offset(intRowNum - 1, j - 1)
                                    myCell.Activate
                                    strWholeLine = strWholeLine & myCell.Text & PadBlanks(myCell.Width / 4 - Len(myCell.Text))
                                End If
                            
                      Next j
                      Print #1, strWholeLine
                      strWholeLine = ""
                    End If
            
              Next intRowNum
                
ErrExit:
    Close #1
    Application.ScreenUpdating = True
    Exit Sub
ErrHandle:
    MsgBox "Line: " & intRowNum & " Col: " & j & " Text: " & ActiveCell(intRowNum, j).Text
    MsgBox Err.number & ":  " & Err.Description
    Resume ErrExit
End Sub

This works for all of my worksheets except one. That worksheet randomly freezes in the middle, stopping the entire process. I have identified that it always freezes on one specific column, but the "freeze row" varies.

I never even get to the error handler - it just quits in the middle.

Any suggestions?

Thanks!
TemiU
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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