Open and copy the contents of another excel sheet to active workbook using VBA

gsgurpreetsingh501

New Member
Joined
Jan 7, 2016
Messages
12
I'm using below code for copying data from another worksheet to the active worksheet. If i give Sheet2 instead of Sheet1 (in bold), it's giving error as "Run-time error 1004: copy method of range class failed". Please note that Sheet2 exists and command button for the code is placed in Sheet1. Please help in resolving the issue

Private Sub ImportData_Click()


Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range


Set wb1 = ActiveWorkbook
Set PasteStart = [Sheet1!A1]


Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents


FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Report to Parse", _
FileFilter:="Report Files *.xls (*.xls),")


If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)


For Each Sheet In wb2.Sheets
With Sheet.UsedRange
.Copy PasteStart
Set PasteStart = PasteStart.Offset(.Rows.Count)
End With
Next Sheet


End If


wb2.Close


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try setting PasteStart after you've cleared 'Sheet1'.
 
Upvote 0
No it's not working.

Sheets("Sheet2").Select '------1
Cells.Select '------2
Selection.ClearContents '------3

Set PasteStart = [Sheet2!A1]

In debug mode, it's showing error coming due to line 2.
 
Upvote 0
That code isn't the same as the code in the original post.

Also, you don't need to select anything to work with it.

Try this.
Code:
Private Sub ImportData_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range

    Set wb1 = ActiveWorkbook

    With wb1
        .Sheets("Sheet1").ClearContents
        Set PasteStart = .Sheets("Sheet1").Range("A1")
    End With

    FileToOpen = Application.GetOpenFilename _
                 (Title:="Please choose a Report to Parse", _
                  FileFilter:="Report Files *.xls (*.xls),")

    If FileToOpen = False Then
        MsgBox "No File Specified.", vbExclamation, "ERROR"
        Exit Sub
    Else
        Set wb2 = Workbooks.Open(Filename:=FileToOpen)

        For Each Sheet In wb2.Sheets
            With Sheet.UsedRange
                .Copy PasteStart
                Set PasteStart = PasteStart.Offset(.Rows.Count)
            End With
        Next Sheet

    End If

    wb2.Close

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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