欢迎加我的QQ群:193522571,一起来讨论、交流!

EXCEL VBA中写了个宏把本EXCEL中的表数据批量导出为csv格式的文件

Sub csv()
    Dim Fs, myFile As Object
    Dim myfileline As String 'txtfile的行数据
    Dim sht As Worksheet
   
    For Each sht In ThisWorkbook.Sheets
        ns = sht.Cells(1, 8)
        Set Fs = CreateObject("Scripting.FileSystemObject")   '建立filesytemobject
        Set myFile = Fs.createtextfile(ActiveWorkbook.Path + "\csv\" + ns + ".csv") '通过filesystemobject新建一个和xls文件同名的txt文件
        For i = 2 To 1000
            ra = sht.Cells(i, 3)
            If ra = "" Then Exit For
            rb = ""
            For j = 3 To 1000
                ca = sht.Cells(2, j)
                If ca = "" Then Exit For
                If rb = "" Then
                    rb = sht.Cells(i, j).Value
                Else
                    rb = rb & "," & sht.Cells(i, j).Value
                End If
            Next j
            myFile.writeline (rb)
        Next i
        Set myFile = Nothing
        Set Fs = Nothing                   '关闭文件和filesystemobject对象
    Next
End Sub

posted @ 2015-05-31 10:03  swtool  阅读(6464)  评论(1)    收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!