Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Location: South Wales, UK
Posts: 11
Default VBA code to import worksheets from other workbooks

Hi

Is there a VBA code that I can put into a Master workbook that will import sheets from four different workbooks?

I only want to import sheets if they contain "W93004" in the sheet name.

Many Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA code to import worksheets from other workbooks

Hi Alice,

Am Mon, 22 Apr 2013 15:55:37 +0100 schrieb Alice21:

Is there a VBA code that I can put into a Master workbook that will
import sheets from four different workbooks?

I only want to import sheets if they contain "W93004" in the sheet
name.


open Master and the other 4 workbooks.
Code for workbook "Master":

Sub Import()
Dim wbk As Workbook
Dim wsh As Worksheet

ThisWorkbook.Activate
For Each wbk In Application.Workbooks
If wbk.Name < "Master.xlsm" Then
For Each wsh In wbk.Worksheets
If InStr(wsh.Name, "W93004") 0 Then
wsh.Copy _
after:=Workbooks("Master.xlsm").Sheets(Sheets.Coun t)
End If
Next wsh
End If
Next wbk
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Junior Member
 
Location: South Wales, UK
Posts: 11
Default

Thanks. I have this code, however i cannot get it to work. I think it may be because the sheet names are not W93004, they contain text after them too. Is there a 'wild card' i need to out in to find these sheets?

Sub Import()
Dim wbk As Workbook
Dim wsh As Worksheet

ThisWorkbook.Activate
For Each wbk In Application.Workbooks
If wbk.Name < "W93004.xlsm" Then
For Each wsh In wbk.Worksheets
If InStr(wsh.Name, "W93004") 0 Then
wsh.Copy _
after:=Workbooks("W93004.xlsm").Sheets(Sheets.Coun t)
End If
Next wsh
End If
Next wbk
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA code to import worksheets from other workbooks

Hi Alice,

Am Tue, 23 Apr 2013 09:07:53 +0100 schrieb Alice21:

Thanks. I have this code, however i cannot get it to work. I think it
may be because the sheet names are not W93004, they contain text after
them too. Is there a 'wild card' i need to out in to find these
sheets?


instr searches for a substring in the sheet name. So you don't need a
wildcard.
Did you suit the workbook name and the sheet names correctly?
Is the code in a standard module of workbook "W93004.xlsm"?


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #5   Report Post  
Junior Member
 
Location: South Wales, UK
Posts: 11
Default

I put the code in the Master workbook called W93004.
I have the other four workbooks open.
When i run the macro it gives me a run time error 9


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA code to import worksheets from other workbooks

Hi Alice,

Am Tue, 23 Apr 2013 10:52:01 +0100 schrieb Alice21:

I put the code in the Master workbook called W93004.
I have the other four workbooks open.
When i run the macro it gives me a run time error 9


is the workbook W93004 saved as workbook with macros?
Or is it another format? xlsx or xlsb?
In Direct window write:
?Thisworkbook.name


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #7   Report Post  
Junior Member
 
Location: South Wales, UK
Posts: 11
Default

It's a workbook with Macros. Is an excel 2003 file .xls
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA code to import worksheets from other workbooks

Hi Alice,

Am Tue, 23 Apr 2013 14:02:16 +0100 schrieb Alice21:

It's a workbook with Macros. Is an excel 2003 file .xls


then you have to change the code:

Sub Import()
Dim wbk As Workbook
Dim wsh As Worksheet

ThisWorkbook.Activate
For Each wbk In Application.Workbooks
If wbk.Name < "W93004.xls" Then
For Each wsh In wbk.Worksheets
If InStr(wsh.Name, "W93004") 0 Then
wsh.Copy _
after:=Workbooks("W93004.xls").Sheets(Sheets.Count )
End If
Next wsh
End If
Next wbk
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #9   Report Post  
Junior Member
 
Location: South Wales, UK
Posts: 11
Default

Thank you ever so much! That works! I've been working on that for days!
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA code to import worksheets from other workbooks

Hi Alice,

Am Tue, 23 Apr 2013 16:01:49 +0100 schrieb Alice21:

Thank you ever so much! That works! I've been working on that for
days!


glad to help and thank you for the feedback.
For the futu Please write your Excel version in the question

Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to split Worksheets into seperat workbooks Phil Smith Excel Programming 6 March 26th 09 12:06 AM
HELP = VBA Code To Import Worksheets Into A WorkBook tommo_blade Excel Programming 7 July 3rd 08 07:56 PM
Simple Folder, Workbooks, Worksheets Import ? u473 Excel Programming 1 April 16th 08 09:11 PM
how can i import worksheets from multiple workbooks davidp23 New Users to Excel 1 September 1st 06 08:49 PM
Change code to make worksheets instead of workbooks mikeb1[_4_] Excel Programming 3 December 15th 03 03:23 PM


All times are GMT +1. The time now is 10:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"