的环境:
我想做什么:
问题:
我的代码:
Dim xlApp As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open("C:\Users\jfrick\Documents\BPM Access\Revenue Evolution GFS.xlsx")
With xlApp
.Application.Cells.Select
.Application.Selection.ClearFormats
.Application.Rows("1:1").Select
.Application.Selection.Font.Bold = True
.Application.Cells.Select
.Application.Selection.RowHeight = 12.75
.Application.Selection.Columns.AutoFit
.Application.Range("E:E").TextToColumns
.Application.Range("F:F").TextToColumns
.Application.ActiveWorkbook.Save
.Application.ActiveWorkbook.Close
.Quit
End With
我也尝试使用以下代码段,但这也只会更改第一张表中的列:
Dim ws As Worksheet
For Each ws In xlApp.Worksheets 'also tried xlSheet.Worksheets but same result
ws.Application.Range("E:E").TextToColumns
ws.Application.Range("F:F").TextToColumns
Next ws
- >如何在每张表格中的列上应用“textToColumn”功能?
答案 0 :(得分:1)
你几乎就在那里,只是一个简单的误解:
改变这个:
For Each ws In xlApp.Worksheets 'also tried xlSheet.Worksheets but same result
ws.Application.Range("E:E").TextToColumns
ws.Application.Range("F:F").TextToColumns
Next ws
对此:
For Each ws In xlApp.Worksheets
ws.Range("E:E").TextToColumns
ws.Range("F:F").TextToColumns
Next ws
由于您在尝试时引用ws.Application.Range
,因此您在应用程序对象上调用.Range
而不是工作表对象,而该工作表对象是指活动工作表,而不是{ {1}}