range pasted to string

iknowu99

Well-known Member
Joined
Dec 26, 2004
Messages
1,158
Office Version
  1. 2016
I am trying to get the column besides the header to be exported to a text file. how to get the line to work:
Code:
strText= Range("C2:C" & Range("C:C").Rows)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Do you want the values in C2:Cx written in a single line in your text file, or as multiple lines? And how are you actually writing it to a text file?
 
Upvote 0
This works without the loop:

Code:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Sub[/COLOR] phoo()
 
    [COLOR=blue]Dim[/COLOR] arr()
    [COLOR=blue]Dim[/COLOR] rngText [COLOR=blue]As[/COLOR] Range
    [COLOR=blue]Dim[/COLOR] iFile [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]
    [COLOR=blue]Dim[/COLOR] strText [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], strFileName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
 
    [COLOR=blue]Set[/COLOR] rngText = Range("C2", Range("C" & Rows.Count).End(xlUp))
    arr = Application.Transpose(rngText)
    strText = [COLOR=blue]Join$[/COLOR](arr(), vbCrLf)
 
    iFile = [COLOR=blue]FreeFile[/COLOR]()
 
    strFileName = "C:/TestIt.txt"
    [COLOR=blue]Open[/COLOR] strFileName [COLOR=blue]For[/COLOR] [COLOR=blue]Output[/COLOR] [COLOR=blue]As[/COLOR] #iFile
        [COLOR=blue]Print[/COLOR] #iFile, strText
    [COLOR=blue]Close[/COLOR] #iFile
 
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0
like a charm because i was gettin lucky:P

it is breaking down when there are more than 32000 records...it only carries over so many characters to the text file...anybody know why?
 
Upvote 0
I didn't realise there were so many, so now lets try write it to the text file one at a time:
Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] phoo()
    [COLOR="Blue"]Dim[/COLOR] rngCell [COLOR="Blue"]As[/COLOR] Range
    [COLOR="Blue"]Dim[/COLOR] iFile [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] strFileName [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    
    iFile = [COLOR="Blue"]FreeFile[/COLOR]()
 
    strFileName = "C:/TestIt.txt"
    [COLOR="Blue"]Open[/COLOR] strFileName [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Output[/COLOR] [COLOR="Blue"]As[/COLOR] #iFile
        [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] rngCell [COLOR="Blue"]In[/COLOR] Range("C2", Range("C" & Rows.Count).End(xlUp))
            [COLOR="Blue"]Print[/COLOR] #iFile, rngCell.Value
        [COLOR="Blue"]Next[/COLOR] rngCell
    [COLOR="Blue"]Close[/COLOR] #iFile
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,405
Messages
6,171,925
Members
452,433
Latest member
Woodchuck76

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