我有将Access表导出到.csv的代码。但是,我不想在导出后尝试自动调整列。我得到一个“对象变量或没有设置块变量”。
还可以在同一程序中打开多个csv文件和自动调整吗?
以下是代码,我感谢您提供的任何帮助。
Function AutoFits()
Dim ObjFSO As Object
Dim ws As Worksheet
Set ObjFSO = CreateObject("scripting.filesystemobject")
ObjFSO.Opentextfile "C:\filename.csv"
With ObjFSO
ws("sheet1").Select
.Columns("A:O").Select
.Selection.Columns.AutoFit
.activeworkbook.Save
End With
ObjFSO.Quit
Set ObjFSO = Nothing
End Function
答案 0 :(得分:0)
以下内容应该有效。请注意,如果您以CSV格式保存,则无法保留任何格式(包括列宽),因此您需要保存xls文件。
Sub AutoFits()
Application.Workbooks.Open "C:\filename.csv"
ActiveSheet.Columns("A:O").Select
Selection.EntireColumn.AutoFit
ActiveWorkbook.SaveAs "C:\filename.xls"
End Sub
并遍历目录中的所有csv文件......
Function AutoFitsAllFiles()
Dim file As String
file = Dir("c:\*.csv")
While (file <> "")
Application.Workbooks.Open "c:\" & file
ActiveSheet.Columns("A:O").Select
Selection.EntireColumn.AutoFit
ActiveWorkbook.SaveAs "c:\" & Left(file, Len(file) - 3) & "xls"
ActiveWorkbook.Close False
file = Dir
Wend
End Function