PC Review


Reply
Thread Tools Rate Thread

Copy Worksheet, Excel 2000 & 2003

 
 
jfcby
Guest
Posts: n/a
 
      6th Aug 2007
Hello,

The following macro code gives the error: "Subscript out of range" on
this line: "wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)".

I'm trying to copy certain worksheets form one workbook to another
workbook.

Why am I getting this error and how can the following macro be
modified to copy the worksheets?

Sub CopyWorkSheets_()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsA As Worksheet
Set wbA = Workbooks("Equip_List_FF.xls")
Set wbB = Workbooks("FF_Zone5_Bldgs.xls")
For Each wsA In wbA.Worksheets
For Each cell In wbB.Worksheets("Index").Range("E4:E27")
If wsA.Visible = xlSheetHidden Then GoTo nws
'MsgBox "Worksheet Name = " & wsA.Name & " " & "Value = " &
Right(wsA.Range("C2").Value, 4) _
& " " & "cell = " & cell
If cell.Text = Right(wsA.Range("C2").Text, 4) Then
wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)
GoTo nws
End If
Next cell
nws:
Next wsA
End Sub

Than you for your help,
jfcby

 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      6th Aug 2007

1. Are both workbooks open?
2. Since the subject of your message references two different versions
of Excel - are both workbooks open in the same instance of Excel?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"jfcby" <(E-Mail Removed)>
wrote in message
Hello,
The following macro code gives the error: "Subscript out of range" on
this line: "wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)".
I'm trying to copy certain worksheets form one workbook to another
workbook.
Why am I getting this error and how can the following macro be
modified to copy the worksheets?

Sub CopyWorkSheets_()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsA As Worksheet
Set wbA = Workbooks("Equip_List_FF.xls")
Set wbB = Workbooks("FF_Zone5_Bldgs.xls")
For Each wsA In wbA.Worksheets
For Each cell In wbB.Worksheets("Index").Range("E4:E27")
If wsA.Visible = xlSheetHidden Then GoTo nws
'MsgBox "Worksheet Name = " & wsA.Name & " " & "Value = " &
Right(wsA.Range("C2").Value, 4) _
& " " & "cell = " & cell
If cell.Text = Right(wsA.Range("C2").Text, 4) Then
wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)
GoTo nws
End If
Next cell
nws:
Next wsA
End Sub

Than you for your help,
jfcby

 
Reply With Quote
 
jfcby
Guest
Posts: n/a
 
      6th Aug 2007
Hello Jim Cone,

Yes, both worksheets are open in the same version of Excel.

Thank you for your help,
jfcby

 
Reply With Quote
 
jfcby
Guest
Posts: n/a
 
      7th Aug 2007
Hello,

Thank you for your help!

This is the working macro code:

Sub CopyWorkSheets_()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsA As Worksheet
Set wbA = Workbooks("Equip_List_FF.xls")
Set wbB = Workbooks("FF_Zone5_Bldgs.xls")
For Each wsA In wbA.Worksheets
For Each cell In wbB.Worksheets("Index").Range("E4:E27")
If wsA.Visible = xlSheetHidden Then GoTo nws
'MsgBox "Worksheet Name = " & wsA.Name & " " & "Value = " &
Right(wsA.Range _("C2").Value, 4) & " " & "cell = " & cell
If cell.Text = Right(wsA.Range("C2").Text, 4) Then
wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)
GoTo nws
End If
Next cell
nws:
Next wsA
End Sub

Thank you,
jfcby

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      7th Aug 2007
I don't know if you mean the code works now or not?
I don't see any difference in the two versions.
In any case here is a modified version (untested) that you can try.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)

'---
Sub CopyWorkSheets_()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsA As Worksheet
Dim cell As Range
Dim strT As String

Set wbA = Workbooks("Equip_List_FF.xls")
Set wbB = Workbooks("FF_Zone5_Bldgs.xls")

For Each wsA In wbA.Worksheets
If wsA.Visible = xlSheetHidden Then
strT = Right$(wsA.Range("C2").Text, 4)
For Each cell In wbB.Worksheets("Index").Range("E4:E27")
If cell.Text = strT Then
wsA.Visible = xlSheetVisible
wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)
wsA.Visible = xlSheetHidden
Exit For
End If
Next cell
End If
Next wsA
End Sub
'---


"jfcby" <(E-Mail Removed)>
wrote in message
Hello,
Thank you for your help!
This is the working macro code:

Sub CopyWorkSheets_()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsA As Worksheet
Set wbA = Workbooks("Equip_List_FF.xls")
Set wbB = Workbooks("FF_Zone5_Bldgs.xls")
For Each wsA In wbA.Worksheets
For Each cell In wbB.Worksheets("Index").Range("E4:E27")
If wsA.Visible = xlSheetHidden Then GoTo nws
'MsgBox "Worksheet Name = " & wsA.Name & " " & "Value = " &
Right(wsA.Range _("C2").Value, 4) & " " & "cell = " & cell
If cell.Text = Right(wsA.Range("C2").Text, 4) Then
wsA.Copy after:=wbB.Sheets(wbB.Sheets.Count)
GoTo nws
End If
Next cell
nws:
Next wsA
End Sub

Thank you,
jfcby

 
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
Unable to Insert Worksheet/Move and copy worksheet Excel 2003 lukerush Microsoft Excel Worksheet Functions 4 7th Sep 2006 05:05 PM
Unable to Insert Worksheet/Move and copy worksheet Excel 2003 lukerush Microsoft Excel Worksheet Functions 0 7th Sep 2006 03:37 PM
Excel 2000 Copy worksheet w/ ChartObjects memory leak aeren.robinson@gmail.com Microsoft Excel Programming 0 3rd Feb 2006 12:57 AM
Can't copy Word 2003 table into Excel 2003 worksheet JerryD Microsoft Excel Worksheet Functions 0 24th Apr 2004 03:34 PM
Can't copy Word 2003 table into Excel 2003 worksheet =?Utf-8?B?SmVycnlE?= Microsoft Excel Setup 0 14th Apr 2004 11:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:06 PM.