Copy Specific Sheet from another worbook to active workbook

EddyVilla

New Member
Joined
May 23, 2011
Messages
16
hi,

i'm starting at VBA and what i want to do is to load the files into a listbox (excel files, already done) and then when an specific workbook is selected i want to grab/copy a sheet called "Yield" from the selected sheet and paste it into my workbook. (MainMenu_c)

So i have the following code which works OK (found it online) however i'm unable to adapt it to only copy "Yield" sheet... Thank you!

Code:
Private Sub UserForm_Initialize()
Dim objFSO, objFile As Object
Dim strDirectory As String


Set objFSO = CreateObject("Scripting.FileSystemObject")


strDirectory = "\\xxxx\xxx\xx\Core\DX\Weekly Metrics\Wk's 01-52 2013\"
Me.txtDirectory = strDirectory


    For Each objFile In objFSO.GetFolder(strDirectory).Files
        If StrConv(Right(objFile.Name, 4), vbUpperCase) = "XLSX" Then
            ListBox1.AddItem objFile.Name
        End If
    Next
End Sub




Private Sub cmdOK_Click()


Application.ScreenUpdating = False


Dim sh As Worksheet, wb As Workbook, wb2 As Workbook, shcopy As Worksheet


Set wb = Workbooks("MainMenu_C.xlsm")


Workbooks.Open Me.txtDirectory & "" & ListBox1, ReadOnly:=True
For Each sh In Workbooks("" & ListBox1).Worksheets
 sh.Copy After:=wb.Sheets(wb.Sheets.Count)
Next sh
Sheets(1).Select
Application.ScreenUpdating = True
Unload Me


End Sub
Private Sub cmdCancel_Click()
    Unload Me
   
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try,

Code:
Private Sub UserForm_Initialize()
Dim objFSO, objFile As Object
Dim strDirectory As String




Set objFSO = CreateObject("Scripting.FileSystemObject")




strDirectory = "\\xxxx\xxx\xx\Core\DX\Weekly Metrics\Wk's 01-52 2013\"
Me.txtDirectory = strDirectory




    For Each objFile In objFSO.GetFolder(strDirectory).Files
        If StrConv(Right(objFile.Name, 4), vbUpperCase) = "XLSX" Then
            ListBox1.AddItem objFile.Name
        End If
    Next
End Sub








Private Sub cmdOK_Click()




Application.ScreenUpdating = False




Dim sh As Worksheet, wb As Workbook, wb2 As Workbook, shcopy As Worksheet




Set wb = Workbooks("MainMenu_C.xlsm")




Workbooks.Open Me.txtDirectory & "" & ListBox1, ReadOnly:=True
For Each sh In Workbooks("" & ListBox1).Worksheets
 If UCase(sh.Name) = "YIELD" Then
    sh.Copy After:=wb.Sheets(wb.Sheets.Count)
 End If
Next sh
Sheets(1).Select
Application.ScreenUpdating = True
Unload Me




End Sub
Private Sub cmdCancel_Click()
    Unload Me
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,956
Messages
6,127,931
Members
449,411
Latest member
AppellatePerson

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