Consulting

Results 1 to 11 of 11

Thread: Solved: Copy Selected Rows to Chosen Sheet

  1. #1

    Solved: Copy Selected Rows to Chosen Sheet

    Hi,

    I'm new to VBA and have run into a slight problem that I was hoping someone might help me out with. I am trying to copy the selected rows from one sheet (MASTER) to the next available blank row in a choice of other sheets a,b,c etc. I have a cell in the MASTER sheet (C1) in which I designate the destination sheet to copy the rows to. My code is as follows:
    [vba]Private Sub CopyRows_Click()
    Worksheets("MASTER").Select
    Selection.Rows.Copy
    DestinationSheet = Worksheets("MASTER").Range("C1")
    NextBlankRow = Sheets(DestinationSheet).Range("A65536").End(xlUp).Row + 1
    Sheets(DestinationSheet).Rows(NextBlankRow).PasteSpecial
    End Sub[/vba]This throws up an error:

    Run-time error '9':

    Subscript out of range
    The Code works fine when I remove the variable DestinationSheet and simply replace it with "a" i.e.
    [vba]Private Sub CopyRows_Click()
    Worksheets("MASTER").Select
    Selection.Rows.Copy
    NextBlankRow = Sheets("a").Range("A65536").End(xlUp).Row + 1
    Sheets("a").Rows(NextBlankRow).PasteSpecial
    End Sub[/vba]I'm sure it is a simple thing to correct but I can't seem to find the answer in the excel help files. If I had managed to get this working easily enough then my next stage was to try and substitute Cell C1 with a List Box of available sheets to copy to but this is problem enough I think! Any help with this would be greatly appreciated.

    Shaolin
    Last edited by Shaolin1976; 05-03-2006 at 04:42 AM.

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    It works fine for me, except that it pastes the copied data right across the row repeating it until it fills the entire row.
    That suggests that the entry in C1 is not the name of a valid Sheet as it worked with both Sheet3 and a as the entry for me.
    Last edited by OBP; 05-03-2006 at 05:21 AM.

  3. #3
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    [VBA]
    Destinationsheet=Worksheets("MASTER")
    [/VBA]

  4. #4
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Sorry.

  5. #5
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Try this:
    [VBA]
    Private Sub CopyRows()
    Dim DestinationSheet As String

    Worksheets("MASTER").Select
    Selection.Rows.Copy
    DestinationSheet = Worksheets("MASTER").Range("C1").Value
    NextBlankRow = Sheets(DestinationSheet).Range("A65536").End(xlUp).Row + 1
    Sheets(DestinationSheet).Rows(NextBlankRow).PasteSpecial
    End Sub
    [/VBA]

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try this if Ale's code doesn't work out:
    [vba]
    Option Explicit
    Sub CopyRows_Click()
    Dim NextBlankRow As Long
    Worksheets("MASTER").Select
    Selection.Rows.Copy
    NextBlankRow = Sheets("DestinationSheet").Range("A65536").End(xlUp).Row + 1
    Sheets("DestinationSheet").Rows(NextBlankRow).PasteSpecial
    End Sub
    [/vba]

    Edit:
    Remember that the sheet your copying to must be named DestinationSheet
    Exactly...or you will get a subscript out of range error....

    ps you can't designate a cell to copy to if your going to look for the next blank row to copy to (per your first post...) a little confusion here I think. this will copy from master to destinationsheet, change the names to suit your need....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    I suppose DestinationSheet is the content of the cell C1 as Shaolin wrote "I have a cell in the MASTER sheet (C1) in which I designate the destination sheet to copy the rows to."

  8. #8
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    [VBA]Option Explicit

    Sub CopyRows_Click()
    Worksheets("MASTER").Select
    Selection.EntireRow.Copy
    Sheets(Sheets("MASTER").[C1].Value) _
    .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) _
    .PasteSpecial Paste:=xlFormats '< or whatever
    End Sub[/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Yes Ale, I am admittedly a little confused as to what is trying to be accomplished....Hey John, good to see you....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Thanks for all the helpful replies and suggestions everyone, ALe's solution worked perfectly. I could have sworn I had previously tried declaring DestinationSheet as a String but no matter, I must have done something wrong in that attempt.

    Now to try and tackle the ListBox instead of having it read the destination from Cell C1.

    Shaolin

    PS Dont worry I refuse to post my problems unless I'm utterly at a loss so you wont see any posts regading the next stage of my little macro!

    PPS You were correct in suggesting that DestinationSheet does not refer to the name of a sheet but merely the contents of C1

  11. #11
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    remember to mark this thread as solved

Posting Permissions

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