VBA - Import Workbook/Choose Sheet/Display Details

default_name

Board Regular
Joined
May 16, 2018
Messages
170
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hello,

This was a follow-on question related to another thread here on the board.
At the recommendation of the board (the original question is more than 1400 days old), as well as the original solution giver, I am posting this with some of my own directed details.

I am trying to do a mixture of a few different things I have found on this board, but I have had difficulty implementing it.
Source posts:
VBA Code to Select Import Worksheet from a Workbook
Macro to Open File Select a Sheet and Copy This to Another File

I have two worksheets in my workbook ('Status' and 'Import').
On sheet 'Status' I have a button that is attached to a macro called 'ImportData'.

  1. When the button is pressed, I want the user to be prompted to select an excel workbook.
  2. Since the possible workbooks to choose from are all unique in names and content, once a file is selected, I want the user to then be able to identify which worksheet within the desired workbook they would like to import.
    (I found a macro that seemed to accomplish this task quite nicely, but it only works if the desired file is already open.)
  3. The desired worksheet is then copied from the chosen workbook, onto my 'Import' tab/worksheet
  4. The location of the file is displayed/pasted on cell B6 of the 'Status' worksheet/tab

Here is what I currently have. I feel like I am so close, but there are some snags that I have been unable to overcome on my own.
I added some notes/comments, to hopefully help.

VBA Code:
Sub ImportData()
    Dim OpenBook As Workbook
    Dim TargetFile As String

    'Need code here that prompts user to select and open a workbook (no screen updating)
    'The open/selected workbook is recorded as being "TargetFile"
  
    'The following code looks at the newly selected/opened workbook, then prompts the user to choose which worksheet to copy from
    'The information is then copied into the 'Import' tab
    Dim msg As String
    msg = "Which worksheet would you like to copy from this file?"
  
    With Workbooks(TargetFile)
        For i = 1 To .Worksheets.Count
            msg = msg & "(" & i & ") " & .Worksheets(i).Name & vbCrLf
        Next i
  
        response = InputBox(msg, "Type numbers for sheets to import")
  
        If response = Null Then Exit Sub 'check for cancel button
  
        For x = .Worksheets.Count To 1 Step -1
            If InStr(response, x) > 0 Then
                Dim ws As Worksheet
                Set ws = .Worksheets(x)
                ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) 'Copies the chosen worksheet into 'Import' tab
                ActiveSheet.Name = "Import"
            End If
        Next x
    End With
        Sheets("Status").Select    'This routine pastes the path of the chosen/selected file on cell B6 of the 'Status' tab/sheet
        Range("B" & counter + 6).Value = TargetFile
  
    If FileName <> False Then
    End If
  
    'Need code here that closes the chosen workbook file without making any saves or changes (no screen updating)
    Workbooks(TargetFile).Close SaveChanges:=False
    Application.ScreenUpdating = False
  
End Sub

I hope this was clear enough. If not, let me know.
Thanks in advance for your help!
 
It worked! Thank you so much! You're a life saver.
I really appreciate it, Robert. Have an awesome day!
Thanks for letting us know and you're welcome. Hope you have a great too :)
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,215,174
Messages
6,123,451
Members
449,100
Latest member
sktz

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