EXCEL VBA代码导出到文本文件只有在设置了断点时才起作用

我有一些代码将数据从电子表格导出到逗号文件。 如果我在代码的任何地方设置了一个断点,数据就会按照预期导出到文件中。 如果我没有断点,则创build文件时没有任何数据。 考虑到这是一个计时问题,我在代码中尝试了一个等待周期,但是这并没有解决问题。 这里是代码:

Private Sub WriteDataToFile() Dim i As Long Dim iLastRow As Integer Dim strFile As String Dim FSO As FileSystemObject Dim FSOFile As TextStream Dim strData As String Dim s As String strFile = "C:\Temp\DSGELIG.txt" ' Delete the file if it already exists DeleteFile (strFile) ' Determine the last row iLastRow = 50 For i = 2 To 65000 strData = Range("B" + CStr(i)) If Len(strData) < 1 Then iLastRow = i - 1 Exit For End If Next i ' Create the file system object Set FSO = New FileSystemObject Set FSOFile = FSO.OpenTextFile(strFile, ForWriting, True) For i = 2 To iLastRow strData = "" With Worksheets(1) 'Debug.Print Range("B" + CStr(i)) strData = """" ' Patient Name strData = strData + Range("B" + CStr(i)) strData = strData + """" + "," + """" ' SSN / Policy # strData = strData + Range("C" + CStr(i)) strData = strData + """" + "," + """" ' Birthdate strData = strData + CStr(Range("D" + CStr(i))) strData = strData + """" + "," + """" ' Admit Date strData = strData + CStr(Range("E" + CStr(i))) strData = strData + """" + "," + """" ' Admit Date - 2 strData = strData + CStr(Range("F" + CStr(i))) strData = strData + """" + "," + """" ' Account Number strData = strData + CStr(Range("G" + CStr(i))) strData = strData + """" + "," + """" ' Insurance Code strData = strData + Range("H" + CStr(i)) strData = strData + """" + "," + """" ' Financial Class strData = strData + Range("I" + CStr(i)) strData = strData + """" + "," + """" ' Location strData = strData + Range("J" + CStr(i)) strData = strData + """" ' Write the record to the file FSOFile.WriteLine (strData) End With Next i FSOFile.Close End Sub 

先谢谢你 – 自从我做了任何VBA以来,这已经有一段时间了。

我修改了一下Kittoes代码,如下所示:

 Sub Test() Dim FSO As New FileSystemObject Dim ts As TextStream Dim strFile As String, strData As String Dim iLastRow As Integer, i As Long, c As Long strFile = "C:\Temp\DSGELIG.txt" ' Determine the last row iLastRow = Cells(Rows.Count, 2).End(xlUp).Row ' Create the file system object Set ts = FSO.CreateTextFile(strFile, True) With Worksheets(1) For i = 2 To iLastRow strData = "" For c = 2 To 10 strData = "'" & strData & .Cells(i, c) & "'," Next c ' Write the record to the file without the last comma ts.WriteLine Left(strData, Len(strData) - 1) Next i End With ts.Close Set ts = Nothing Set FSO = Nothing End Sub 

我必须说,我从来没有使用FSO.CreateFile和其余。 我通常使用旧的Print #1语法。 我通常尽量避免任何额外的参考,如果我可以。

更新与我可能从头开始做这个:

 Option Explicit Sub Test() Dim FSO As New FileSystemObject Dim ts As TextStream Dim WS As Worksheet Dim arrData(9) As String Dim strFile As String, strData As String Dim iLastRow As Integer, i As Integer, j As Integer strFile = "C:\Temp\DSGELIG.txt" Set ts = FSO.CreateTextFile(strFile, True) Set WS = ThisWorkbook.Worksheets(1) iLastRow = WS.UsedRange.SpecialCells(xlCellTypeLastCell).Row With WS For i = 2 To iLastRow For j = 2 To 10 arrData(j - 2) = """" & CStr(.Cells(i, j)) & """" Next j strData = Join(arrData, ",") ts.WriteLine Left(strData, Len(strData) - 1) Next i End With ts.Close Set ts = Nothing Set FSO = Nothing End Sub 

您不需要删除该文件的代码,因为您可以在CreateTextFile()简单地将overwrite设置为true

我已经testing了这个,并没有遇到任何与文件写入问题。 我甚至试图创build一组数据,可能是基于您的原始导出相比,它工作得很好。