PDA

View Full Version : Solved: Copy Selected Rows to Chosen Sheet



Shaolin1976
05-03-2006, 03:57 AM
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:
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 SubThis throws up an error:


Run-time error '9':

Subscript out of rangeThe Code works fine when I remove the variable DestinationSheet and simply replace it with "a" i.e.
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 SubI'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

OBP
05-03-2006, 05:05 AM
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.

ALe
05-03-2006, 05:32 AM
Destinationsheet=Worksheets("MASTER")

ALe
05-03-2006, 05:36 AM
Sorry.

ALe
05-03-2006, 05:37 AM
Try this:

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

lucas
05-03-2006, 06:03 AM
Try this if Ale's code doesn't work out:

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


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....

ALe
05-03-2006, 06:27 AM
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."

johnske
05-03-2006, 06:29 AM
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

lucas
05-03-2006, 06:36 AM
Yes Ale, I am admittedly a little confused as to what is trying to be accomplished....Hey John, good to see you....

Shaolin1976
05-03-2006, 07:00 AM
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

ALe
05-03-2006, 07:41 AM
remember to mark this thread as solved