VBA print sheet to text file only works on active sheet

dappy

Board Regular
Joined
Apr 23, 2018
Messages
124
Office Version
  1. 2013
Platform
  1. Windows
Morning folks,

I wish to copy the contents of a particular sheet to a text file. I have this that works well but, that only works on the active sheet so i have to have the macro button on the sheet that i want to be copied to a text file. is it possible to have the macro on a seperate sheet that looks up the required sheet to copy? i've tried to add worksheet("textOP").range but it makes no difference

iLastRow = Worksheets("textOP").Range("A" & Rows.Count).End(xlUp).Row
iLastCol = Worksheets("textOP").Cells(1, Columns.Count).End(xlToLeft).Column
Open "h:\catm.txt" For Output As #1
For i = 1 To iLastRow
For j = 1 To iLastCol
If j <> iLastCol Then 'keep writing to same line
Print #1, Cells(i, j),
Else 'end the line
Print #1, Cells(i, j)
End If
Next j
Next i
Close #1
Shell "notepad.exe ""h:\catm.txt", vbNormalFocus


any advice much appreciated

thanks in advance
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try
For both Print#1
VBA Code:
Print #1, Worksheets("textOP").Cells(i, j),
 
Upvote 0
And
May
VBA Code:
Open "h:\catm.txt" For Output As #1
With Sheets("textOP")
    iLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    iLastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To iLastRow
        For j = 1 To iLastCol
            If j <> iLastCol Then    'keep writing to same line
                Print #1, .Cells(i, j),
            Else    'end the line
                Print #1, .Cells(i, j)
            End If
        Next j
    Next i
End With
Close #1
Shell "notepad.exe ""h:\catm.txt", vbNormalFocus
 
Upvote 0
Solution
Perfect, that's really does the job.

thank you so much for a swift resolution.
 
Upvote 0
You are very well come
And thank you for the feedback
Be happy
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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