Vba to import one entire worksheet from another workbook

jzellman

New Member
Joined
Aug 12, 2015
Messages
17
Want a user the ability to:

1. Navigate to a file
2. Import the only non-blank worksheet from another workbook

thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
Sub shCopy()
Dim fName As String, wb As Workbook
fName = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
Set wb = Workbooks.Open(fName)
    For Each sh In wb.Sheets
        If Application.CountA(sh.Cells) > 0 Then
            sh.Copy Before:=ThisWorkbook.Sheets(1)
            Exit For
        End If
    Next
    wb.Close False
End Sub
This will allow the user to find the file they want then open it by selecting it and the code will find the first sheet with data to import to the host workbook. The code is assumed to be run from the destination workbook.
 
Last edited:
Upvote 0
Code:
Sub shCopy()
Dim fName As String, wb As Workbook
fName = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
Set wb = Workbooks.Open(fName)
    For Each sh In wb.Sheets
        If Application.CountA(sh.Cells) > 0 Then
            sh.Copy Before:=ThisWorkbook.Sheets(1)
            Exit For
        End If
    Next
    wb.Close False
End Sub
This will allow the user to find the file they want then open it by selecting it and the code will find the first sheet with data to import to the host workbook. The code is assumed to be run from the destination workbook.

I had been using this code but now I am receiving a run-time error 91. Do you know why this would be happening?
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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