我打开它的工作簿VBA时,我想在循环期间将工作表名称更改为文件名

时间:2016-07-13 20:07:08

标签: excel vba excel-vba

我目前正在使用此代码打开文件夹中的所有.xls文件

Sub OpenFiles()

    Dim MyFolder As String
    Dim MyFile As String
    MyFolder = InputBox("Please enter the folder for files")
    MyFile = Dir(MyFolder & "\*.xls")
    Do While MyFile <> ""
        Workbooks.Open Filename:=MyFolder & "\" & MyFile
        ****Sheet1.Name = "MyFile"****
        MyFile = Dir
    Loop
End Sub

我正在尝试将工作表名称更改为文件名,因为它循环

我打开的每个工作表都将被称为&#34;零件清单&#34;

我正在尝试使用星号部分来执行此操作,但它不起作用。

2 个答案:

答案 0 :(得分:1)

这会将新打开的工作簿中的工作表(“部件列表”)重命名为MyFile。

Sub OpenFiles()
    Dim wb As Workbook
    Dim MyFolder As String
    Dim MyFile As String
    MyFolder = InputBox("Please enter the folder for files")
    MyFile = Dir(MyFolder & "\*.xls")
    Do While MyFile <> ""
        Set wb = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)

        wb.Worksheets("Parts List").Name = MyFile

        MyFile = Dir
    Loop
End Sub

答案 1 :(得分:0)

尝试将行更改为:

Sheets("Sheet1").Name = "MyFile"