Hi,
I would like to be able to import multiple delimited text files without always having to go through the wizard, i.e. I have a text file of data and have to constantly select delimited, then comma to get the wizard to import the text into individual columns. Everytime I open up the same type of file, I have to do this. Is there a way to get Excel to recognize that the file I want to open is the same as the ones I opened before it without having to go through the selection process??
Thanks.
1) Turn on the macro recorder.
2) Let it record you going through all the steps to import a text file to the format you want.
3) Do it again for a second file, just to be sure.
4) Turn off the macro recorder and post the resulting code here (between code tags, please, as shown in my signature)
We'll take a look and make a "dynamic" version you can run anytime to do it for you.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi Jerry,
Thanks for the instructions. I don't quite understand when you wrote to "post the resulting code here (between code tags, as shown in my signature"). How do I post the code, just copy and paste it in this reply with the words "code" before it? I am new to this forum so don't quite know the rules.
Thanks.
The Forum Rules are posted... see the link in the menu bar across the top of this page.
In my signature you see examples to the two "tags" you would need to add, one at the start of your pasted code, one at the end. The end result look like this:
Sub Macro1() 'my code is here End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi Jerry,
I have posted the code below (properly, I hope). I have some familiarity with Visual Basic and note that the lines indicating the directory and workbooks is very specific. Can you make it more general, i.e. the "F\Gloucester\PLC" is correct and can stay but I want to be able to select difference directories and different workbooks after that.
Thanks.
Sub Macro1() ' ' Macro1 Macro ' Macro recorded by Customer ' ' ActiveWindow.ScrollRow = 2 ChDir "F:\Gloucester\PLCData\1993\Dec93\12-93\H" Workbooks.OpenText Filename:= _ "F:\Gloucester\PLCData\1993\Dec93\12-93\H\DW1TO5.353.001", Origin:=xlWindows _ , StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=True, _ Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _ , 1), Array(4, 1), Array(5, 1), Array(6, 1)) Workbooks.OpenText Filename:= _ "F:\Gloucester\PLCData\1993\Dec93\12-93\H\DW6TO8.353.001", Origin:=xlWindows _ , StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=True, _ Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _ , 1), Array(4, 1), Array(5, 1)) End Sub
This will keep prompting you to select files and will abort when you do...
Option Explicit Sub ImportTextFiles() Dim fName As String, fPath As String fPath = "F:\Gloucester\PLCData\1993\Dec93\12-93\H\" 'starting directory Do With Application.FileDialog(msoFileDialogOpen) .InitialFileName = fPath .AllowMultiSelect = False .Filters.Add "All Files", "*.*" 'everything .Filters.Add "Text Files", "*.txt" 'text files .Filters.Add "001 Files", "*.001", 1 '001 files, the default .Show If .SelectedItems.Count > 0 Then fName = .SelectedItems(1) Else Exit Sub End If End With Workbooks.OpenText Filename:=fName, Origin:=xlWindows, StartRow:=1, _ DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _ Comma:=True, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _ Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)) Loop End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi Jerry,
I opened an excel file and copied your code into the macro. Not sure what to do after that
because I just tried to run the macro and I got an compile error message for the line"
It says variable (msoFileDialogOpen) not defined.Do With Application.FileDialog(msoFileDialogOpen)
Also, not exactly sure what to do with the macro. How do you get it to run as I am opening a file? Sorry, I am afraid I am a bit clueless when it comes to running macros in Excel. I have never done it before.
HI,
Here is something I use for this purpose. It was put together from snips provided by different forum members over time.
Hope it helps you.
Good luck.
abousetta
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
Sorry, CMORAL, I've been using that so long I forgot you needed to turn on an optional reference library in your Excel.
In the VBEDITOR, go to Tool > References and put a check-mark in the reference for:
Microsoft Office 11.0 Object Library
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Sorry, Jerry. Forgot to mention that I am still using Excel 2000 so there is no 11.0 Object library, Just the 9.0 Object library, which is checked. I have been thinking about getting Excel 2010 but haven't done so yet. Can you adjust the code to work with Excel 2000?
cmoral
Do you have a reference option for Microsoft Shell Controls and Automation?
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Or any chance this works?
Option Explicit Sub ImportTextFiles() Dim fName As String Do fName = Application.GetOpenFilename("001 Text Files(*.001), *.001") If fName = "False" Then Exit Sub Workbooks.OpenText Filename:=fName, Origin:=xlWindows, StartRow:=1, _ DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _ Comma:=True, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _ Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)) Loop End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi Jerry,
Yes. Thanks. That last bit of code worked nicely except I changed the code a bit so that I could open up all text files of the same type of data. Some have different extensions other than ".001"
One last question. How do I get to use the code for all other files I want to open? Do I have to always open the file containing that particular code to open the other files or is there some universal place I can store the code and then access it regardless of the files I open?
Thanks for all your help. I really appreciate it.Option Explicit Sub ImportTextFiles() Dim fName As String Do fName = Application.GetOpenFilename("Text Files(*.*), *.*") If fName = "False" Then Exit Sub Workbooks.OpenText Filename:=fName, Origin:=xlWindows, StartRow:=1, _ DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _ Comma:=True, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _ Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)) Loop End Sub
cmoral
Last edited by cmoral1266; 08-30-2011 at 11:03 AM.
1) fix the closing code tag in the post above. / instead of \
2) Macros stored in your Personal.XLS project (look in the other projects listed on the left, might be there already) are available at all times.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thanks Jerry for your help. You have resolved my problem beautifully.
cmoral
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks