Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 所以,我有6个“;大师;然后将文件分为40个单独的文件_Vba_Excel - Fatal编程技术网

Vba 所以,我有6个“;大师;然后将文件分为40个单独的文件

Vba 所以,我有6个“;大师;然后将文件分为40个单独的文件,vba,excel,Vba,Excel,我将简要描述我想要的: 我有6个“主”文件,每个文件包含40个工作表,如下所示: AG工作簿有HR Gp 1至HR Gp 40, ER工作簿有FB Gp 1至Gp 40等。所有工作表都已“平整” 我已经成功地创建了一个宏(使用Excel Mac 2011),该宏适用于一个组(下面是代码),但我未能成功地使其“循环” 如果您能帮我整理一下,我们将不胜感激 非常感谢,, 迈克 尝试这样的方式(尝试坚持你的风格/方法) 尝试这样的方式(尝试坚持你的风格/方法) 好吧,如果没有帕尔默小姐,我仍然会在黑暗

我将简要描述我想要的: 我有6个“主”文件,每个文件包含40个工作表,如下所示: AG工作簿有HR Gp 1至HR Gp 40, ER工作簿有FB Gp 1至Gp 40等。所有工作表都已“平整”

我已经成功地创建了一个宏(使用Excel Mac 2011),该宏适用于一个组(下面是代码),但我未能成功地使其“循环”

如果您能帮我整理一下,我们将不胜感激 非常感谢,, 迈克

尝试这样的方式(尝试坚持你的风格/方法)

尝试这样的方式(尝试坚持你的风格/方法)


好吧,如果没有帕尔默小姐,我仍然会在黑暗中(真的是黑色的),但我设法使它工作(代码如下),但没有我所展示的优雅。。。还是非常感谢她的帮助

Sub Macro4()

'turn off screen
With Application
'        .ScreenUpdating = False  only removed while testing
'        .EnableEvents = False
    '.Calculation = xlCalculationManual  disbled for the moment
End With

'get the path to desktop
Dim sPath As String
sPath = MacScript("(path to desktop folder as string)")

'give a name to new work book for macro use
Dim NewCaseFile As Workbook

'-------------------------------------------------
Dim strSheetNameAG As String
Dim strSheetNameER As String
Dim strSheetNameCS As String
Dim strSheetNameEV As String
Dim strSheetNameJD As String
Dim strSheetNamePG As String
'etc

'Dim intLoop As Integer
Dim i As Integer

For i = 1 To 40

'open new workbook
Set NewCaseFile = Workbooks.Add

    'set sheet names
    strSheetNameAG = "HR gp " & i
    strSheetNameER = "F&B gp " & i
    strSheetNameCS = "Acc gp " & i
    strSheetNameEV = "Mkt gp " & i
    strSheetNameJD = "Rdiv gp " & i
    strSheetNamePG = "Fac gp " & i
    'etc

    'move them across
        Windows("AG.xlsx").Activate
        Sheets(strSheetNameAG).Select
        Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1)
        Windows("ER.xlsx").Activate
        Sheets(strSheetNameER).Select
        Sheets(strSheetNameER).Move Before:=NewCaseFile.Sheets(1)
        Windows("CS.xlsx").Activate
        Sheets(strSheetNameCS).Select
        Sheets(strSheetNameCS).Move Before:=NewCaseFile.Sheets(1)
        Windows("EV.xlsx").Activate
        Sheets(strSheetNameEV).Select
        Sheets(strSheetNameEV).Move Before:=NewCaseFile.Sheets(1)
        Windows("JD.xlsx").Activate
        Sheets(strSheetNameJD).Select
        Sheets(strSheetNameJD).Move Before:=NewCaseFile.Sheets(1)
        Windows("PG.xlsx").Activate
        Sheets(strSheetNamePG).Select
        Sheets(strSheetNamePG).Move Before:=NewCaseFile.Sheets(1)

    'etc

'Save the created file for Group in use
 ActiveWorkbook.SaveAs Filename:=sPath & "gp " & i & ".xlsx", FileFormat:= _
   xlOpenXMLWorkbook, CreateBackup:=False
   ActiveWorkbook.Close False

Next i

'-------------------------------------------------

'turn screen back on
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

好吧,如果没有帕尔默小姐,我仍然会在黑暗中(真的是黑色的),但我设法使它工作(代码如下),但没有我所展示的优雅。。。还是非常感谢她的帮助

Sub Macro4()

'turn off screen
With Application
'        .ScreenUpdating = False  only removed while testing
'        .EnableEvents = False
    '.Calculation = xlCalculationManual  disbled for the moment
End With

'get the path to desktop
Dim sPath As String
sPath = MacScript("(path to desktop folder as string)")

'give a name to new work book for macro use
Dim NewCaseFile As Workbook

'-------------------------------------------------
Dim strSheetNameAG As String
Dim strSheetNameER As String
Dim strSheetNameCS As String
Dim strSheetNameEV As String
Dim strSheetNameJD As String
Dim strSheetNamePG As String
'etc

'Dim intLoop As Integer
Dim i As Integer

For i = 1 To 40

'open new workbook
Set NewCaseFile = Workbooks.Add

    'set sheet names
    strSheetNameAG = "HR gp " & i
    strSheetNameER = "F&B gp " & i
    strSheetNameCS = "Acc gp " & i
    strSheetNameEV = "Mkt gp " & i
    strSheetNameJD = "Rdiv gp " & i
    strSheetNamePG = "Fac gp " & i
    'etc

    'move them across
        Windows("AG.xlsx").Activate
        Sheets(strSheetNameAG).Select
        Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1)
        Windows("ER.xlsx").Activate
        Sheets(strSheetNameER).Select
        Sheets(strSheetNameER).Move Before:=NewCaseFile.Sheets(1)
        Windows("CS.xlsx").Activate
        Sheets(strSheetNameCS).Select
        Sheets(strSheetNameCS).Move Before:=NewCaseFile.Sheets(1)
        Windows("EV.xlsx").Activate
        Sheets(strSheetNameEV).Select
        Sheets(strSheetNameEV).Move Before:=NewCaseFile.Sheets(1)
        Windows("JD.xlsx").Activate
        Sheets(strSheetNameJD).Select
        Sheets(strSheetNameJD).Move Before:=NewCaseFile.Sheets(1)
        Windows("PG.xlsx").Activate
        Sheets(strSheetNamePG).Select
        Sheets(strSheetNamePG).Move Before:=NewCaseFile.Sheets(1)

    'etc

'Save the created file for Group in use
 ActiveWorkbook.SaveAs Filename:=sPath & "gp " & i & ".xlsx", FileFormat:= _
   xlOpenXMLWorkbook, CreateBackup:=False
   ActiveWorkbook.Close False

Next i

'-------------------------------------------------

'turn screen back on
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
最后的建议包括(工作手册而不是Windows…),下面的更新代码,测试和工作,非常感谢,Mike

Sub Macro4()

    'turn off screen
    With Application
        '        .ScreenUpdating = False  only removed while testing
        '        .EnableEvents = False
        '.Calculation = xlCalculationManual  disbled for the moment
    End With

    'get the path to desktop
    Dim sPath As String
    sPath = MacScript("(path to desktop folder as string)")

    'give a name to new work book for macro use
    Dim NewCaseFile As Workbook

    'Create sheet names
    Dim strSheetNameAG As String
    Dim strSheetNameER As String
    Dim strSheetNameCS As String
    Dim strSheetNameEV As String
    Dim strSheetNameJD As String
    Dim strSheetNamePG As String

    'Create loop counter variable
    'Dim intLoop As Integer
    Dim i As Integer

    For i = 1 To 40

        'open new workbook
        Set NewCaseFile = Workbooks.Add

        'set sheet names
        strSheetNameAG = "HR gp " & i
        strSheetNameER = "F&B gp " & i
        strSheetNameCS = "Acc gp " & i
        strSheetNameEV = "Mkt gp " & i
        strSheetNameJD = "Rdiv gp " & i
        strSheetNamePG = "Fac gp " & i

        'move them across
        Workbooks("AG.xlsx").Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("ER.xlsx").Sheets(strSheetNameER).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("CS.xlsx").Sheets(strSheetNameCS).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("EV.xlsx").Sheets(strSheetNameEV).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("JD.xlsx").Sheets(strSheetNameJD).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("PG.xlsx").Sheets(strSheetNamePG).Move Before:=NewCaseFile.Sheets(1)

        'Save the created file for Group in use
        ActiveWorkbook.SaveAs Filename:=sPath & "gp " & i & ".xlsx", FileFormat:= _
                              xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close False

    Next i

    '-------------------------------------------------

    'turn screen back on
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub
最后的建议包括(工作手册而不是Windows…),下面的更新代码,测试和工作,非常感谢,Mike

Sub Macro4()

    'turn off screen
    With Application
        '        .ScreenUpdating = False  only removed while testing
        '        .EnableEvents = False
        '.Calculation = xlCalculationManual  disbled for the moment
    End With

    'get the path to desktop
    Dim sPath As String
    sPath = MacScript("(path to desktop folder as string)")

    'give a name to new work book for macro use
    Dim NewCaseFile As Workbook

    'Create sheet names
    Dim strSheetNameAG As String
    Dim strSheetNameER As String
    Dim strSheetNameCS As String
    Dim strSheetNameEV As String
    Dim strSheetNameJD As String
    Dim strSheetNamePG As String

    'Create loop counter variable
    'Dim intLoop As Integer
    Dim i As Integer

    For i = 1 To 40

        'open new workbook
        Set NewCaseFile = Workbooks.Add

        'set sheet names
        strSheetNameAG = "HR gp " & i
        strSheetNameER = "F&B gp " & i
        strSheetNameCS = "Acc gp " & i
        strSheetNameEV = "Mkt gp " & i
        strSheetNameJD = "Rdiv gp " & i
        strSheetNamePG = "Fac gp " & i

        'move them across
        Workbooks("AG.xlsx").Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("ER.xlsx").Sheets(strSheetNameER).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("CS.xlsx").Sheets(strSheetNameCS).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("EV.xlsx").Sheets(strSheetNameEV).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("JD.xlsx").Sheets(strSheetNameJD).Move Before:=NewCaseFile.Sheets(1)
        Workbooks("PG.xlsx").Sheets(strSheetNamePG).Move Before:=NewCaseFile.Sheets(1)

        'Save the created file for Group in use
        ActiveWorkbook.SaveAs Filename:=sPath & "gp " & i & ".xlsx", FileFormat:= _
                              xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close False

    Next i

    '-------------------------------------------------

    'turn screen back on
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

因此,也许我的方法不是最好的,但你给了我一个更简洁的解决方案。我将移动保存并使用“I”来构建文件名,因为它也需要增加。非常感谢!将发布更新。所以,让女儿上床睡觉了!!,但宏在该点错误停止。工作表-找不到方法或数据成员,已尝试在strSheetNameAG周围加引号,但没有结果。请帮忙,今晚一定会成功的!我忘了把第二个strSheetNameAG换成strSheetNameER。。。可能是这样吗?我接着说,是.Sheet因为我不知道的原因被忽略了,改成了更长的语句(尽管另一个程序员说不要使用Select…),现在它可以工作了,所以非常感谢,因为手动移动240个Sheet很困难!发布解决方案,以便人们能够从中受益,并在必要时制定更简洁的解决方案。ttfn MikeAh,我明白这是因为我们使用的是“Windows”(我不熟悉),而不是“工作簿”。以下应该适用于工作簿(“AG.xlsx”).Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1),因此,也许我的方法不是最好的,但您已经向我展示了一个更简洁的解决方案。我将移动保存并使用“I”来构建文件名,因为它也需要增加。非常感谢!将发布更新。所以,让女儿上床睡觉了!!,但宏在该点错误停止。工作表-找不到方法或数据成员,已尝试在strSheetNameAG周围加引号,但没有结果。请帮忙,今晚一定会成功的!我忘了把第二个strSheetNameAG换成strSheetNameER。。。可能是这样吗?我接着说,是.Sheet因为我不知道的原因被忽略了,改成了更长的语句(尽管另一个程序员说不要使用Select…),现在它可以工作了,所以非常感谢,因为手动移动240个Sheet很困难!发布解决方案,以便人们能够从中受益,并在必要时制定更简洁的解决方案。ttfn MikeAh,我明白这是因为我们使用的是“Windows”(我不熟悉),而不是“工作簿”。以下应该是工作手册(“AG.xlsx”).Sheets(strSheetNameAG).Move Before:=NewCaseFile.Sheets(1)