Macro to Save excel work sheets as separate .prn files

porterrm

New Member
Joined
Aug 21, 2018
Messages
5
Hi, I searched the forum for this and didn't have much luck finding a discussion on this. I looking for a macro where I can save either one or multiple excel sheets in a workbook to individual .prn files. For example, if I have sheet 1, sheet 2, sheet 3. I want to run the macro and save sheet 2 as a .prn file and sheet 3 as a seperate .prn file. If someone could point me to an example macro or some resouce so I can figure this out I'd appreciate it. I am pretty green when it comes to macros. Thanks!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
hello,

try the following code in module of your workbook.
VBA Code:
Sub Create_PRN()
Dim wb As Workbook, ws As Worksheet

For Each ws In Worksheets
    shnum = ws.Index
        If shnum > 1 Then
            lr = Cells(Rows.Count, 1).End(xlUp).Row
            ActiveSheet.Range("A1:Z" & lr).SpecialCells(xlCellTypeVisible).Copy
            Set wb = Workbooks.Add
            wb.Sheets(1).Range("A1").PasteSpecial
            wb.SaveAs "C:\Sheet" & shnum & ".prn"
            wb.Close
        End If
    Next ws
End Sub

The code copies all the data from your worksheet and pastes it into a new workbook and saves it as sheetname.prn file in c:\ directory.
hope this helps...
 
Upvote 0
Hi Fadee2,

Thank you for the quick response. I tried your code and when I tried to open the .prn file all the text was scrambled and nonsensical characters. Is there anything I should do prior to running it?

Thanks
 
Upvote 0
hi porterrm,
Sorry for a delayed reply.

Well, I have no idea what these prn files really are and what application reads files with prn extension. As for the files created, in question, these files are simple excel files with prn extension. You can read these files using Ms. Excel (file > right click > open with > Excel), but if you open these files with notepad, you will mostly see ASCII Characters and Machine Language, this is the default behavior of notepad, as it cannot process the algorithms used by Excel to create that particular file.

Hope this helps.
 
Upvote 0
I tried to open the .prn file all the text was scrambled and nonsensical characters.

@porterrm, what kind of output are you expecting?
I am asking this because a PRN file contains a (binary) sequence of data intended for a specific printer. To be precise, the printer which is set as default in Excel. This can be different from the default printer in Windows. If that's what you want, you can use the code below. The PRN file is saved in the folder in which your workbook is located. Its file name consists of both workbook name and worksheet name. Note the separate function to ensure a valid file name.

VBA Code:
Public Sub SaveToPRN()

    Dim oWs         As Worksheet
    Dim sShtName    As Variant
    Dim arrShts     As Variant
    Dim sFileName   As String
    
    arrShts = Array("Sheet2", "Sheet5")   ' <<<  change to suit
    
    If Len(ThisWorkbook.Path) > 0 Then
        For Each sShtName In arrShts
            Set oWs = ThisWorkbook.Sheets(sShtName)
            sFileName = oWs.Parent.FullName & "__" & ValidateFileName(oWs.Name) & ".PRN"
            oWs.PrintOut Copies:=1, PrintToFile:=True, PrToFileName:=sFileName
        Next sShtName
    Else
        MsgBox "Save this workbook before running this code", vbExclamation
    End If
End Sub

Public Function ValidateFileName(ByVal argFileName As String) As String

    Const cUnwanted As String = "<>"":/\|?*"

    Dim sResult As String, i As Long
    sResult = argFileName
    For i = 1 To Len(cUnwanted)
        sResult = Replace(sResult, Mid(cUnwanted, i, 1), "_")
    Next
    ValidateFileName = sResult
End Function
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,923
Members
448,533
Latest member
thietbibeboiwasaco

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