Consulting

Results 1 to 13 of 13

Thread: Solved: Copy multiple Excel Sheets

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location

    Solved: Copy multiple Excel Sheets

    Hello All!

    Well, for example I have 50 Excel Sheets. I would like to make a code that copies whatever I want from the first sheet and then jumped to the next one.
    My idea was:

    For n = 1 To 50
    Sheets(n).Select
    Range("C4").Select
    Next n

    The problem is that it is not accepting me to put Sheets(n).Select.

    Can someone please help me. Your help will be much apreciated!

    Thank You!

    AJPF

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not accepting ... in what way, what do you get?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    Well, it says:
    Error in time of execution '9':
    Subscript out of the range

    My VBA is in portuguese. I don't know if this is the correct translation!

    Thanks!

    AJPF

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That suggest you don't have 50 sheets to process through.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    You shouldn't need to select anything in the first place.

    What is it you actually want to do?

    In your title you seem to be indicating you want to copy sheets, but the posted code doesn't have any copying of anything in it.

  6. #6
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    I have 50 sheet tabs with information. I want to copy some cells from each sheet to another excel base.

    ActiveWindow.ActivateNext
    For n = 1 To 2
    Sheets(n).Select
    Rows("1:8").Select
    Selection.Copy
    ActiveWindow.ActivateNext
    Sheets("plan1").Select
    Range("A5").Select
    ActiveSheet.Paste
    Next n

    When I run it, it only copies the cells of the 1 sheet (when n = 1). It stops when n = 2.

    Thanks!
    AJPF

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For n = 1 To 2
    Sheets(n).Rows("1:8").Copy Sheets("plan1").Range("A5")
    Next n
    [/vba]

    see if that is any better (but I suspect protected sheets or something)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why are you copying and then pasting to the same location over and over again from different worksheets?

    Surely all you'll end up with is the data from the last sheet?

  9. #9
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    Dear Norie,
    Sorry for the misunderstanding.
    That was only an example of what I want to do.
    I'm using a rowcounter in order not to paste to the same location every time.

    Here is the whole code:

    ActiveWindow.ActivateNext
    For n = 1 To 2
    'Sheets(n).Select
    'Rows("1:8").Select
    'Selection.Copy
    Sheets(n).Rows("1:8").Copy
    ActiveWindow.ActivateNext
    Sheets("plan1").Select
    i = 1
    Conta = 1
    Do While Cells(i, 1) <> ""
    i = i + 1
    Loop
    Conta = i
    col = i
    Cells(col, 1).Select
    ActiveSheet.Paste
    Next n

    Any help will be much apreciated!

    Thanks!

    AJPF

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Still not 100% sure what you want, especially with all that unneeded activating/selectin, but try this.
    [vba]
    For n = 1 To 2
    Sheets(n).Rows("1:8").Copy Sheets("plan1").Range("A1").Offset((n - 1) * 8)
    Next n[/vba]

    PS Another confusing thing is that's it's not clear if you are working with more than one workbook.

  11. #11
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    Dear Norie and xld,

    It worked! Thank you very much for your support!

    Thanks!

    AJPF

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What worked?

  13. #13
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    The macro worked! It is running without errors.
    Thank you!

    AJPF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •