我正在尝试从另一个spredsheet打开一个spredsheet,以便它不在视图中。然后,我想使用TextToColumns功能更改其中一列的列格式。然后应保存更改并自动关闭文件。
当我运行以下内容时,它说没有选择数据来解析。有什么想法吗?
Sub Test()
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp.Visible = False
Set xlWB = xlApp.Workbooks.Open("directory of file")
Set xlWS = xlWB.Worksheets("Sheet 1")
xlWS.Unprotect
xlWS.Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Set xlWS = Nothing
xlApp.DisplayAlerts = False
xlWB.Close True
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
答案 0 :(得分:0)
一些研究应该给你结果,但无论如何我都会提供......
'Since you want the Workbook to be invisible, we have to open it in a new Excel Application
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp.Visible = False
'Open Workbook from specified path
Set xlWB = xlApp.Workbooks.Open("YOUR FILEPATH HERE")
'Select Worksheet from opened Workbook
Set xlWS = xlWB.Worksheets("YOUR WORKSHEET NAME HERE")
'Do something
'example
xlWS.Name = "Asdf"
'Cleanup
Set xlWS = Nothing
xlWB.Close 'True to save changes, False to discard changes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
答案 1 :(得分:0)
这应该让你开始......
Sub Test()
Dim wbk As Workbook
'Open the workbook that is closed
Set wbk = Workbooks.Open("C:\OtherWorkbook.xlsx")
'Change the format of the first column
wbk.Worksheets(1).Range("A:A").NumberFormat = "0.00"
'Close the workbook and save changes
wbk.Close True
End Sub