简体   繁体   中英

Problom With VBA code throw an “user defined type not defined” error

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

The code has many several problems:

  1. You need to set a Reference

在此处输入图片说明

  1. use: Set fso = New FileSystemObject
  2. establish values for your variables before you use them; this includes TotalColumnNumber , SheetName , etc.

您是否在“工具/参考”中引用了正确的名称空间?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM