求教,将CSV文件转换成excel文件的问题。
如题,代码如下:程序代码:
Sub txttoexcel(txtfile As String, distancechar As String) '建立excel对象 Dim hang As Integer Dim XlApp As New Excel.Application Dim xlwb As New Excel.Workbook Dim xlst As New Excel.Worksheet Set XlApp = CreateObject("excel.application") Set xlwb = XlApp.Workbooks.Add xlwb.SaveAs FileName:=Left(txtfile, Len(txtfile) - 4) & ".xlsx" Set xlst = xlwb.Worksheets(1) '开始转换 Dim J As Integer, linenext As String, strb() As String J = 1 hang = 0 Open txtfile For Input As #1 Do Until EOF(1) Line Input #1, linenext hang = hang + 1 strb = Split(linenext, distancechar) For i = 0 To UBound(strb) xlst.Cells(J, i + 1) = strb(i) Next J = J + 1 Loop Close #1 '结束,释放空间 XlApp.Workbooks(1).Worksheets(1).Cells.HorizontalAlignment = xlCenter XlApp.Workbooks(1).Worksheets(1).Cells.VerticalAlignment = xlCenter XlApp.Workbooks(1).Worksheets(1).Cells.WrapText = False XlApp.Workbooks(1).Worksheets(1).Cells.Orientation = 0 XlApp.Workbooks(1).Worksheets(1).Cells.AddIndent = False XlApp.Workbooks(1).Worksheets(1).Cells.IndentLevel = 0 XlApp.Workbooks(1).Worksheets(1).Cells.ShrinkToFit = False XlApp.Workbooks(1).Worksheets(1).Cells.ReadingOrder = xlContext XlApp.Workbooks(1).Worksheets(1).Cells.MergeCells = False XlApp.Workbooks(1).Worksheets(1).Cells.EntireColumn.AutoFit XlApp.Workbooks(1).Worksheets(1).Range(Cells(1, 1), Cells(hang, 140)).Borders.LineStyle = xlContinuous xlwb.Save Set xlst = Nothing xlwb.Close Set xlwb = Nothing XlApp.Quit Set XlApp = Nothing End Sub
现在问题是
如果执行语句如下,可以正常运行:
程序代码:
Private Sub Command1_Click() txttoexcel App.Path & "\SUMMARY_Week.csv", "," End Sub
但如果执行语句如下,运行到这一条语句就会报错:
XlApp.Workbooks(1).Worksheets(1).Range(Cells(1, 1), Cells(hang, 140)).Borders.LineStyle = xlContinuous
程序代码:
Private Sub Command1_Click() txttoexcel App.Path & "\SUMMARY_Week.csv", "," txttoexcel Dir1.Path & "\SUMMARY.csv", "," End Sub
请教各位路过的大大们怎么会出错?如何修改?
[此贴子已经被作者于2022-12-16 16:24编辑过]