Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Looping a Macro

Hi there folks. I have a macro that will import .txt files into excel03. I
recorded it using the "record macro" feature. So, the original text file is
in the macro code.

Sub DirectoryClean()
'
' DirectoryClean Macro
' Macro recorded 6/12/2008 by PE GWINN
'
' Keyboard Shortcut: Ctrl+g
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and
Settings\Phil\Desktop\directory\directory.txt", _
Destination:=Range("A1"))
.Name = "aerobics"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(10, 7, 4, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

What I want to do is have the macro loop in that directory and import each
text file into a new worksheet tab. I have an answer on the tip of my tongue
but it just wont gel.

Thanks a lot.
Phil
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Looping a Macro

Hi,

This script will loop through your files in your dir and only deal with .txt
files.

You need to insert your code to create a newtab on each pass and insert your
data.

If you have any probs you can get me at



Dim mypath As String
Dim myfile As String

'mypath = "C:\Documents and Settings\Phil\Desktop\directory\"
mypath = "c:\"
myfile = Dir(mypath)
Do While myfile < ""
If Mid(myfile, Len(myfile) - 3, Len(myfile)) = ".txt" Then
'Your Code Here
MsgBox myfile
End If
myfile = Dir
Loop

PEGWINN wrote:
Hi there folks. I have a macro that will import .txt files into excel03. I
recorded it using the "record macro" feature. So, the original text file is
in the macro code.

Sub DirectoryClean()
'
' DirectoryClean Macro
' Macro recorded 6/12/2008 by PE GWINN
'
' Keyboard Shortcut: Ctrl+g
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and
Settings\Phil\Desktop\directory\directory.txt", _
Destination:=Range("A1"))
.Name = "aerobics"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(10, 7, 4, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

What I want to do is have the macro loop in that directory and import each
text file into a new worksheet tab. I have an answer on the tip of my tongue
but it just wont gel.

Thanks a lot.
Phil


--
Anthony Prescott
http://www.apofficesolutions.co.uk

Message posted via http://www.officekb.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Looping a Macro

Hi, Thank you for the response. I am not a programmer per se. Rather I am a
quick learner. I just have a clarification question.

In your code, there is a space for my code. Do I insert the code that was
generated via the macro recorder there?

I really appreciate the response since it will help me import about 150
files which will then allow me to categorise about 15000 total files. Thank
you again.

Phil
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro looping? klh84 Excel Worksheet Functions 3 February 26th 10 05:58 PM
Looping Macro Neil Excel Programming 5 June 13th 08 01:08 PM
Looping macro furanku Excel Programming 1 September 25th 05 07:25 AM
Need help with my looping macro Pete Excel Programming 1 March 17th 05 11:09 PM
Looping Macro Pete Excel Programming 3 February 18th 04 05:33 PM


All times are GMT +1. The time now is 11:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"