Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Index of a For Loop to Name files

Status
Not open for further replies.

Ranf

Technical User
May 1, 2003
6
IL
Hi,
I have a working macro that open one file at a time (looks like "Alpha-111").
I would like to add 3 "For" Loops with three Indexes to name the numbers-part of that file name, since I have plethora of files like this one.

Do you find it Too Simple? well, I'll try to come out with something more chalanging next time...
I would appreciate any help.

Here is the beginning of this Macro:

Workbooks.OpenText Filename:= _
"C:\Documents and Settings\physics\My Documents\Ran\300-300-02-5E-6\isaOP1-300-300-LU-5E-6-323" _
, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
Array(Array(0, 1), Array(6, 1), Array(18, 1))
Workbooks.OpenText Filename:= _
"C:\Documents and Settings\physics\My Documents\Ran\300-300-02-5E-6\isaOP1-300-300-LU-02-323" _
, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
Array(Array(0, 1), Array(6, 1), Array(18, 1))
Windows("isaOP1-300-300-LU-5E-6-323").Activate
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\physics\My Documents\Ran\300-300-02-5E-6\isaOP1-300-300-LU-323-diff.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _he beginning of this macro
 
All you need is a bit of structure. Something like this should help you on your way:
[blue]
Code:
Sub ConvertMyFiles()
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 1 To 9
  For j = 1 To 9
    For k = 1 To 9
      ConvertOneFile "isaOP1-300-300-LU-5E-6-" & i & j & k
    Next k
  Next j
Next i
End Sub

Sub ConvertOneFile(AFileName As String)
Const PATH_NAME = "C:\Documents and Settings\physics\My Documents\Ran\300-300-02-5E-6\"
  Workbooks.OpenText FileName:=PATH_NAME + AFileName, _
        Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(6, 1), Array(18, 1))
  Windows(AFileName).Activate
  ActiveWorkbook.SaveAs FileName:=PATH_NAME + AFileName + "-diff.xls", _
        FileFormat:=xlNormal, Password:="", WriteResPassword:=""
End Sub
[/color]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top