提问人:user22571955 提问时间:9/16/2023 更新时间:9/16/2023 访问量:43
VBA代码将工作表保存到。生成空文件的 CSV 文件
VBA Code to save sheet to .CSV file producing empty files
问:
我有现有的代码要创建.CSV 文件,但很多时候,.CSV文件最终为空,但VBA代码运行没有问题。我正在尝试找到修复程序或更好的创建方法。Excel 工作表中的 CSV 文件。
' Create .CSV assumption files
Public Sub Create_File(ByVal fldr As String, ws As String, fname As String, cnn As ADODB.Connection)
Dim rs As ADODB.Recordset
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.CreateTextFile(fldr & fname & ".csv", True)
Set rs = New ADODB.Recordset
rs.Open "select * from [" & ws & "$];", cnn
'determine what the field names are
strFields = ""
For z = 0 To rs.Fields.Count - 1
strFields = strFields & rs.Fields(z).Name & ","
Next z
'last field has a "," after so remove it
strFields = Left(strFields, Len(strFields) - 1)
txtFile.WriteLine strFields
If rs.EOF And rs.BOF Then
Else
Do While Not rs.EOF
txtFile.Write rs.GetString(adClipString, , ",", vbCrLf, "")
Loop
End If
rs.Close
Set rs = Nothing
End Sub
答: 暂无答案
评论
rs.CursorLocation = adUseClient
rs.Open
ws
rs.MoveFirst