0

I cannot figure out why the following code is throwing a compile error with the message "user defined type not defined". It is highlighting Set fso = FileSystemObject

Sub S()     
Dim fso As FileSystemObject
    Dim ts As TextStream
    Dim i As Integer
    Dim myCell As Range

    Set fso = FileSystemObject

    For i = 0 To TotalColumnNumber
       ' last argument, True, says to create the text file if it doesnt exist, which is
       ' good for us in this case
       Set ts = fso.OpenTextFile("column_" & i, ForWriting, True)

       ' set mycell to the first cell in the ith column
       Set myCell = SheetName.Cells(1, i)

       ' continue looping down the column until you reach a blank cell
       ' writing each cell value as you go
       Do Until myCell.Value = ""
           ts.writeline myCell.Value
           Set myCell = myCell.Offset(1, 0)
       Loop

       ts.Close
    Next

    Set ts = Nothing
    Set fso = Nothing


    End Sub

thanks

4
  • Move the line Sub S() to the top of your code. Commented Feb 16, 2019 at 14:35
  • @Gary'sStudent now Sub S() is highlighting :( Commented Feb 16, 2019 at 14:40
  • Try Set fso = New FileSystemObject Commented Feb 16, 2019 at 14:52
  • trumpexcel.com/vba-filesystemobject Commented Feb 16, 2019 at 14:53

2 Answers 2

2

The code has many several problems:

  1. You need to set a Reference

enter image description here

  1. use: Set fso = New FileSystemObject
  2. establish values for your variables before you use them; this includes TotalColumnNumber, SheetName, etc.
Sign up to request clarification or add additional context in comments.

Comments

0

Are you referencing the correct namespaces in Tools/References?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.