Results 1 to 5 of 5

Thread: [RESOLVED] Excel macro - help with loop / text file export

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Resolved [RESOLVED] Excel macro - help with loop / text file export

    I've tried to adapt an existing macro that converts excel to a txt file (fixedwidth). I want (as you can se below) to add a new variable, NumEntry, which adds an outer loop to the existing loop.

    The debug facility highlight the line "For MyRow = FirstRow To LastRow " though. I also moved the Open PageName For Output As #1 line too (probably to the wrong place, but in its original place it kept on saying a file was already open)

    I'm a noob but want to get this done for someone as it will save them sooo much time and I owe them

    Code:
    Sub MakeFixedWidth()
    Dim MyStr As String, PageName As String, FirstRow As Integer, LastRow As Integer, MyRow As Integer, NumEntry As Integer
    
    PageName = "C:\Modding\EDU_Unit" & Format(Time, "HHMM") & ".txt" ' location and name of saved file
    
    Open PageName For Output As #1
    For NumEntry = 1 To Range("B4").Value
    FirstRow = ((NumEntry * 25) - 25) + 10 ' gets first row of each edu entry per loop
    LastRow = FirstRow + 23 ' adds 23 rows to include all of entry
     For MyRow = FirstRow To LastRow ' loop through each row of the table
     MyStr = ""
     MyStr = Cells(MyRow, 1).Value & String(17 - Len(Cells(MyRow, 1).Value), " ") ' type + spacer
     MyStr = MyStr & Cells(MyRow, 2).Value ' adds type entry
     ' MyStr = MyStr & " " & Cells(MyRow, 3).Value & String(20 - Len(Cells(MyRow, 3).Value), " ")
     ' MyStr = MyStr & Cells(MyRow, 4).Value & String(15 - Len(Cells(MyRow, 4).Value), " ")
     ' MyStr = MyStr & Cells(MyRow, 5).Value & String(13 - Len(Cells(MyRow, 5).Value), " ")
     ' MyStr = MyStr & Cells(MyRow, 6).Value & String(25 - Len(Cells(MyRow, 6).Value), " ")
     ' MyStr = MyStr & Format(Cells(MyRow, 7).Value, "0000000.00")
     Print #1, MyStr
     Next MyRow
    Next NumEntry
    Close #1
    Sheets("DATA").Range("G2").ClearContents ' note that this row expects the worksheet to be named DATA
    Sheets("DATA").Hyperlinks.Add Range("G2"), PageName
    End Sub
    I've attached the excel should anyone want to know more, and an example of what the txt file *should* look like when generated.

    VBA 6.3
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width