VBA import multiple sheets change to only one

This code works great, but if there are multiple worksheets in the importing workbook, it is going to import each.
I just want to import the active sheet in wb2.

Can anyone help modify?

Sub ImportData()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet, ws As Worksheet

Dim lr As Long


Set wb1 = ActiveWorkbook
Set ws = Sheets("Shipping")
  ws.Cells.Delete

lr = 1
FileToOpen = Application.GetOpenFilename _
(title:="Please choose Shipping Data", _
FileFilter:="(*.xlsx), (*.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 ws.range("a" & lr)
         lr = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End With
    Next Sheet

End If

    wb2.Close

End Sub

Open in new window

Euro5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Not tested by try changing lines 25 to 30 to


        With wb2.activesheet
            .Copy ws.range("a" & lr)
         lr = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End With

Open in new window

0
Euro5Author Commented:
@ Martin - error on your line 2.
            .Copy ws.range("a" & lr)

error.JPG
0
Martin LissOlder than dirtCommented:
Please supply the workbook.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Euro5Author Commented:
0
Martin LissOlder than dirtCommented:
I'm not sure how this is supposed to work but I added your ImportData sub to the workbook you posted and slightly modified it as shown below. I added line 6 and commented out lines 25 and 30. When I ran the sub I opened a random xlsx file of mine and got no error. What did I do wrong if anything?


Sub ImportData()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet, ws As Worksheet
Dim FileToOpen
Dim lr As Long


Set wb1 = ActiveWorkbook
Set ws = Sheets("Shipping")
  ws.Cells.Delete

lr = 1
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose Shipping Data", _
FileFilter:="(*.xlsx), (*.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 wb2.ActiveSheet.UsedRange
            .Copy ws.Range("a" & lr)
         lr = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End With
    'Next Sheet

End If

    wb2.Close

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Euro5Author Commented:
Working perfectly for both rate sheets & data. Thanks so much!!!
0
Saurabh Singh TeotiaCommented:
Euro,

In order to make the code more full proof..I would not rely on active sheet..i will first select that sheet from where i want to copy data from in wb2 and then copy it...Something like this...

Sub ImportData()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet, ws As Worksheet

Dim lr As Long


Set wb1 = ActiveWorkbook
Set ws = Sheets("Shipping")
  ws.Cells.Delete

lr = 1
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose Shipping Data", _
FileFilter:="(*.xlsx), (*.xls),")

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

 Sheets("Sheet1").Select
 
        With ActiveSheet.UsedRange
            .Copy ws.Range("a" & lr)
         lr = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End With


End If

    wb2.Close

End Sub

Open in new window


Saurabh...
1
Euro5Author Commented:
@Saurabh, Very good comment - helpful and good idea to explicitly choose rather than passively copy. Thanks for helping here!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.