Having error with 'Print #1, Join(Application.Transpose(Selection), vbCrLf)' line of code when a single cell is selected

jfarc

Active Member
Joined
Mar 30, 2007
Messages
316
I have been using the following code (and variations of) for 6 years now with no issues until now. This is where I got the code in the first place - http://www.mrexcel.com/forum/excel-...lected-range-save-txt-file-2.html#post2235815

This code takes data from a pre-Selected Range of Cells (ex: A1:A120) in a single column and saves the data to a .txt file named in the 'Open' line of the code.

Code:
Sub f()
    Open "C:\TEXTFILE.txt" For Output As #1
    Print #1, Join(Application.Transpose(Selection), vbCrLf)
    Close #1
End Sub

In this past the Selected Range has always included multiple cells in the single column. This time there is only data in a single cell, so the pre-Seleted Range is 'A1:A1'. When the above code runs , it errors out on the Print #1.... line of code with err code .

I can easily test to see if there is only a single cell Selected and perform a different line of code to save this single cell to the .txt file, but I'm not sure what the new line of code should be to take the place of the 'Print #1....' line above. Or of there was a new line of code to replace the 'Print #1...' line above that would execute when there are multi-cells selected or just a single cell selected.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The problem is you cannot Transpose a single cell. I think the following should work for you...
Code:
    Open "C:\TEXTFILE.txt" For Output As #1
    If Selection.Count = 1 Then
      Print #1, Selection
    Else
      Print #1, Join(Application.Transpose(Selection), vbCrLf)
    End If
    Close #1
 
Upvote 0
That was it. I installed your code suggestion and it worked perfectly. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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