Excel 将目标工作簿命名为目标工作簿中的特定单元格

Excel 将目标工作簿命名为目标工作簿中的特定单元格,excel,vba,Excel,Vba,我从一个网站上获得了这段代码,它运行良好,完成了我需要的工作,即将活动工作簿中的每个工作表提取到一个文件夹中,并将这些工作表命名为与其来源的工作表相同的工作表 Sub Copy_Every_Sheet_To_New_Workbook() 'Working in 97-2013 Dim FileExtStr As String Dim FileFormatNum As Long Dim Sourcewb As Workbook Dim Destwb As Workb

我从一个网站上获得了这段代码,它运行良好,完成了我需要的工作,即将活动工作簿中的每个工作表提取到一个文件夹中,并将这些工作表命名为与其来源的工作表相同的工作表

Sub Copy_Every_Sheet_To_New_Workbook()
'Working in 97-2013
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim FolderName As String

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ThisWorkbook

    'Create new folder to save the new files in
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
    MkDir FolderName

    'Copy every visible sheet to a new workbook
    For Each sh In Sourcewb.Worksheets

        'If the sheet is visible then copy it to a new workbook
        If sh.Visible = -1 Then
            sh.Copy

            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook

            'Determine the Excel version and file extension/format
            With Destwb
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2013
                    If Sourcewb.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
                        GoTo GoToNextSheet
                    Else
                        Select Case Sourcewb.FileFormat
                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                        Case 52:
                            If .HasVBProject Then
                                FileExtStr = ".xlsm": FileFormatNum = 52
                            Else
                                FileExtStr = ".xlsx": FileFormatNum = 51
                            End If
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If
            End With

            'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    .Cells(1).Select
                End With
                Application.CutCopyMode = False
            End If


            'Save the new workbook and close it
            With Destwb
                .SaveAs FolderName _
                      & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                        FileFormat:=FileFormatNum
                .Close False
            End With

        End If
GoToNextSheet:
    Next sh

    MsgBox "You can find the files in " & FolderName

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub
我只需要稍微调整一下,将目标工作簿命名为工作表中某个特定单元格的名称

Sub Copy_Every_Sheet_To_New_Workbook()
'Working in 97-2013
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim FolderName As String

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ThisWorkbook

    'Create new folder to save the new files in
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
    MkDir FolderName

    'Copy every visible sheet to a new workbook
    For Each sh In Sourcewb.Worksheets

        'If the sheet is visible then copy it to a new workbook
        If sh.Visible = -1 Then
            sh.Copy

            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook

            'Determine the Excel version and file extension/format
            With Destwb
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2013
                    If Sourcewb.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
                        GoTo GoToNextSheet
                    Else
                        Select Case Sourcewb.FileFormat
                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                        Case 52:
                            If .HasVBProject Then
                                FileExtStr = ".xlsm": FileFormatNum = 52
                            Else
                                FileExtStr = ".xlsx": FileFormatNum = 51
                            End If
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If
            End With

            'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    .Cells(1).Select
                End With
                Application.CutCopyMode = False
            End If


            'Save the new workbook and close it
            With Destwb
                .SaveAs FolderName _
                      & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                        FileFormat:=FileFormatNum
                .Close False
            End With

        End If
GoToNextSheet:
    Next sh

    MsgBox "You can find the files in " & FolderName

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub
将每个工作表复制到新工作簿()
“97-2013年工作
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
将sh设置为工作表
将日期字符串设置为字符串
Dim FolderName作为字符串
应用
.ScreenUpdate=False
.EnableEvents=False
.Calculation=xlCalculationManual
以
'使用此宏复制工作簿中的每张工作表
设置Sourcewb=ThisWorkbook
'创建新文件夹以在其中保存新文件
DateString=格式(现在为“yyyy-mm-dd-hh-mm-ss”)
FolderName=Sourcewb.Path&“\”&Sourcewb.Name&“&DateString
MkDir FolderName
'将每个可见工作表复制到新工作簿
对于Sourcewb.工作表中的每个sh
'如果工作表可见,则将其复制到新工作簿
如果sh.Visible=-1,则
sh.副本
'将Destwb设置为新工作簿
设置Destwb=ActiveWorkbook
'确定Excel版本和文件扩展名/格式
用Destwb
如果Val(Application.Version)<12,则
“您使用的是Excel 97-2003
FileExtStr=“.xls”:FileFormatNum=-4143
其他的
“您使用的是Excel 2007-2013
如果Sourcewb.Name=.Name,则
MsgBox“您的答案在安全对话框中为否”
转到转到下一页
其他的
选择Case Sourcewb.FileFormat
案例51:FileExtStr=“.xlsx”:FileFormatNum=51
案例52:
如果.hasvb项目
FileExtStr=“.xlsm”:FileFormatNum=52
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
案例56:FileExtStr=“.xls”:FileFormatNum=56
其他情况:FileExtStr=“.xlsb”:FileFormatNum=50
结束选择
如果结束
如果结束
以
'如果需要,请将工作表中的所有单元格更改为值
如果Destwb.Sheets(1).ProtectContents=False,则
与Destwb.Sheets(1)一起使用
.细胞,复制
.Cells.paste特殊XLPaste值
.单元格(1)。选择
以
Application.CutCopyMode=False
如果结束
'保存新工作簿并关闭它
用Destwb
.SaveAs FolderName_
&“\”&Destwb.Sheets(1).Name&FileExtStr_
FileFormat:=FileFormatNum
.关闭错误
以
如果结束
GoToNextSheet:
下一个sh
MsgBox“您可以在”&FolderName中找到文件
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=xlcalculation自动
以
端接头

您只需更改保存工作簿的方式即可。您可以创建一个变量,并在保存之前将名称存储在其中。i、 e.我在下面的代码中使用
sFileName
,并假定名称存储在范围(“A1”)中,您可以根据需要进行更改

ub Copy_Every_Sheet_To_New_Workbook()
'Working in 97-2013
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim FolderName As String
    Dim sFileName As String

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ThisWorkbook

    'Create new folder to save the new files in
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
    MkDir FolderName

    'Copy every visible sheet to a new workbook
    For Each sh In Sourcewb.Worksheets

        'If the sheet is visible then copy it to a new workbook
        If sh.Visible = -1 Then
            sh.Copy

            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook

            'Determine the Excel version and file extension/format
            With Destwb
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2013
                    If Sourcewb.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
                        GoTo GoToNextSheet
                    Else
                        Select Case Sourcewb.FileFormat
                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                        Case 52:
                            If .HasVBProject Then
                                FileExtStr = ".xlsm": FileFormatNum = 52
                            Else
                                FileExtStr = ".xlsx": FileFormatNum = 51
                            End If
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If
            End With

            'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    .Cells(1).Select
                End With
                Application.CutCopyMode = False
            End If


            'Save the new workbook and close it
            'get name of workbook
            sFileName = sh.Range("A1").Value

            With Destwb
                .SaveAs FolderName _
                      & "\" & sFileName & FileExtStr, _
                        FileFormat:=FileFormatNum
                .Close False
            End With

        End If
GoToNextSheet:
    Next sh

    MsgBox "You can find the files in " & FolderName

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub
ub将每张工作表复制到新工作簿()
“97-2013年工作
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
将sh设置为工作表
将日期字符串设置为字符串
Dim FolderName作为字符串
将sFileName设置为字符串
应用
.ScreenUpdate=False
.EnableEvents=False
.Calculation=xlCalculationManual
以
'使用此宏复制工作簿中的每张工作表
设置Sourcewb=ThisWorkbook
'创建新文件夹以在其中保存新文件
DateString=格式(现在为“yyyy-mm-dd-hh-mm-ss”)
FolderName=Sourcewb.Path&“\”&Sourcewb.Name&“&DateString
MkDir FolderName
'将每个可见工作表复制到新工作簿
对于Sourcewb.工作表中的每个sh
'如果工作表可见,则将其复制到新工作簿
如果sh.Visible=-1,则
sh.副本
'将Destwb设置为新工作簿
设置Destwb=ActiveWorkbook
'确定Excel版本和文件扩展名/格式
用Destwb
如果Val(Application.Version)<12,则
“您使用的是Excel 97-2003
FileExtStr=“.xls”:FileFormatNum=-4143
其他的
“您使用的是Excel 2007-2013
如果Sourcewb.Name=.Name,则
MsgBox“您的答案在安全对话框中为否”
转到转到下一页
其他的
选择Case Sourcewb.FileFormat
案例51:FileExtStr=“.xlsx”:FileFormatNum=51
案例52:
如果.hasvb项目
FileExtStr=“.xlsm”:FileFormatNum=52
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
案例56:FileExtStr=“.xls”:FileFormatNum=56
其他情况:FileExtStr=“.xlsb”:FileFormatNum=50
结束选择
如果结束
如果结束
以
'更改中的所有单元格