Help in retrieving data from a worksheet of a different workbook

Milind Agarwal
Milind Agarwal used Ask the Experts™
on
Hello Experts,
The below code works if  I have "Template Tab" and "Forms" tab both present in the same workbook. But If I have a "Forms" tab coming from a Workbook2 which is closed and saved at a location and MyActiveWorkbook has a Template Tab, what modifications do I need to make to the below code in order to achieve my goal without coping the "Forms" Tab into the Active Workbook? Any help would be appreciated

Below code takes the names present in the Column A of Forms Tab and Creates different worksheet with the same names. All the newly formed worksheets will have same template as the template tab

Below Code:

Sub createWorksheets()

    Dim r As Long, TemplateWS As Worksheet
    Set TemplateWS = Worksheets("Template Tab")
    For r = 2 To Worksheets("Forms").Cells(Rows.Count, "A").End(xlUp).Row
        If Worksheets("Forms").Cells(r, "D") = True Then
            TemplateWS.Copy after:=Worksheets(Worksheets.Count)
            ActiveSheet.Name = Worksheets("Forms").Cells(r, "A")
        End If
    Next
   
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
soulintox,

Quick question are you comfortable..writting a process which opens the workbook which has form tab..copying it over in this workbook..and then creating the necessary templates file and then deleting the form tab??

Saurabh...

Author

Commented:
I think that would work.
Top Expert 2015
Commented:
You can do something like this assuming the workbook where you are looking for is known as Book1..

Sub createWorksheets()
Application.DisplayAlerts = False
Application.ScreenUpdating = False


    Dim r As Long, TemplateWS As Worksheet
    Dim xtr As String, wb As Workbook, wb1 As Workbook
    
    xtr = ThisWorkbook.Path
    
    Set wb = ThisWorkbook
    
    Set wb1 = Workbooks.Open(xtr & "\" & "Book1.xlsx")
     Sheets("Forms").Copy After:=wb.Sheets(Sheets.Count)
     wb1.Close (False)
     
    Set TemplateWS = Worksheets("Template Tab")
    For r = 2 To Worksheets("Forms").Cells(Rows.Count, "A").End(xlUp).Row
        If Worksheets("Forms").Cells(r, "D") = True Then
            TemplateWS.Copy After:=Worksheets(Worksheets.Count)
            ActiveSheet.Name = Worksheets("Forms").Cells(r, "A")
        End If
    Next
    Sheets("Forms").Delete
    
   
   Application.DisplayAlerts = True
   
End Sub

Open in new window


Saurabh...

Author

Commented:
Hey Saurabh,
I am getting Run-Time error 9 : Subscript out of range @ Sheets("Forms").Copy After:=wb.Sheets(Sheets.Count)  that is when the Book1 is closed. Once it is open and I run it, i get an error like Index refers beyond end of list @ Sheets("Forms").Copy After:=wb.Sheets(Sheets.Count).

Am I missing something here?

Author

Commented:
It worked just made little change to the existing code wb.Sheets(wb.Sheets.Count)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial