Automate export...?
Posted by Mark on January 04, 2002 11:23 AM
Is there a command that will automatically save a SELECTED group of cels to a text file?
Can I invoke this command in a macro?
Posted by Russell Hauf on January 04, 2002 3:41 PM
Yes, you can. Select your cells and run the following macro:
Sub SaveAsText()Selection.Copy
ActiveWorkbook.SaveAs "MyFileName.txt", xlTextWindows
End Sub
Hope this helps,
Russell
Is there a command that will automatically save a SELECTED group of cels to a text file?
Posted by Russell Hauf on January 04, 2002 3:54 PM
Actually, that doesn't seem to work...sorry (more inside)
However, what you could do is add a new sheet in your workbook, copy the selected cells, save that sheet as a text file, then delete the sheet you just added.
Sorry about the confusion,
Russell
Yes, you can. Select your cells and run the following macro: Sub SaveAsText() ActiveWorkbook.SaveAs "MyFileName.txt", xlTextWindows End Sub : : Is there a command that will automatically save a SELECTED group of cels to a text file? : Can I invoke this command in a macro?
Posted by Ivan F Moala on January 04, 2002 4:31 PM
The is no command to do this however you can
use a macro sililar to this....just change
as required...........
It will save your Hard coded range as a textfile
to the Desk top.
Sub WriteRangeCellsText()
Dim MyRg As Range
Dim ocell As Range
Dim TxtToWrite As String
Set MyRg = Range("a1:D1")
For Each ocell In MyRg
TxtToWrite = TxtToWrite & ocell.Text
Next
Open "C:\Windows\Desktop\Test.txt" For Output As #1
Print #1, TxtToWrite
Close #1
End Sub
HTH
Ivan Is there a command that will automatically save a SELECTED group of cels to a text file?
Posted by Mark on January 04, 2002 7:23 PM
Thanks Ivan..but how do I importthe data back to excel.
Maybe using another macro..could you demonstrate
to me how to import the data back ?
thanks Ivan
The is no command to do this however you can use a macro sililar to this....just change as required........... It will save your Hard coded range as a textfile to the Desk top. Sub WriteRangeCellsText() Dim MyRg As Range Dim ocell As Range Dim TxtToWrite As String Set MyRg = Range("a1:D1") For Each ocell In MyRg TxtToWrite = TxtToWrite & ocell.Text Next Open "C:\Windows\Desktop\Test.txt" For Output As #1 Print #1, TxtToWrite Close #1 End Sub
HTH Ivan : Is there a command that will automatically save a SELECTED group of cels to a text file? : Can I invoke this command in a macro?
Posted by Ivan F Moala on January 05, 2002 3:37 AM
The text file was written purely as a string file
No delimiter.
To load back into you array / range you should
have a delimiter eg space,comma etc.
So........following the reverse.
Routine should look like this;
Option Explicit
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim x As Integer
Dim MyRg As Range
Dim ocell As Range
Dim TxtToWrite As String
Sub ReadRangeCellsText()
Set MyRg = Range("a1:D1")
'set File's Name
FileName = "C:\Windows\Desktop\Test.txt" 'For Output As #1
'Get Next Available File Handle Number
FileNum = FreeFile()
'Open Text File For Input
Open FileName For Input As #FileNum
x = 1
'Loop Until the Length Of File Is Reached
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
MyRg(1, x) = ResultStr
x = x + 1
Loop
'Close The Open Text File
Close FileNum
End Sub
Sub WriteRangeCellsText()
Set MyRg = Range("A1:D1")
Open "C:\Windows\Desktop\Test.txt" For Output As #1
For Each ocell In MyRg
TxtToWrite = ocell.Text
Print #1, TxtToWrite
Next
Close #1
End Sub
Thanks Ivan..but how do I importthe data back to excel. Maybe using another macro..could you demonstrate to me how to import the data back ? thanks Ivan
Posted by Mark on January 05, 2002 4:19 AM
Thanks again Ivan for the prompt reply...
But a few more questions I want to ask..
Let say my range : a1:d1
where a1 is a , b1 is b , c1 is c and d1 is d
Using your export code the data will be together
like : abcd..it suppose to be like : a b c d ( all in different cells )
The same happened when I try to import the data..
Ivan..could you fixed the code and it will be exported
and imported to a different cells..thanks again Ivan
The text file was written purely as a string file No delimiter. To load back into you array / range you should have a delimiter eg space,comma etc. So........following the reverse. Routine should look like this; Option Explicit Dim ResultStr As String Dim FileName As String Dim FileNum As Integer Dim x As Integer Dim MyRg As Range Dim ocell As Range Dim TxtToWrite As String Sub ReadRangeCellsText() Set MyRg = Range("a1:D1") 'set File's Name FileName = "C:\Windows\Desktop\Test.txt" 'For Output As #1 'Get Next Available File Handle Number FileNum = FreeFile() 'Open Text File For Input Open FileName For Input As #FileNum x = 1 'Loop Until the Length Of File Is Reached Do While Seek(FileNum) <= LOF(FileNum) Line Input #FileNum, ResultStr MyRg(1, x) = ResultStr x = x + 1 Loop 'Close The Open Text File Close FileNum End Sub Sub WriteRangeCellsText() Set MyRg = Range("A1:D1") Open "C:\Windows\Desktop\Test.txt" For Output As #1 For Each ocell In MyRg TxtToWrite = ocell.Text Print #1, TxtToWrite Next Close #1 End Sub
: Thanks Ivan..but how do I importthe data back to excel. : Maybe using another macro..could you demonstrate : to me how to import the data back ? : thanks Ivan
Posted by Ivan F Moala on January 05, 2002 5:27 AM
If you have a look @ code supplied I have changed
it slightly so it does this.
NB: range reads and write is hardcoded
you may have to change to suit
ivan Thanks again Ivan for the prompt reply... But a few more questions I want to ask.. Let say my range : a1:d1 where a1 is a , b1 is b , c1 is c and d1 is d Using your export code the data will be together like : abcd..it suppose to be like : a b c d ( all in different cells ) The same happened when I try to import the data.. Ivan..could you fixed the code and it will be exported and imported to a different cells..thanks again Ivan The text file was written purely as a string file : No delimiter. : To load back into you array / range you should : have a delimiter eg space,comma etc. : So........following the reverse. : Routine should look like this; : Option Explicit : Dim ResultStr As String : Dim FileName As String : Dim FileNum As Integer : Dim x As Integer : Dim MyRg As Range : Dim ocell As Range : Dim TxtToWrite As String : Sub ReadRangeCellsText() : Set MyRg = Range("a1:D1") : 'set File's Name : FileName = "C:\Windows\Desktop\Test.txt" 'For Output As #1 : 'Get Next Available File Handle Number : FileNum = FreeFile() : 'Open Text File For Input : Open FileName For Input As #FileNum : x = 1 : 'Loop Until the Length Of File Is Reached : Do While Seek(FileNum) <= LOF(FileNum) : Line Input #FileNum, ResultStr : MyRg(1, x) = ResultStr : x = x + 1 : Loop : 'Close The Open Text File : Close FileNum : End Sub : Sub WriteRangeCellsText() : Set MyRg = Range("A1:D1") : Open "C:\Windows\Desktop\Test.txt" For Output As #1 : For Each ocell In MyRg : TxtToWrite = ocell.Text : Print #1, TxtToWrite : Next : Close #1 : End Sub :
Posted by Mark on January 05, 2002 10:12 AM
Hi again Ivan,
I've try it many times but it still exported
and imported in a single cell format : abcd
and not a b c d..what am i doing wrong, please
assist me. thanks
If you have a look @ code supplied I have changed it slightly so it does this. NB: range reads and write is hardcoded you may have to change to suit ivan : Thanks again Ivan for the prompt reply... : But a few more questions I want to ask.. : Let say my range : a1:d1 : where a1 is a , b1 is b , c1 is c and d1 is d : Using your export code the data will be together : like : abcd..it suppose to be like : a b c d ( all in different cells ) : The same happened when I try to import the data.. : Ivan..could you fixed the code and it will be exported : and imported to a different cells..thanks again Ivan : The text file was written purely as a string file