VBA Code to Select/Import Worksheet From a Workbook

Fieldsdj

Board Regular
Joined
Feb 27, 2012
Messages
65
I need help excel gurus...

Here is my situation: I have a workbook called "AFE MTP_4SOS.xlsm" and one called "Export.xlsm". I need a macro that will run from "AFE MTP_4SOS" and import a worksheet from "Export". So the macro will need to open "Export" and show a list of all the available worksheets and let the user choose which one to import. Once the user chooses, that worksheet should then be imported after the last tab in "AFE MTP_4SOS" and then deleted from the "Export" workbook.

Thank you in advance for your assistance.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
you can make a userform that adds checkbox controls dynamically by creating a checkbox for each sheet in Export. Then in an ok button event you run code that looks at which checkboxes are checked and then copying and moving sheets from one workbook to another is a matter of referencing one workbook's sheet and moving to another workbook reference.

Do you know how to write VBA because your question is vague. If you wanted to know how to do it then my suggestion is one of many ways to accomplish.

If you need the code I can help you but can you write code if i explain it?
 
Last edited:
Upvote 0
Thanks for the quick reply cerfani. I have very little experience with writing code and I've never seen a userform before. If you want to try and explain it to me I'll do my best to try and follow directions.
 
Upvote 0
Thanks for the quick reply cerfani. I have very little experience with writing code and I've never seen a userform before. If you want to try and explain it to me I'll do my best to try and follow directions.

Well I want to let you know now, that in order to accomplish this you will have lots of reading to do but none of the concepts are difficult but you do have to be aware of many things so it can be hard at first. Start with this...

Excel Forms (Userforms) (how to create a form)

I suggest googling "how to make simple vba user form"

Creating Controls at Runtime, On the Fly. Excel Training VBA 2 lesson 21 (adding controls to a form dynamically)

I will make some example for you.
 
Upvote 0
btw if you want something a little more rough, you can accomplish with an inputbox and not use a userform but a userform will be way better

i will make an inputbox version
 
Last edited:
Upvote 0
Code:
Sub SheetImport()
'this code is in a module in AFE MTP_4SOS.xlsm
' call this once Export.xlsm is open or else you gotta write code to open it
'i didnt bother to declare some integers or longs


    Dim msg As String
    msg = ""
    
    With Workbooks("Export.xlsm")
        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 = 1 To Workbooks("Export.xlsm").Worksheets.Count
            If InStr(response, x) > 0 Then
                Dim ws As Worksheet
                Set ws = Workbooks("Export.xlsm").Worksheets(x)
                ws.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) 'you can copy to and not move
            End If
        Next x
    End With
End Sub
 
Last edited:
Upvote 0
edited a couple times, make sure you refresh page... forgot i used the with in the 2nd loop... doh
 
Last edited:
Upvote 0
Rich (BB code):
Sub SheetImport()
'this code is in a module in AFE MTP_4SOS.xlsm
' call this once Export.xlsm is open or else you gotta write code to open it
'i didnt bother to declare some integers or longs


    Dim msg As String
    msg = ""
    
    With Workbooks("Export.xlsm")
        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.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) 'you can copy to and not move
            End If
        Next x
    End With
End Sub

So instead of making a fancy userform with checkboxes you select your sheets by typing the corresponding sheet index

for example if you typed "135" or 1.5.3" or whatever, it would move those sheet indexes from export to the workbook calling code
 
Last edited:
Upvote 0
Rich (BB code):
Sub SheetImport()
'this code is in a module in AFE MTP_4SOS.xlsm
' call this once Export.xlsm is open or else you gotta write code to open it
'i didnt bother to declare some integers or longs


    Dim msg As String
    msg = ""
    
    With Workbooks("Export.xlsm")
        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.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) 'you can copy to and not move
            End If
        Next x
    End With
End Sub

So instead of making a fancy userform with checkboxes you select your sheets by typing the corresponding sheet index

for example if you typed "135" or 1.5.3" or whatever, it would move those sheet indexes from export to the workbook calling code

That code works perfectly. I read some of the material on the userform and it sounds pretty simple to make one but to program it seems pretty complicated. I'll play around with it and see if I can figure it out. Thanks a bunch for your help!!
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,951
Members
449,412
Latest member
montand

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