Copy from workbook 2 and paste to wb1 range

simmerer

New Member
Joined
Sep 6, 2017
Messages
33
Hi,.
I have this and it fails at the paste for "Subscript out of range". It would seem I am not properly selecting my destination. Is it my method?

Sub Macro2()
'
' Macro2 Macro
Dim wb1 As Excel.Workbook 'Open PSSLA workbook first
Set wb1 = ThisWorkbook
Dim wb2 As String 'Open Raw Data Workbook next
wb2 = Application.GetOpenFilename("Excel workbooks,*.xls*")

If wb2 = "False" Then
' ' the user clicked Cancel

Else
' the user selected a file; its path+name is in wb2
Application.Workbooks.Open Filename:=wb2
End If

Worksheets("IBM Rational ClearQuest Web").Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("wb1").Worksheets("PS").Select (failure point)
Range("A2").Select
Selection.Paste

End Sub
 
Re: Copy from woorkbook 2 and paste to wb1 range

In that case you will need to ensure that your workbook is the active workbook & use this
Code:
    Set wb1 = ActiveWorkbook
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Re: Copy from woorkbook 2 and paste to wb1 range

Alternatively you can open the 2 workbooks & assign them a variable name like this
Code:
Sub Macro1()

    Dim Fname As String
    Dim wb(1 To 2) As Workbook
    Dim Cnt As Long
    
    For Cnt = 1 To 2
        Fname = Application.GetOpenFilename("Excel workbooks,*.xls*")
        Set wb(Cnt) = Workbooks.Open(Fname)
    Next Cnt
    MsgBox wb(1).Name
    MsgBox wb(2).Name
End Sub
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

So now with this I need to select the PS sheet and now that is out of range. The code is staring right at it.

Sub Macro2()
'
' Macro2 Macro
Dim wb1 As Excel.Workbook 'Open PSSLA workbook first
Set wb1 = ThisWorkbook
Dim wb2 As String 'Open Raw Data Workbook next
wb2 = Application.GetOpenFilename("Excel workbooks,*.xls*")

If wb2 = "False" Then
' ' the user clicked Cancel

Else
' the user selected a file; its path+name is in wb2
Application.Workbooks.Open Filename:=wb2
End If

Worksheets("IBM Rational ClearQuest Web").Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Set wb1 = ActiveWorkbook (failure point)

Sheets("PS").Select

Range("A2").Select
Selection.Paste

End Sub
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

It should be like this
Code:
Sub Macro2()
'
' Macro2 Macro
Dim wb1 As Excel.Workbook 'Open PSSLA workbook first
Set wb1 = [COLOR=#ff0000]ActiveWorkbook[/COLOR]
Dim wb2 As String 'Open Raw Data Workbook next
wb2 = Application.GetOpenFilename("Excel workbooks,*.xls*")

If wb2 = "False" Then
' ' the user clicked Cancel

Else
' the user selected a file; its path+name is in wb2
Application.Workbooks.Open Filename:=wb2
End If

Worksheets("IBM Rational ClearQuest Web").Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
[COLOR=#ff0000]wb1.Worksheets("PS").Activate[/COLOR]
Range("A2").Select
Selection.Paste

Sheets("PS").Select

Range("A2").Select
Selection.Paste

End Sub
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

wb1.Worksheets("PS").Activate is getting the out of range error again.
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

Open the PSSLA workbook & make sure the PS sheet is active & run this
Code:
Sub FluffTest()

Dim wb1 As Excel.Workbook 'Open PSSLA workbook first
Set wb1 = ActiveWorkbook

MsgBox "|" & ActiveSheet.Name & "|" & vbLf & wb1.Name & vbLf & ActiveWorkbook.Name
End Sub
What does the top line of the message box say & are the 2 file names underneath the same?
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

The top line is the gives the wb2 worksheet, second and third lines are the same: wb2 name.
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

I did that wrong. With the PSSLA Workbook open, and the PS Worksheet active, I get
|PS|
PSSLA.xlsm
PSSLA.xlsm
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

I put your value check into my code here and got the same results again.

Sub Macro2()
'
' Macro2 Macro
Dim wb1 As Excel.Workbook 'Open PSSLA workbook first
Set wb1 = ActiveWorkbook
Dim wb2 As String 'Open Raw Data Workbook next
wb2 = Application.GetOpenFilename("Excel workbooks,*.xls*")



If wb2 = "False" Then
' ' the user clicked Cancel

Else
' the user selected a file; its path+name is in strFile
Application.Workbooks.Open Filename:=wb2
End If

Worksheets("IBM Rational ClearQuest Web").Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
wb1.Worksheets("PS").Activate
MsgBox "|" & ActiveSheet.Name & "|" & vbLf & wb1.Name & vbLf & ActiveWorkbook.Name

End Sub
 
Upvote 0
Re: Copy from woorkbook 2 and paste to wb1 range

That would suggest that you are no longer the out of range error, is that correct?
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top