Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default HELP = VBA Code To Import Worksheets Into A WorkBook

Hello,
does anyone have any snippets/examples of code that will help
me do the following:-

I have a directory that will contain 'n' number of excel workbooks
that have been emailed to me, there is a **single worksheet within
this workbook and it will contain data, cell functions and macro's, I
am going to put a button on my master workbook and I want that to
import all workbooks from this directory and have them inserted as a
seperate worksheet within my master workbook keeping all the original
imported worksheet data, functions & macro's intact.

** I may need to test that the imported worksheet only has the ONE
worksheet, anymore and I need to know about it...

any help/guidance is appreciated...


thanks in advance, Mark.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default HELP = VBA Code To Import Worksheets Into A WorkBook

Sub InportSheets()

Folder = "c:\temp\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
Set bk = Workbooks.Open(Filename:=Folder & FName)
If bk.Sheets.Count 1 Then
MsgBox ("More Then one sheet in " & FName)
End If
Worksheets.Copy after:=.Sheets(.Sheets.Count)
bk.Close savechanges:=False
End With

FName = Dir()
Loop

End Sub


"tommo_blade" wrote:

Hello,
does anyone have any snippets/examples of code that will help
me do the following:-

I have a directory that will contain 'n' number of excel workbooks
that have been emailed to me, there is a **single worksheet within
this workbook and it will contain data, cell functions and macro's, I
am going to put a button on my master workbook and I want that to
import all workbooks from this directory and have them inserted as a
seperate worksheet within my master workbook keeping all the original
imported worksheet data, functions & macro's intact.

** I may need to test that the imported worksheet only has the ONE
worksheet, anymore and I need to know about it...

any help/guidance is appreciated...


thanks in advance, Mark.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default HELP = VBA Code To Import Worksheets Into A WorkBook

It does'nt seem to like the line identified by == LINE <== below:

Sub import_xls()
MsgBox "Hello:"
Folder = "F:\My Documents\Fantasy Football\XLS_Emails\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
== Set bk = Workbooks.Open(Filename:=Folder & FName) <==
If bk.Sheets.Count 1 Then
MsgBox ("More Then one sheet in " & FName)
End If
Worksheets.Copy after:=.Sheets(.Sheets.Count)
bk.Close savechanges:=False
End With


FName = Dir()
Loop


End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default HELP = VBA Code To Import Worksheets Into A WorkBook

I tried running your modified code and it is working fine. Try the following

1) Try opening the files manually in excel and see if it opens. You may not
have permission to open the file or the file is corrupted. Maybe an excel
file created in a different version of excel.
2) Put some excel files on your c: drive and try again to verify the code
runs on your PC.

This code is pretty generic and shold not have problems. The folder exists
because you would of gotten an error on the line with DIR.



"tommo_blade" wrote:

It does'nt seem to like the line identified by == LINE <== below:

Sub import_xls()
MsgBox "Hello:"
Folder = "F:\My Documents\Fantasy Football\XLS_Emails\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
== Set bk = Workbooks.Open(Filename:=Folder & FName) <==
If bk.Sheets.Count 1 Then
MsgBox ("More Then one sheet in " & FName)
End If
Worksheets.Copy after:=.Sheets(.Sheets.Count)
bk.Close savechanges:=False
End With


FName = Dir()
Loop


End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default HELP = VBA Code To Import Worksheets Into A WorkBook

my error, I had copied the same xls file into the directory that I was
using as the master, put a completely different xls in that dir and it
works fine...

many thanks..


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default HELP = VBA Code To Import Worksheets Into A WorkBook

the code does not appear to be doing exactly what I wanted, here was
my criteria:-

I have an open workbook and I execute a macro from one of the
worksheets within that book, I want the macro to look in a directory
and search for valid .xls files, I then need it to look thr every
workbook looking for a certain type of worksheet, so far the code does
this ok (except that it physically open each workbook, can it do this
in the background), when such a sheet is found I want the sheet
copying (macro, functions & all) as a new sheet in my open workbook -
this the code is not doing.

-------------------------------------------------------------------------------------------------------------
Sub import_xls()
Dim y As Integer
Dim d As Integer
Dim p As Integer

Folder = "F:\My Documents\Fantasy Football\XLS_Emails\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
Set bk = Workbooks.Open(Filename:=Folder & FName)
For y = 1 To bk.Sheets.Count
If Left(bk.Sheets(y).Cells(1, 1), 4) = "Name" Then
MsgBox "FOUND A VALID TEAMSHEET " & bk.Sheets(y).Cells(1, 2) &
" IN:" & FName
For p = 8 To 18
If InStr(1, bk.Sheets(y).Cells(p, 2), 1) < "" Then
MsgBox "PLAYER CELL POPULATED OK: " & p
Else
MsgBox "ERROR: EMPTY PLAYER CELL IN: " &
bk.Sheets(y).Cells(p, 2)
Exit Sub
End If
Next p

Else
'MsgBox "UN-MATCHED TEAMSHEET:" & FName
End If

MsgBox "CREATING NEW WORKSHEET FOR:" & bk.Sheets(y).Cells(1,
2)
Worksheets.Copy after:=Sheets(Sheets.Count)
bk.Close savechanges:=False

Next y
End With


FName = Dir()
Loop
End Sub

-----------------------------------------------------------------------------------------------------------------------------------
thanks.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default HELP = VBA Code To Import Worksheets Into A WorkBook

worksheets.copy needs to have an index value, like worksheets(y).copy

each workbook needs to be opened. To hide this, put this near the top of
your routine, resetting it to true at the end of your routine.

Application.ScreenUpdating = False

hth
Keith

"tommo_blade" wrote in message
...
the code does not appear to be doing exactly what I wanted, here was
my criteria:-

I have an open workbook and I execute a macro from one of the
worksheets within that book, I want the macro to look in a directory
and search for valid .xls files, I then need it to look thr every
workbook looking for a certain type of worksheet, so far the code does
this ok (except that it physically open each workbook, can it do this
in the background), when such a sheet is found I want the sheet
copying (macro, functions & all) as a new sheet in my open workbook -
this the code is not doing.

-------------------------------------------------------------------------------------------------------------
Sub import_xls()
Dim y As Integer
Dim d As Integer
Dim p As Integer

Folder = "F:\My Documents\Fantasy Football\XLS_Emails\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
Set bk = Workbooks.Open(Filename:=Folder & FName)
For y = 1 To bk.Sheets.Count
If Left(bk.Sheets(y).Cells(1, 1), 4) = "Name" Then
MsgBox "FOUND A VALID TEAMSHEET " & bk.Sheets(y).Cells(1, 2) &
" IN:" & FName
For p = 8 To 18
If InStr(1, bk.Sheets(y).Cells(p, 2), 1) < "" Then
MsgBox "PLAYER CELL POPULATED OK: " & p
Else
MsgBox "ERROR: EMPTY PLAYER CELL IN: " &
bk.Sheets(y).Cells(p, 2)
Exit Sub
End If
Next p

Else
'MsgBox "UN-MATCHED TEAMSHEET:" & FName
End If

MsgBox "CREATING NEW WORKSHEET FOR:" & bk.Sheets(y).Cells(1,
2)
Worksheets.Copy after:=Sheets(Sheets.Count)
bk.Close savechanges:=False

Next y
End With


FName = Dir()
Loop
End Sub

-----------------------------------------------------------------------------------------------------------------------------------
thanks.



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default HELP = VBA Code To Import Worksheets Into A WorkBook

it's this piece of code that I do not understand:

Worksheets.Copy after:=Sheets(Sheets.Count)
bk.Close savechanges:=False

at the top of my code I have set the following, this sets bk to be the
workbook I have opened:

Folder = "F:\My Documents\Fantasy Football\XLS_Emails\"
FName = Dir(Folder & "*.xls")
Do While FName < ""
With ThisWorkbook
Set bk = Workbooks.Open(Filename:=Folder & FName)
For y = 1 To bk.Sheets.Count

< more code


so how does the copy work, what is copying what to where - I want to
copy the sheet from the open workbook to a new sheet in my master
workbook

Worksheets.Copy after:=Sheets(Sheets.Count)
bk.Close savechanges:=False


sorry for being dumb..
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
Import worksheets from another workbook? bjwheeler Excel Worksheet Functions 1 April 13th 09 05:24 PM
Rename worksheets of workbook - Error code [email protected] Excel Programming 1 July 21st 07 12:31 AM
macro to execute code on all worksheets in workbook Gretta Excel Programming 5 April 5th 07 03:44 PM
Import two worksheets into workbook dan Excel Programming 1 July 24th 06 03:51 PM
Run code for multiple worksheets in a workbook & Column Formatting Shaun[_3_] Excel Programming 1 January 7th 04 08:35 PM


All times are GMT +1. The time now is 04:02 PM.

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

About Us

"It's about Microsoft Excel"