This code will not open the required worksheet selection in the listbox. I`ve underlined the code that`s failing below

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows

This code will not open the required workbook selection in the listbox. I`ve underlined the code that`s failing below​

Rich (BB code):
Private Sub ListBox3_Click()

    Dim ObjWorksheet As Object, Sht As Worksheet, PageCollect As Collection
    Dim FSO As Object, X As Object, FilDir As Object, Fil As Object, Cnt As Integer
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim ws As Worksheet
  
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
  
    Set FSO = CreateObject("Scripting.FilesystemObject")
    Set wkbDest = Workbooks("Automated Cardworker.xlsm")
    Set wkbSource = FSO.GetFile("\\TGS-SRV01\Share\ShopFloor\PRODUCTION\DLS Cardworker\Jobcard Templates\" _
                         & UserForm1.ListBox3.List(UserForm1.ListBox3.ListIndex))

  Do While wkbDest.Sheets.Count > 4
        wkbDest.Sheets(wkbDest.Sheets.Count).Delete
    Loop



    Workbooks.Open Filename:=wkbSource
Set PageCollect = New Collection
For Each Sht In Workbooks(wkbSource.Name).Sheets
PageCollect.Add Workbooks(wkbSource.Name).Sheets(Sht.Name)
Next Sht

For Cnt = 1 To PageCollect.Count
PageCollect(Cnt).Copy wkbSource.Sheets(Cnt)
Next Cnt
Workbooks(wkbSource.Name).Close SaveChanges:=False
Set wkbSource = Nothing
wkbDest.Worksheets(ObjWorksheet).Delete


Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True



Set FSO = Nothing

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You can't use GetFile to open a workbook. You need to use Workbooks.Open
 
Upvote 0
Thanks for that
The code above is not copying over tabs from specified workbook to "Automated coardworker" can you help me.
 
Upvote 0
Assuming you want to copy the actual sheets themselves:

VBA Code:
  Do While wkbDest.Sheets.Count > 4
        wkbDest.Sheets(wkbDest.Sheets.Count).Delete
    Loop
    Set wkbSource =Workbooks.Open("\\TGS-SRV01\Share\ShopFloor\PRODUCTION\DLS Cardworker\Jobcard Templates\" _
                         & UserForm1.ListBox3.List(UserForm1.ListBox3.ListIndex))

For Each Sht In Workbooks(wkbSource.Name).Sheets
   Sht.Copy After:=wkbDest.Sheets(wkbDest.Sheets.Count)
Next Sht

wkbSource.Close SaveChanges:=False
 
Upvote 0
Not at all - glad we could help. :)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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