PC Review


Reply
Thread Tools Rate Thread

Copy problem between worksheets

 
 
Project Mangler
Guest
Posts: n/a
 
      27th Apr 2010
My understanding of fully qualified ranges is a little flimsy. Perhaps this
is why the copy operation in the last line of this code fails with an
"Object doesn't support this property or method".

I would be grateful if someone could point out where I'm going wrong? Is it
the variable typing, the qualification of the ranges or am I just using the
wrong syntax?

Thanks!

Sub procOpenBook()

Dim Book1 As Workbook 'target book
Dim i As Long ' loop variable
Dim wbName As Variant 'file to be opened
Dim shtlastcell As Long 'cell on target sheet at which rows will be
inserted
Dim selRows As Long 'number of elected rows on source sheet
Dim srcSheetName As String ' name of last
Dim srcSheetRng As Range 'selected range on source sheet
Dim srcBook As Workbook ' source workbook


Set srcBook = ActiveWorkbook
Set srcSheetRng = ActiveWindow.RangeSelection
srcSheetName = ActiveSheet.Name
selRows = Selection.Rows.Count

wbName = Application.GetOpenFilename _
(FileFilter:="microsoft excel files (*.xls), *.xls", _
Title:="Get File", MultiSelect:=False)

If wbName <> False Then
Set Book1 = Workbooks.Open(wbName)
Else
Exit Sub
End If

Book1.Sheets(1).Activate

shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To selRows
Rows(shtlastcell + i).Insert (xlShiftDown)
Next

srcBook.srcSheetName.Range(srcSheetRng).Copy
Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)

End Sub



 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      27th Apr 2010
I believe that if you change this:

srcSheetName = ActiveSheet.Name

To this:

srcSheetName = ActiveSheet

It should work. Otherwise, you will have to refer to it as:

Sheets(srcSheetName) to make it work as a destination sheet.

but you can do one or the other and it should work.




"Project Mangler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My understanding of fully qualified ranges is a little flimsy. Perhaps
> this
> is why the copy operation in the last line of this code fails with an
> "Object doesn't support this property or method".
>
> I would be grateful if someone could point out where I'm going wrong? Is
> it
> the variable typing, the qualification of the ranges or am I just using
> the
> wrong syntax?
>
> Thanks!
>
> Sub procOpenBook()
>
> Dim Book1 As Workbook 'target book
> Dim i As Long ' loop variable
> Dim wbName As Variant 'file to be opened
> Dim shtlastcell As Long 'cell on target sheet at which rows will be
> inserted
> Dim selRows As Long 'number of elected rows on source sheet
> Dim srcSheetName As String ' name of last
> Dim srcSheetRng As Range 'selected range on source sheet
> Dim srcBook As Workbook ' source workbook
>
>
> Set srcBook = ActiveWorkbook
> Set srcSheetRng = ActiveWindow.RangeSelection
> srcSheetName = ActiveSheet.Name
> selRows = Selection.Rows.Count
>
> wbName = Application.GetOpenFilename _
> (FileFilter:="microsoft excel files (*.xls), *.xls", _
> Title:="Get File", MultiSelect:=False)
>
> If wbName <> False Then
> Set Book1 = Workbooks.Open(wbName)
> Else
> Exit Sub
> End If
>
> Book1.Sheets(1).Activate
>
> shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
>
> For i = 1 To selRows
> Rows(shtlastcell + i).Insert (xlShiftDown)
> Next
>
> srcBook.srcSheetName.Range(srcSheetRng).Copy
> Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)
>
> End Sub
>
>
>



 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      27th Apr 2010
Sorry, forgot the Set part.

To this:

Set srcSheetName = ActiveSheet




"Project Mangler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My understanding of fully qualified ranges is a little flimsy. Perhaps
> this
> is why the copy operation in the last line of this code fails with an
> "Object doesn't support this property or method".
>
> I would be grateful if someone could point out where I'm going wrong? Is
> it
> the variable typing, the qualification of the ranges or am I just using
> the
> wrong syntax?
>
> Thanks!
>
> Sub procOpenBook()
>
> Dim Book1 As Workbook 'target book
> Dim i As Long ' loop variable
> Dim wbName As Variant 'file to be opened
> Dim shtlastcell As Long 'cell on target sheet at which rows will be
> inserted
> Dim selRows As Long 'number of elected rows on source sheet
> Dim srcSheetName As String ' name of last
> Dim srcSheetRng As Range 'selected range on source sheet
> Dim srcBook As Workbook ' source workbook
>
>
> Set srcBook = ActiveWorkbook
> Set srcSheetRng = ActiveWindow.RangeSelection
> srcSheetName = ActiveSheet.Name
> selRows = Selection.Rows.Count
>
> wbName = Application.GetOpenFilename _
> (FileFilter:="microsoft excel files (*.xls), *.xls", _
> Title:="Get File", MultiSelect:=False)
>
> If wbName <> False Then
> Set Book1 = Workbooks.Open(wbName)
> Else
> Exit Sub
> End If
>
> Book1.Sheets(1).Activate
>
> shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
>
> For i = 1 To selRows
> Rows(shtlastcell + i).Insert (xlShiftDown)
> Next
>
> srcBook.srcSheetName.Range(srcSheetRng).Copy
> Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)
>
> End Sub
>
>
>



 
Reply With Quote
 
Project Mangler
Guest
Posts: n/a
 
      27th Apr 2010
JLGWhiz,

Thanks for the reply. I couldn't get the code to work but you put me on the
right track:

Workbooks(srcBook.Name).Sheets(srcSheetName).Range(srcSheetRng.Address).Copy
ActiveSheet.Paste Destination:=Workbooks(Book1.Name).Sheets(1).Range("A" &
shtlastcell + 1)

Thanks for the help!


"JLGWhiz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Sorry, forgot the Set part.
>
> To this:
>
> Set srcSheetName = ActiveSheet
>
>
>
>
> "Project Mangler" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > My understanding of fully qualified ranges is a little flimsy. Perhaps
> > this
> > is why the copy operation in the last line of this code fails with an
> > "Object doesn't support this property or method".
> >
> > I would be grateful if someone could point out where I'm going wrong? Is
> > it
> > the variable typing, the qualification of the ranges or am I just using
> > the
> > wrong syntax?
> >
> > Thanks!
> >
> > Sub procOpenBook()
> >
> > Dim Book1 As Workbook 'target book
> > Dim i As Long ' loop variable
> > Dim wbName As Variant 'file to be opened
> > Dim shtlastcell As Long 'cell on target sheet at which rows will be
> > inserted
> > Dim selRows As Long 'number of elected rows on source sheet
> > Dim srcSheetName As String ' name of last
> > Dim srcSheetRng As Range 'selected range on source sheet
> > Dim srcBook As Workbook ' source workbook
> >
> >
> > Set srcBook = ActiveWorkbook
> > Set srcSheetRng = ActiveWindow.RangeSelection
> > srcSheetName = ActiveSheet.Name
> > selRows = Selection.Rows.Count
> >
> > wbName = Application.GetOpenFilename _
> > (FileFilter:="microsoft excel files (*.xls), *.xls", _
> > Title:="Get File", MultiSelect:=False)
> >
> > If wbName <> False Then
> > Set Book1 = Workbooks.Open(wbName)
> > Else
> > Exit Sub
> > End If
> >
> > Book1.Sheets(1).Activate
> >
> > shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
> >
> > For i = 1 To selRows
> > Rows(shtlastcell + i).Insert (xlShiftDown)
> > Next
> >
> > srcBook.srcSheetName.Range(srcSheetRng).Copy
> > Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)
> >
> > End Sub
> >
> >
> >

>
>



 
Reply With Quote
 
Project Mangler
Guest
Posts: n/a
 
      27th Apr 2010
This also works if I Dim srcBook & Book1 as Object rather than workbook

srcBook.Sheets(srcSheetName).Range(srcSheetRng.Address).Copy
ActiveSheet.Paste Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)

I may understand this one day ...


"Project Mangler" <(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
> JLGWhiz,
>
> Thanks for the reply. I couldn't get the code to work but you put me on

the
> right track:
>
>

Workbooks(srcBook.Name).Sheets(srcSheetName).Range(srcSheetRng.Address).Copy
> ActiveSheet.Paste Destination:=Workbooks(Book1.Name).Sheets(1).Range("A" &
> shtlastcell + 1)
>
> Thanks for the help!
>
>
> "JLGWhiz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Sorry, forgot the Set part.
> >
> > To this:
> >
> > Set srcSheetName = ActiveSheet
> >
> >
> >
> >
> > "Project Mangler" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > My understanding of fully qualified ranges is a little flimsy. Perhaps
> > > this
> > > is why the copy operation in the last line of this code fails with an
> > > "Object doesn't support this property or method".
> > >
> > > I would be grateful if someone could point out where I'm going wrong?

Is
> > > it
> > > the variable typing, the qualification of the ranges or am I just

using
> > > the
> > > wrong syntax?
> > >
> > > Thanks!
> > >
> > > Sub procOpenBook()
> > >
> > > Dim Book1 As Workbook 'target book
> > > Dim i As Long ' loop variable
> > > Dim wbName As Variant 'file to be opened
> > > Dim shtlastcell As Long 'cell on target sheet at which rows will be
> > > inserted
> > > Dim selRows As Long 'number of elected rows on source sheet
> > > Dim srcSheetName As String ' name of last
> > > Dim srcSheetRng As Range 'selected range on source sheet
> > > Dim srcBook As Workbook ' source workbook
> > >
> > >
> > > Set srcBook = ActiveWorkbook
> > > Set srcSheetRng = ActiveWindow.RangeSelection
> > > srcSheetName = ActiveSheet.Name
> > > selRows = Selection.Rows.Count
> > >
> > > wbName = Application.GetOpenFilename _
> > > (FileFilter:="microsoft excel files (*.xls), *.xls", _
> > > Title:="Get File", MultiSelect:=False)
> > >
> > > If wbName <> False Then
> > > Set Book1 = Workbooks.Open(wbName)
> > > Else
> > > Exit Sub
> > > End If
> > >
> > > Book1.Sheets(1).Activate
> > >
> > > shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
> > >
> > > For i = 1 To selRows
> > > Rows(shtlastcell + i).Insert (xlShiftDown)
> > > Next
> > >
> > > srcBook.srcSheetName.Range(srcSheetRng).Copy
> > > Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)
> > >
> > > End Sub
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      27th Apr 2010
I apologize for not mentioning that if you use the:

Set srcSheetName = ActiveSheet

That you would also have to Dim it as a Worksheet. Probably the easiest way
would have been to just use the Sheets(srcSheetName) syntax. The point is
that the syntax originally posted was attempting to use the variable as a
worksheet vs a worksheet name.
1. If it is used as a name then it has to be qualified with an object
Sheets or Worksheets.
2. If it is Dim as a worksheet and Set as an object variable to either
the ActiveSheet or a specific sheet name or sheets index, then only the
variable need be used because it will equate to the appropriate sheet name
as an object.

I sometimes get in a hurry to respond to these postings and overlook the
fact that if you had known what to do, there would not have been a need for
posting in the first place. Again, my apologies.



"Project Mangler" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> This also works if I Dim srcBook & Book1 as Object rather than workbook
>
> srcBook.Sheets(srcSheetName).Range(srcSheetRng.Address).Copy
> ActiveSheet.Paste Destination:=Book1.Sheets(1).Range("A" & shtlastcell +
> 1)
>
> I may understand this one day ...
>
>
> "Project Mangler" <(E-Mail Removed)> wrote in message
> news:u%(E-Mail Removed)...
>> JLGWhiz,
>>
>> Thanks for the reply. I couldn't get the code to work but you put me on

> the
>> right track:
>>
>>

> Workbooks(srcBook.Name).Sheets(srcSheetName).Range(srcSheetRng.Address).Copy
>> ActiveSheet.Paste Destination:=Workbooks(Book1.Name).Sheets(1).Range("A"
>> &
>> shtlastcell + 1)
>>
>> Thanks for the help!
>>
>>
>> "JLGWhiz" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Sorry, forgot the Set part.
>> >
>> > To this:
>> >
>> > Set srcSheetName = ActiveSheet
>> >
>> >
>> >
>> >
>> > "Project Mangler" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> > > My understanding of fully qualified ranges is a little flimsy.
>> > > Perhaps
>> > > this
>> > > is why the copy operation in the last line of this code fails with an
>> > > "Object doesn't support this property or method".
>> > >
>> > > I would be grateful if someone could point out where I'm going wrong?

> Is
>> > > it
>> > > the variable typing, the qualification of the ranges or am I just

> using
>> > > the
>> > > wrong syntax?
>> > >
>> > > Thanks!
>> > >
>> > > Sub procOpenBook()
>> > >
>> > > Dim Book1 As Workbook 'target book
>> > > Dim i As Long ' loop variable
>> > > Dim wbName As Variant 'file to be opened
>> > > Dim shtlastcell As Long 'cell on target sheet at which rows will
>> > > be
>> > > inserted
>> > > Dim selRows As Long 'number of elected rows on source sheet
>> > > Dim srcSheetName As String ' name of last
>> > > Dim srcSheetRng As Range 'selected range on source sheet
>> > > Dim srcBook As Workbook ' source workbook
>> > >
>> > >
>> > > Set srcBook = ActiveWorkbook
>> > > Set srcSheetRng = ActiveWindow.RangeSelection
>> > > srcSheetName = ActiveSheet.Name
>> > > selRows = Selection.Rows.Count
>> > >
>> > > wbName = Application.GetOpenFilename _
>> > > (FileFilter:="microsoft excel files (*.xls), *.xls", _
>> > > Title:="Get File", MultiSelect:=False)
>> > >
>> > > If wbName <> False Then
>> > > Set Book1 = Workbooks.Open(wbName)
>> > > Else
>> > > Exit Sub
>> > > End If
>> > >
>> > > Book1.Sheets(1).Activate
>> > >
>> > > shtlastcell = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
>> > >
>> > > For i = 1 To selRows
>> > > Rows(shtlastcell + i).Insert (xlShiftDown)
>> > > Next
>> > >
>> > > srcBook.srcSheetName.Range(srcSheetRng).Copy
>> > > Destination:=Book1.Sheets(1).Range("A" & shtlastcell + 1)
>> > >
>> > > End Sub
>> > >
>> > >
>> > >
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select specific worksheets & copy - code problem BeSmart Microsoft Excel Programming 2 11th Mar 2010 11:30 AM
how do i copy a cell in worksheets 10 to the other 9 worksheets =?Utf-8?B?YmV0ZQ==?= Microsoft Excel New Users 3 15th Mar 2007 10:41 AM
Copy data from multiple worksheets to worksheets in a number of other spreadsheets SteveH Microsoft Excel Discussion 5 6th Nov 2006 06:59 PM
copy between worksheets does not copy formulae just values =?Utf-8?B?Q2hyaXNAMTAwMCBPYWtz?= Microsoft Excel Misc 0 19th Mar 2006 11:44 AM
Worksheets won't copy Ted Microsoft Excel Misc 0 1st Jun 2005 01:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:16 AM.