Excel macro to import text file

peter2047

Board Regular
Joined
Dec 31, 2005
Messages
70
Hi All,
I am a newbee here. Greatly appreciated if some expert here can guide me to code a Excel macro to import a text file. The excel and text files are in the same folder.

Thanks in advance.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Peter

The best way to begin would be to record a macro of you importing the text file to begin with, and then examine the resultant code. Use the macro recorder: Tools>Macro>Record New Macro and go from there.

Best regards

Richard
 
Upvote 0
Hi Parsnip,
Thanks. Manage to do it... Here is the macro.

Sub Macro4()
'
Workbooks.OpenText Filename:= _
"C:\Documents and Settings\Administrator\Desktop\Stock\5EE.SI.TXT", Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, _
1), Array(8, 1), Array(13, 1), Array(18, 1), Array(23, 1), Array(28, 1))
Cells.Select
Selection.Copy
Windows("Beta.xls").Activate
Cells.Select
ActiveSheet.Paste
Windows("5EE.SI.TXT").Activate
ActiveWindow.Close
End Sub

But I have a problem with the prompt from Excel asking me whether keep the large clipboard or not. Can I stop or disable that prompt ?
 
Upvote 0
Peter

If you include the line:

Code:
Application.DisplayAlerts = False

within your sub, that should stop you from seeing the clipboard warning.

Best regards

Richard[/code]
 
Upvote 0
Parsnip,
Thank you very much. It works.

One more question and I ready to start on my data compilation project. Is there a way to automatically locate the folder so I can easily move my project folder.

Is there a way to replace this statement ?
"C:\Documents and Settings\Administrator\Desktop\Stock\fileaname"

I am looking for an Excel macro command to detemine the current location.
 
Upvote 0
Peter

If you include the following code (this assumes that the text file name the same each time, and that the text file is held in the same folder as the excel workbook that contains the macro):


Code:
Sub Macro4() 
Dim strPath as String

strPath = ThisWorkBook.Path


Workbooks.OpenText Filename:= strPath & "/5EE.SI.TXT", Origin:= _ 
xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, _ 
1), Array(8, 1), Array(13, 1), Array(18, 1), Array(23, 1), Array(28, 1)) 
Cells.Select 
Selection.Copy 
Windows("Beta.xls").Activate 
Cells.Select 
ActiveSheet.Paste 
Windows("5EE.SI.TXT").Activate 
ActiveWindow.Close 
End Sub

It should work. Please let me know if you run into any problems.

Best regards

Richard
 
Upvote 0
Hi Richard,
Your code works perfectly. But need to change the forward slash to a back slash("\5EE.SI.TXT").

Thank you very much. It's almost mid-night here and approaching the new year.

I would like to wish you and your love ones a Happy New Year.
 
Upvote 0
Peter

You are welcome:)

Happy New Year to you and your family too!

Richard

PS: Would you believe I actually checked my path names to find out which way round the slash should be, and then went and wrote down completely the wrong one? It must be my age :confused:
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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