0

I am wanting to write a macro which will copy columns and rows from different excel documents and put them into one document but on different pages.

I have a folder on my c drive which containts several different excel documents, each document has different page, the only page I want to copy (duplicate) is a page called 'users'. What I want to do is copy everything from this one page (in the different docuemnts) and put it into another excel document, each having there own page and each page to be called with what the original document on the c drive was named.

Hope I have explained this clearly enough, is a macro able to do what I want it do to. At the moment I just copy and paste everything across from this 'users' page into the one document ( it is very time consuming).

Community
  • 1
  • 1
user3088476
  • 155
  • 1
  • 4
  • 12
  • If your question is mainly "can a macro help with this?" the answer is yes, it can. You'll find, however, you'll have much better luck getting answers if you provide an attempt at writing the code yourself (it's also a much better way to learn). Stackoverflow isn't a free code writing service. – Rick Jul 29 '14 at 14:34
  • Here are some places to start: http://stackoverflow.com/questions/12951946/excel-vba-open-workbook-perform-actions-save-as-close http://stackoverflow.com/questions/6195566/copying-worksheets-from-one-excel-document-to-another – Rick Jul 29 '14 at 14:42
  • Hi, thanks I will take a look at those links. I am very new to macros never written them before. Would I need to run the macro from a blank excel sheet that I want all the different users pages from the other excel documents saved into, or would I run the macro each time in one of the excel documents Ive already got? – user3088476 Jul 30 '14 at 07:34
  • The VBA code can be saved and run from almost anywhere that supports it. VBA is just an extension of Visual Basic, so you could write a standalone Visual Basic program that does it. Theoretically you could even do it in VBA from Word or Powerpoint or even a non Microsoft, VBA implementing product like AutoCAD, though those wouldn't make sense. In this case it sounds like it would make the most sense to include your procedure as part of your destination Excel workbook. – Rick Jul 30 '14 at 12:56
  • Here is another couple of pieces to the puzzle you'll need: http://stackoverflow.com/questions/5971292/vba-excel-getting-file-path-ends-with-folder http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba – Rick Jul 30 '14 at 13:09

1 Answers1

0

The bad news is, based on your comments above, you have a moderate amount of work ahead of you before you will be able to write this program. The good news is the program itself will be relatively simple, and the knowledge you obtain in the process will prove extremely useful (and if you enjoy learning it should be fun, too).

A basic overview of what you'll want your program to do to accomplish your task is:

  1. Tell your program where the source files are stored (presumably they are all stored in a directory together; this will make it a lot easier)
  2. Open each document one at a time
  3. Copy and paste the sheet you are interested in (this can be done in one statement)
  4. Close the document
  5. Repeat 2-4
  6. When finished, exit the procedure

The best way to do this is to split up the whole thing into pieces. The first/top piece will look something like this:

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

See if you can make sense of this. I'll add some more later (for the SelectFolder function and the CopyTheUserWorksheetToThisWorkbook procedure).

Note that this link was very helpful in formulating this answer, and might help you understand what is going on a bit better.

EDIT: Here is the SelectFolder function called by the procedure above:

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
Rick
  • 43,029
  • 15
  • 76
  • 119
  • Thanks for all of this. Im going to take a look at that link now an see if I can make sense of it. – user3088476 Jul 31 '14 at 13:31
  • I still have one function to add (`CopyTheUserWorksheetToThisWorkbook`) for it to be a complete solution and I'll do that when I have time. However, it's probably the easiest one to write; see if you can figure it out. – Rick Jul 31 '14 at 17:10