[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.
Re: Excel macro - help with loop / text file export
here is some basic code to do some of what you want, leaves you to find your start and end rows etc, also it puts the same number of fields into every line, even if they are empty
vb Code:
myarr = Range("b10:m83")
tmparr = Range("a10:a83")
For i = 1 To UBound(myarr, 1)
tmpstr = Space(17)
LSet tmpstr = tmparr(i, 1)
myarr(i, 1) = tmpstr & myarr(i, 1)
Next
Open "c:\temp\mytest.txt" For Output As 1
Print #1, join2(myarr, ",", vbNewLine)
Close 1
vb Code:
Function join2(myarr, chr1 As String, chr2 As String) As String
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Excel macro - help with loop / text file export
Originally Posted by palantir
RESOLVED. Thank you!
As a newer member I ask that if you consider your question to be resolved please help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.