Automatically import txt data and save.

excelent

Board Regular
Joined
Sep 7, 2002
Messages
105
Is it possible to automatically import text files,in which the data is separated by commas, then have it save automatically.

I can get an excel files to open and save automatically,for a simple task like deleting the first row.
But i need to add and sort and delete dates, but the date format is not recognised and i do not know if it is possible to call a function in a macro to solve this problem.
If i could import the data automatically from text files, this problem would be solved as i dont get a date format problem.


Thanks

Mike
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

It may be better to ask a specific question with an example of your code as to why the date format gives you a headache - I'd bet someone here could answer it for you.....

With reference to the importing of a text file try this.

Sub GetData()
Workbooks.OpenText FileName:=ThisWorkbook.Path & "\Yourfile.txt" _
, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier _
:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:= _
False, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array( _
1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1))
End Sub()

HTH

Regards

Kevin
 
Upvote 0
Thanks
It works well.


Is it possible to add your code in these lines

File_Names = Application.GetOpenFilename _
("txt (*.txt*), *.txt*", , , , True)

in the code below


Sub Open_Multiple_Files()


Const MyPath2 = "C:\Testing5\"
Dim Rng As Range
Dim fFName1 As String
Dim File_Names As Variant

Dim File_count As Integer

Dim Counter As Integer


Application.ScreenUpdating = False

File_Names = Application.GetOpenFilename _
("txt (*.txt*), *.txt*", , , , True)



File_count = UBound(File_Names)

Counter = 1

Do Until Counter > File_count

Workbooks.Open Filename:=File_Names(Counter)


Set Rng = Range("A1").CurrentRegion


FName2 = MyPath2 & Rng.Cells(3, 1).Text & "D.txt"

Rows("1:1").Select
Selection.Delete Shift:=xlUp

ActiveWorkbook.SaveAs Filename:=FName2, FileFormat:=xlText 'WorkbookNormal
ActiveWorkbook.Close Savechanges:=True
Counter = Counter + 1

Loop

Application.ScreenUpdating = True

End Sub


Thanks
Mike.
 
Upvote 0
excelent ...
I might be able to help if you could tell me what your ojective is in your second posting .
 
Upvote 0
Hi Nimrod.
I am trying to open text files automatically.

Then modify the data and have it save as a text file in the name of the text in column A, row 2.

Then save it, and open the next file, e.t.c. There are over 1300 files.


The text files are actually Stockmarket ticker files in which i am trying to fill in the holes of non trading days ( most likely due to trading halts).

I can then import these files into my stockmarket share program and create more accurate artificial composites.

I have been working on the code that finds these holes by using dates. Then fills the holes with the previous rows data.
But i am having problems in trying to open a text file without knowing the stock ticker data file names.

The code below will open files without knowing the file names, by selecting the files from the source folder, when running this code, and then saves it in a new folder automatically.
I can not open text files, which are separated by commas, properly into the sheet.


Sub Open_Multiple_Files()


Const MyPath2 = "C:\Testing5\"
Dim Rng As Range
Dim fFName1 As String
Dim File_Names As Variant

Dim File_count As Integer

Dim Counter As Integer


Application.ScreenUpdating = False

File_Names = Application.GetOpenFilename _
("txt (*.txt*), *.txt*", , , , True)



File_count = UBound(File_Names)

Counter = 1

Do Until Counter > File_count

Workbooks.Open Filename:=File_Names(Counter)


Set Rng = Range("A1").CurrentRegion


FName2 = MyPath2 & Rng.Cells(2, 1).Text & "D.txt"

Rows("1:1").Select
Selection.Delete Shift:=xlUp

ActiveWorkbook.SaveAs Filename:=FName2, FileFormat:=xlText 'WorkbookNormal
ActiveWorkbook.Close Savechanges:=True
Counter = Counter + 1

Loop

Application.ScreenUpdating = True

End Sub





This other code below would probably be able to open a text file, by adding the code Kevin Philips supplied into the appropriate area of the code.
At the moment it only opens and saves excel files.
To get this macro to open and save files i need to have a list of the file names in colA sheet1.

I would prefer not to use a list of file names to perform this task, but if this is not possible the code below would do.


At the moment these codes only open a file, deletes the first row (as a test only) and saves the data in a new folder.

Another question i have is when these new data files are opening, it opens a new workbook, in the name of the data file it opened e.g. AAID,
If my original workbook where i run my macro, needs to copy a column of dates and paste it into the newly opened workbook, e.g.AAID, do i need to select and activate workbook 1 copy the column of data , then select and activate the newly opened workbook by its correct name, or as workbook 2 to paste this data into a column.







Sub SaveRows2()
Const MyPath = "C:\sourcefile\"
Const MyPath2 = "C:\destinationfile\"
Dim Rng As Range
Dim x As Long
Dim FName As String
Set Rng = Range("A1").CurrentRegion
Application.ScreenUpdating = False
For x = 2 To Rng.Rows.Count Step 1
FName = MyPath & Rng.Cells(x + 1, 1).Text & "D.xls"

FName2 = MyPath2 & Rng.Cells(x + 1, 1).Text & "D.xls"

Workbooks.Open FName


Rows("1:1").Select
Selection.Delete Shift:=xlUp

ActiveWorkbook.SaveAs Filename:=FName2, FileFormat:=xlWorkbookNormal

ActiveWorkbook.Close Savechanges:=True


Next x

Application.ScreenUpdating = True
End Sub






If the text data could actually be imported, by importing external data into worksheet1, then use the macro to fix the holes in the data, and save it as the name of column A row 2 would be much easier than working between workbooks.


Thanks
Mike.
 
Upvote 0
Then you can automatically import your text data using the Data | Get External Data | Import Text File... menu command. Subsequently, your worksheet's resultant External Data range can be configured for automatic refresh.
 
Upvote 0

Forum statistics

Threads
1,221,481
Messages
6,160,080
Members
451,616
Latest member
swgrinder

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