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
I've attached the excel should anyone want to know more, and an example of what the txt file *should* look like when generated.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
VBA 6.3





Reply With Quote