简体   繁体   English

在将多个工作簿合并到一个工作簿之后,根据文件名重命名工作表

[英]Rename sheet based on file name after doing the merging multiple workbook into one workbook

I made this for my personal daily jobs. 我是为了个人日常工作而做的。 after I search on google, I found the code for merging the multiple workbooks (each has 1 worksheet) into one workbook. 在谷歌搜索后,我找到了将多个工作簿(每个工作表有1个)合并到一个工作簿中的代码。 and those worksheet have a same name it call "shXetnaXe", so when i try to select the workbooks, it ended up 和那些工作表有一个相同的名称,它称为“shXetnaXe”,所以当我尝试选择工作簿时,它最终

"shXetnaXe" for sheet(1)

"shXetnaXe(1)" for sheet(2)

"shXetnaXe(2)" for sheet(3)

And so on. 等等。

I want those sheets to automatically named as their original selected workbook's name those original names are: "1 sept" "2 sept" "3 sept" , I have try changing it a little bit, but it always fail. 我希望这些工作表自动命名为原始选定工作簿的名称,这些原始名称是:“1月9日”“2月9日”,“9月3日”,我尝试稍微更改它,但它总是失败。

Here's the code 这是代码

`Sub opensheets()
Dim openfiles
Dim crntfile As Workbook
Set crntfile = Application.ActiveWorkbook
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
openfiles = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls;*.xlsx),*.xls;*.xlsx", _
MultiSelect:=True, Title:="Select Excel file to merge!")

If TypeName(openfiles) = "Boolean" Then
    MsgBox "You need to select atleast one file"
    GoTo ExitHandler
End If

x = 1
While x <= UBound(openfiles)
    Workbooks.Open Filename:=openfiles(x)
    Sheets().Move After:=crntfile.Sheets(crntfile.Sheets.Count)
    Set rnmsht = Workbook.Open
    Sheets(openfiles) = rnmsht

    Before:=ActiveWorkbook.Sheets(openfiles.name)
    x = x + 1
Wend


Application.DisplayAlerts = False
Sheets(1).Select
ActiveWindow.SelectedSheets.Delete


ExitHandler:
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub' 

The problem is that openfiles.name returns the full file path and name of the file. 问题是openfiles.name返回文件的完整文件路径和名称。 You cannot name a worksheet with certain special characters, eg / , \\ or :. 您不能使用某些特殊字符命名工作表,例如/,\\或:。

Sub opensheets()
    Dim openfiles
    Dim xlWB As Workbook
    Dim NewSheetName as String
    Dim x As Integer
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    openfiles = Application.GetOpenFilename _
                (FileFilter:="Microsoft Excel Files (*.xls;*.xlsx),*.xls;*.xlsx", _
                 MultiSelect:=True, Title:="Select Excel file to merge!")

    If TypeName(openfiles) = "Boolean" Then
        MsgBox "You need to select atleast one file"
        GoTo ExitHandler
    End If

    x = 1
    While x <= UBound(openfiles)
        Set xlWB = Workbooks.Open(Filename:=openfiles(x))
        NewSheetName = xlWB.Name
        xlWB.Sheets(1).Move After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name = NewSheetName 

        x = x + 1
    Wend


'    Application.DisplayAlerts = False
'    Sheets(1).Select
'    ActiveWindow.SelectedSheets.Delete


ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

I changed your code on several spots. 我在几个地方改变了你的代码。 You can revert some of these changes very easily. 您可以非常轻松地还原其中一些更改。

Sub opensheets()
    Dim openfiles
    Dim crntfile As Workbook
    Set crntfile = Application.ActiveWorkbook
    Dim targetWkbk As Workbook
    Dim newName As String
    Dim x As Integer
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    openfiles = Application.GetOpenFilename _
                (FileFilter:="Microsoft Excel Files (*.xls;*.xlsx),*.xls;*.xlsx", _
                 MultiSelect:=True, Title:="Select Excel file to merge!")

    If TypeName(openfiles) = "Boolean" Then
        MsgBox "You need to select atleast one file"
        GoTo ExitHandler
    End If

    With crntfile
    x = 1
    While x <= UBound(openfiles)
        Set targetWkbk = Workbooks.Open(Filename:=openfiles(x))
        newName = targetWkbk.Name
        'you need this part if there are several (more than 1) worksheets
        'in your workbook, this might come in handy for later purposes
        'however, if it is always just one worksheet, delete the following parts
        'Line: For i = 1..
        'Line: Next
        'part  & " Sheet " & i
        For i = 1 To targetWkbk.Sheets.Count
            targetWkbk.Worksheets(i).Copy After:=.Sheets(.Sheets.Count)
            .Worksheets(.Sheets.Count).Name = newName & " Sheet " & i
        Next
        targetWkbk.Close
        x = x + 1
    Wend
    End With
ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

I deleted this part 我删除了这部分内容

Application.DisplayAlerts = False
Sheets(1).Select
ActiveWindow.SelectedSheets.Delete

It deleted the first worksheet of the current file. 它删除了当前文件的第一个工作表。 I wasn't sure if this was intended. 我不确定这是不是故意的。 If so put this line back in (at the same position) 如果是这样,把这条线放回(在同一位置)

crntfile.Worksheets(1).Delete

HTH HTH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复制工作表并创建主工作簿后重命名工作表 - Rename Sheet after it is copied and Create Master Workbook 不论工作表或工作簿名称如何,都将一个宏应用于另一工作表/工作簿 - Apply one macro to another sheet/workbook regardless of sheet or workbook name 将文件夹中的多个工作簿合并到一个文件中,每个工作簿作为单独的工作表,文件名 = 工作表名 - Excel VBA 宏 - Combine multiple workbooks in a folder into one file, every workbook as a separate sheet, file name = sheet name - Excel VBA macro excel VBA,在将多个 CSV 文件复制到一个工作簿时,在单元格中创建带有工作表或文件名的列 - excel VBA, create a column with sheet or file name in cells while copying multiple CSV files to one workbook 合并后用工作簿名称命名工作表吗? - Naming sheets with workbook name after merging? VBA基于另一工作簿中的工作表自动更新一个工作簿中的表 - VBA to automatically update a table in one workbook based on a sheet in a different workbook Aspose工作簿复制/重命名表 - Aspose workbook copy/rename sheet 将多个工作簿的第一张表合并为一个工作簿 - Combine first sheet of multiple workbooks into one workbook 将工作簿合并到主工作簿中,每个文件都有单独的工作表 - Merging workbooks into a master workbook with seperate sheet for each file 根据“主”工作表的名称引用另一个工作簿中的工作表 - Referencing a sheet in another workbook based on the 'master' sheet's name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM