Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am just learning how to make macros and I found a macro that nearly does what I need it to do, which is output a text file from Excel.

What I need it to do is output this in a .mhd format, which I have done, and then take all the data written in the #fnum cells and place a return after each in the Excel file.

Essentially I just need all the data to have their a specific line in the text file. I am certain there is an elegant way to go about this, but I can't seem to get it.

Sub CreateFile()
Do While Not IsEmpty(ActiveCell.Offset(0, 1))
    MyFile = ActiveCell.Value & ".mhd"
    'set and open file for output
    fnum = FreeFile()
    Open MyFile For Output As fnum
    'use Print when you want the string without quotation marks
    Print #fnum, ActiveCell.Offset(0, 5); " " & ActiveCell.Offset(0, 6); " " & _
        ActiveCell.Offset(0, 7); " " & ActiveCell.Offset(0, 8); " " & _
        ActiveCell.Offset(0, 9); " " & ActiveCell.Offset(0, 10); " " & _
        ActiveCell.Offset(0, 11); " " & ActiveCell.Offset(0, 12); " " & _
        ActiveCell.Offset(0, 13); " " & ActiveCell.Offset(0, 14); " " & _
        ActiveCell.Offset(0, 15); " " & ActiveCell.Offset(0, 16); " " & _
        ActiveCell.Offset(0, 17); " " & ActiveCell.Offset(0, 18); " " & _
        ActiveCell.Offset(0, 19); " " & ActiveCell.Offset(0, 20); " " & _
        ActiveCell.Offset(0, 21); " " & ActiveCell.Offset(0, 22); " " & _
        ActiveCell.Offset(0, 23); " " & ActiveCell.Offset(0, 24); " " & _
        ActiveCell.Offset(0, 25); " " & ActiveCell.Offset(0, 26)
Close #fnum
ActiveCell.Offset(1, 0).Select
Loop
End Sub
share|improve this question
1  
I don't understand your question. What isn't working? Are you getting an error? –  Head of Catering Jun 11 '12 at 1:46
add comment

1 Answer 1

Your question is good because you have tried to solve your problem and have shown us what you have tried. Your question is bad because you do not say what is wrong. Your macro runs without error. I assume it does not do what you want but you do not tell us what you want.

I created a worksheet that matches your macro:

My example data

I took you code and made some minor changes:

  • I added Option Explicit and declared all the variables.
  • I added a statement to get the name of the folder containing my workbook and added this folder name to the open statement. Perhaps you include the path name in the worksheet and do not need this.

.

Option Explicit
Sub CreateFile()

  Dim fnum As Long
  Dim MyFile As String
  Dim PathCrnt As String

  PathCrnt = ActiveWorkbook.Path & "\"

  Do While Not IsEmpty(ActiveCell.Offset(0, 1))

    MyFile = PathCrnt & ActiveCell.Value & ".mhd"
    'set and open file for output
    fnum = FreeFile()
    Open MyFile For Output As fnum
    'use Print when you want the string without quotation marks
    Print #fnum, ActiveCell.Offset(0, 5); " " & ActiveCell.Offset(0, 6); " " & _
        ActiveCell.Offset(0, 7); " " & ActiveCell.Offset(0, 8); " " & _
        ActiveCell.Offset(0, 9); " " & ActiveCell.Offset(0, 10); " " & _
        ActiveCell.Offset(0, 11); " " & ActiveCell.Offset(0, 12); " " & _
        ActiveCell.Offset(0, 13); " " & ActiveCell.Offset(0, 14); " " & _
        ActiveCell.Offset(0, 15); " " & ActiveCell.Offset(0, 16); " " & _
        ActiveCell.Offset(0, 17); " " & ActiveCell.Offset(0, 18); " " & _
        ActiveCell.Offset(0, 19); " " & ActiveCell.Offset(0, 20); " " & _
        ActiveCell.Offset(0, 21); " " & ActiveCell.Offset(0, 22); " " & _
        ActiveCell.Offset(0, 23); " " & ActiveCell.Offset(0, 24); " " & _
        ActiveCell.Offset(0, 25); " " & ActiveCell.Offset(0, 26)
    Close #fnum
    ActiveCell.Offset(1, 0).Select
  Loop

End Sub

Your macro ran without errors and created a file per line. I agree it is not elegant but it works if this is what you want. I wonder if you want all the lines in a single file. If so you need the file open and close outside the loop.

Below I have tidied up your code but I have not changed what it does. I hope this helps. Come back if any of my explanations are unclear. If this does not give you enough information to solve your problem, you will have to explain more fully what is wrong with your macro.

Option Explicit
Sub CreateFile2()

  Dim ColStart As Long
  Dim ColCrnt As Long
  Dim FileLine As String
  Dim FileName As String
  Dim fnum As Long
  Dim MyFile As String
  Dim PathCrnt As String
  Dim RowStart As Long
  Dim RowLast As Long
  Dim RowCrnt As Long

  ' Your code starts at the active cell.  This relies on the user leaving
  ' the cursor in the correct cell of the correct worksheet.  I have left
  ' the macro like this but have made it more explicit.

  ColStart = ActiveCell.Column
  RowStart = ActiveCell.Row

  PathCrnt = ActiveWorkbook.Path & "\"

  ' My code does  not move the cursor and operates on the worksheet
  ' identified in the With statement.  I have used the active worksheet
  ' but I could have written 'With Worksheets("Sheet2")' or
  ' made the worksheet name a variable.
  With ActiveSheet

    ' Cells(R,C) identifies a cell within the active worksheet by its row
    ' and column number.
    ' .Cells(R,C) identifies a cell within the worksheet named in the With
    ' statement by its row and column number.
    ' Rows.Count gives the maximum row number in your version of Excel.
    ' This statement starts at the bottom of column ColStart, moves up until
    ' it reaches a cell with a value and returns its row number.
    ' With this I could write:
    '    For RowCrnt = RowStart to RowLast
    '       -----
    '    Next
    ' I have kept your style.  But I suggest you experiment with Ctrl+Up,
    ' which is the keyboard equivalent of this VBA and look up "End" in
    ' VBA help.
    RowLast = .Cells(Rows.Count, ColStart).End(xlUp).Row

    RowCrnt = RowStart

    Do While Not IsEmpty(.Cells(RowCrnt, ColStart).Value)

      ' This is not necessary; I can use .Cells(RowCrnt, ColStart).Value
      ' in the file open statment.  But this makes it clearer what I am
      ' doing.  When you need to update this macro in six or twelve
      ' months you will immediately see what the macro is doing.
      FileName = .Cells(RowCrnt, ColStart).Value

      ' Why the extension "MHD"?  It is easier to stick to standard
      ' extensions.
      MyFile = PathCrnt & FileName & ".mhd"
      'set and open file for output
      fnum = FreeFile()
      Open MyFile For Output As fnum

      ' There are lots of different ways of concatenating the cells in
      ' a row.  I will not claim this is the best but I think it is easy
      ' to understand.  Having it in a loop means it is easy to change
      ' the number of columns written to the file.
      FileLine = .Cells(RowCrnt, 6).Value
      For ColCrnt = 7 To 27
        FileLine = FileLine & " " & .Cells(RowCrnt, ColCrnt).Value
      Next
      Print #fnum, FileLine
      Close #fnum
      RowCrnt = RowCrnt + 1

    Loop

  End With

End Sub
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.