将数据从一个工作簿中的工作表复制到另一个工作簿

时间:2017-05-26 12:28:18

标签: excel vba excel-vba

在工作簿中,例如Main.xlsx,我有多张表Main418, Main418_NotBilled, Main923, Main923_NotBilled

在另一个位置,我有两个工作簿,Wokbook_418.xlsxWokbook_923.xlsx。在Wokbook_418.xlsx中有三张Total, Sheet418, Sheet418_NotBilled。同样在Wokbook_923.xlsx中有三张Total, Sheet923, Sheet923_NotBilled

我的要求是拥有macro in Main.xlsx,需要将两个工作簿"Wokbook_418.xlsx" and "Wokbook_923.xlsx"复制到名为Wokbook_418_copy_26May.xlsx and Wokbook_923_copy_26May.xlsx respectively的其他位置。

现在进入" Wokbook_418_copy_26May.xlsx", - 不想触摸Total sheet。 - 想要替换表格中的数据" Sheet418"使用表格中的数据" Main418"。 (表" Main418"在Main.xlsx中可用) - 想要替换表格中的数据" Sheet418_NotBilled"使用表格中的数据" Main418_NotBilled"。 (表" Main418_NotBilled"在Main.xlsx中可用)

在" Wokbook_923_copy_26May.xlsx"中, - 不想触摸Total sheet。 - 想要替换表格中的数据" Sheet923"使用表格中的数据" Main923"。 (表" Main923"在Main.xlsx中可用) - 想要替换表格中的数据" Sheet923_NotBilled"使用表格中的数据" Main923_NotBilled"。 (表" Main923_NotBilled"在Main.xlsx中可用)

我在excel中没有这个级别的专业知识。

我需要一些指导(而不是确切的代码)才能实现此功能。

请帮忙。

2 个答案:

答案 0 :(得分:0)

从Main.xlsx工作簿中的宏中,您可以打开每个工作簿,从Main.xlsx中的工作表中复制信息,然后将编辑的工作簿保存到具有命名条件的新位置。例如:

Sub Macro1()

'opens the Workbook_418.xlsx file
Workbooks.Open Filename:="your_file_path_here\Workbook_418.xlsx"

'Activates your Main.xlsx workbook
Windows("Main.xlsx").Activate

'Selects your Main418 worksheet and copies the entire sheet
    Sheets("Main418").Select
    Cells.Select
    Selection.Copy

'Activates your Workbook_418.xlsx workbook
Windows("Workbook_418.xlsx").Activate

'Selects your Sheet418 worksheet and pastes the entire sheet
    Sheets("Sheet418").Select
    Cells.Select
    ActiveSheet.Paste

'Saves the Workbook_418.xlsx file with your new file name
    'formats a string to return the day & month
    Dim x As String
x = Day(Date) & MonthName(Month(Date))

Application.ActiveWorkbook.SaveAs Filename:="your_file_path_here\Workbook_418_" & x & ".xlsx", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub

答案 1 :(得分:0)

在上面的回复中使用@Dave提供的指导,我已经为所有工作表创建了它。我在这里发布解决方案只是为了帮助其他人:

Sub Macro1()


   Dim LCounter As Integer

   For LCounter = 1 To 4

        'BUNumber should be 417, 618, 641, 682
            If LCounter = 1 Then
                 BUNumber = 417
            ElseIf LCounter = 2 Then
                BUNumber = 618
            ElseIf LCounter = 3 Then
                BUNumber = 641
            ElseIf LCounter = 4 Then
                BUNumber = 682
            End If

        ''''''''''''''''''''''''''''''''''''' File Operation Start'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'opens the Reference_BU417.xlsx file
        Workbooks.Open Filename:="C:\Users\makumar\Desktop\file for mani\reference_files\Reference_BU" & BUNumber & ".xlsx"

        'Activates your Main sheet.xlsm workbook
        Windows("Main sheet.xlsm").Activate

            'Selects your 417 worksheet and copies the entire sheet
                Sheets("" & BUNumber & "").Select
                Cells.Select
                Selection.Copy

        'Activates your Reference_BU417.xlsx workbook
        Windows("Reference_BU" & BUNumber & ".xlsx").Activate

            'Selects your BU417 worksheet and pastes the entire sheet
                Sheets("BU" & BUNumber & "").Select
                Cells.Select
                ActiveSheet.Paste




        'Activates your Main sheet.xlsm workbook
        Windows("Main sheet.xlsm").Activate

            'Selects your 417_NotBilled worksheet and copies the entire sheet
                Sheets("" & BUNumber & "_NotBilled").Select
                Cells.Select
                Selection.Copy

        'Activates your Reference_BU417.xlsx workbook
        Windows("Reference_BU" & BUNumber & ".xlsx").Activate

            'Selects your BU417_NotBilled worksheet and pastes the entire sheet
                Sheets("BU" & BUNumber & "_NotBilled").Select
                Cells.Select
                ActiveSheet.Paste




        'Saves the Reference_BU417.xlsx file with your new file name
            With ActiveWorkbook
                .SaveCopyAs "C:\Users\makumar\Desktop\file for mani\generated_files\" & _
                            Left(.Name, InStrRev(.Name, ".") - 1) & _
                            "_" & Format(Date, "ddmmmyyyy") & ".xlsx"
            End With


        'closes the Reference_BU417.xlsx file
        Workbooks("Reference_BU" & BUNumber & ".xlsx").Close SaveChanges:=False

        '''''''''''''''''''''''''''''''''''''File Operation End'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


      MsgBox ("Complete for: " & BUNumber & "")

   Next LCounter


End Sub