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
308
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.
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,712
Office Version
  1. 2010
Platform
  1. Windows
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
 

jfarc

Active Member
Joined
Mar 30, 2007
Messages
308
That was it. I installed your code suggestion and it worked perfectly. Thank you!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,324
Messages
5,600,956
Members
414,417
Latest member
Nobu

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