我有一些代码可以将电子表格中的数据导出到逗号分隔的文件中。如果我在代码中的任何位置设置了断点,则数据会按预期导出到文件中。如果我没有断点,则创建的文件没有任何数据。认为这是一个时间问题,我在代码中尝试了一个等待周期,但这并没有解决问题。这是代码:
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 了。