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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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,214,825
Messages
6,121,788
Members
449,049
Latest member
greyangel23

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