编写excel宏以在新文档中复制(复制)页面

编写excel宏以在新文档中复制(复制)页面,excel,excel-2010,vba,Excel,Excel 2010,Vba,我想写一个宏,它将复制不同excel文档中的列和行,并将它们放在一个文档中,但放在不同的页面上 我的c驱动器上有一个文件夹,其中包含几个不同的excel文档,每个文档都有不同的页面,我只想复制一个名为“用户”的页面。我想做的是将这一页的所有内容复制到不同的文档中,并将其放入另一个excel文档中,每个excel文档都有自己的页面,每个页面都要调用c驱动器上的原始文档的名称 希望我已经解释得足够清楚,是一个宏能够做我想做的事情。目前,我只是将此“用户”页面上的所有内容复制并粘贴到一个文档中,这非常

我想写一个宏,它将复制不同excel文档中的列和行,并将它们放在一个文档中,但放在不同的页面上

我的c驱动器上有一个文件夹,其中包含几个不同的excel文档,每个文档都有不同的页面,我只想复制一个名为“用户”的页面。我想做的是将这一页的所有内容复制到不同的文档中,并将其放入另一个excel文档中,每个excel文档都有自己的页面,每个页面都要调用c驱动器上的原始文档的名称


希望我已经解释得足够清楚,是一个宏能够做我想做的事情。目前,我只是将此“用户”页面上的所有内容复制并粘贴到一个文档中,这非常耗时。

坏消息是,根据您上面的评论,在您能够编写此程序之前,您还有大量的工作要做。好消息是,这个课程本身相对简单,你在这个过程中获得的知识将被证明是非常有用的,如果你喜欢学习,它也应该很有趣

您希望您的程序完成任务的基本概述如下:

告诉你的程序源文件存储在哪里,假设它们都存储在一个目录中;这将使它变得容易得多 每次打开一个文档 复制和粘贴您感兴趣的工作表这可以在一个语句中完成 关闭文档 重复2-4次 完成后,退出该过程 最好的办法是把整个事情分成几部分。第一件/最上面一件看起来像这样:

Sub CopyAllTheUserWorksheets()
    'Get the folder/directory location with your source files:
    Dim MyDirectory As String
    MyDirectory = SelectFolder 
    'SelectFolder is another function you write separately that will open a
    'file dialog box and return the path of the folder you choose. If you
    'cancel the file dialog, it will just return a "\"

    'Now test to make sure a directory was chosen:
    If MyDirectory = "\" Then Exit Sub

    'Create the file name you will loop over to open all the Excel files:
    Dim MyFile As String
    MyFile = Dir(MyDirectory & "*.xlsx")
    'Change .xlsx to .xls if your source files are the old Excel file format

    'Open each file one at a time, perform the actions you want to perform,
    'then close the file. You accomplish this using what is called a Loop.
    'There are a handful of different kinds of Loops. For this task I suggest a
    'Do While loop. Before the loop, we make a Workbook variable in which to
    'store the opened workbook so we can work with it inside of the loop.
    Dim wb As Workbook
    Do While MyFile <> ""
       'Open the Excel workbook:
        Set wb = Workbooks.Open(Filename:=MyDirectory & MyFile)

       'Here you put the task you want to do with the workbook.
       'We'll put that in a separate procedure as well to keep things tidy.
        Call CopyTheUserWorksheetToThisWorkbook(wb)

       'Now that we are done copying the worksheet, close the Excel workbook 
       'without saving any changes
        wb.Close SaveChanges:=False

       'Proceed to the next file name. If there are no more files that match
       '*.xlsx, then myFile will be equal to an empty string, which is "", and
       'the Do While loop with end at that point.
        myFile = Dir
    Loop

End Sub

如果你的问题主要是宏能帮你吗?答案是可以的。但是,您会发现,如果您自己尝试编写代码,那么您将有更好的运气获得答案,这也是一种更好的学习方式。Stackoverflow不是一个免费的代码编写服务。这里有一些地方可以开始:嗨,谢谢。我会看看那些链接。我对以前从未编写过的宏非常陌生。我是否需要从一张空白的excel表格中运行宏,我希望其他excel文档中的所有不同用户页面都保存到该表格中,还是每次都在我已经得到的一个excel文档中运行宏?VBA代码几乎可以从任何支持它的地方保存和运行。VBA只是VisualBasic的一个扩展,因此您可以编写一个独立的VisualBasic程序来实现它。从理论上讲,您甚至可以通过Word或Powerpoint或非Microsoft的VBA实现产品(如AutoCAD)在VBA中执行此操作,尽管这些都没有意义。在这种情况下,将您的过程作为目标Excel工作簿的一部分似乎是最有意义的。下面是您需要解决的难题的另外两个部分:谢谢您的帮助。我现在要看一下这个链接,看看我是否能理解它。我还有一个功能可以将CopyTheUserWorksheet添加到此工作簿,使其成为一个完整的解决方案,我会在有时间的时候这样做。然而,它可能是最容易写的;看看你能不能弄明白。
Function SelectFolder() As String
    'Make a string variable to hold the folder path
    Dim MyFolder As String
    'Make a FileDialog object for choosing the folder
    Dim MyFolderChooser As FileDialog

    'Make the FileDialog object
    Set MyFolderChooser = Application.FileDialog(msoFileDialogFolderPicker)
    'Give it a title (optional)
    MyFolderChooser.Title = "Select A Target Folder"
    'Disable MultiSelect (you only want one folder selected)
    MyFolderChooser.AllowMultiSelect = False
    'This is a normal dialog box, so it could be Cancelled, or Closed. We 
    'need to tell the procedure what to do if that happens. The statement 
    'below basically says "If the dialog is cancelled,skip everything until
    'NextStep." Without this, you would get an error if you canceled or closed
    'the file dialog.
    If MyFolderChooser.Show <> -1 Then GoTo NextStep
    'Assign the MyFolder variable the value given to the FileDialog object
    MyFolder = MyFolderChooser.SelectedItems(1)
    'The (1) means the first value. Since we turned MultiSelect off, there
    'will be no (2), (3), etc, but VBA isn't smart enough to figure this
    'out on its own so you have to tell it.

NextStep:
    'Now just set the name of the function (SelectFolder) equal to the result
    'you want, and that value will be returned to the procedure that called
    'the function.
    SelectFolder = MyFolder & "\" 
    'If no directory was chosen, the function will return just "\"

End Function