我有几个工作簿,我已经开发了设备的大小调整。我通过 VBA 导出设计的细节到 CSV 文件,使其更容易加载回下面是代码。该脚本工作的大部分时间,但如果我尝试加载 CSV 文件后不久保存一个,Excel 有时崩溃,然后重新启动等我有 4 个不同的大小不同的设备的工作簿,每一个基于相同的 VBA 代码,他们都有。
需要注意的一点是,当我保存 CSV 文件时,我会在文件名的末尾添加文本,以识别大小的设备类型,并防止其被识别为 excel 文件。这是通过导出脚本完成的。例如“Filename.csv”被保存为“Filename.csv.WHRU”
"" "" "" "" "" "" "" "" "" "" "
工作表 (12)。激活
使用 Application.FileDialog (msoFileDialogFilePicker).Show 将 fStr 作为字符串
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
'fStr is the file path and name of the file
fStr = .SelectedItems(1)
End With
With ThisWorkbook.Sheets(12).QueryTables.Add(Connection:= _
"TEXT;" & fStr, Destination:=ThisWorkbook.Sheets(12).Cells(3, 7))
.Name = "CAPTURE"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
For Each Cn In ThisWorkbook.Connections
Cn.Delete
Next Cn
For Each Cn In Sheets(12).QueryTables
Cn.Delete
Next Cn
"" "" "" "" "" "" "" "" "" "" "
要从 CSV 文件中复制信息并将其直接从 VBA 放入 Excel 工作表中,您可以利用this project。该项目还允许您实际编写 CSV 文件。
试试这个:
Sub ImportCSVRecords(filePathAndName As String, OutputSheet As String, OutputRange As String)
Dim CSVix As CSVintece
Set CSVix = New CSVintece 'Create new instance
Call CSVix.OpenConnection(filePathAndName) 'Open a physical connection to the CSV file
Call CSVix.ImportFromCSV 'Import data
Call CSVix.DumpToSheet(WBookName:=ThisWorkbook.Name, SheetName:=OutputSheet, rngName:=OutputRange) 'Dumps the data to the current Workbook's OutputSheet starting at named OutputRange.
Set CSVix = Nothing 'Terminate the current instance
End Sub
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(79条)