Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 23
Default deleting workbooks as i go

Hi. I have a macro that merges all excel workbooks within a given folder into one master workbook (and on 1 sheet). i need to do this every day as new workbooks go into the folder. Because of this i need to delete the workbooks that i have copied so that they are not copied again the next day. Can anyone help please? I'm sure its fairly easy and maybe just a change by the "close workbook" line, but if you don't know then you don't know i guess. Thanks in advance. Here is the code i'm using:

Sub ImportData()

Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim LastRow As Long

'\\ Disable Application Defaults to remove screen flicker, messages, Etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

'\\ Set folder to work from
Path = "\\Holding folder\" 'Change path as needed
FileName = Dir(Path & "\*.xls", vbNormal)
If FileName = "" Then
MsgBox "There are no files awaiting import", vbOKOnly
Exit Sub
End If
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)

'\\ Copy from workbooks and paste into this workbook.
If Range("A4") < 0 Then
LastRow = Range("A65536").End(xlUp).Row + 1
Range("A4:G" & LastRow).Select
Selection.Copy
Windows("Merging Tool.xls").Activate
If Range("A4") < 0 Then
LastRow = Range("A65536").End(xlUp).Row + 1
Range("A" & LastRow).Select
ActiveSheet.Paste
Else
Range("a4").Select
ActiveSheet.Paste

End If

End If

'\\ Close Workbooks

Wkb.Close SaveChanges:=True
FileName = Dir()

Loop

'\\ Re-enable application defaults
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,522
Default deleting workbooks as i go

On Thursday, May 17, 2012 10:15:33 AM UTC-5, kardifflad wrote:
Hi. I have a macro that merges all excel workbooks within a given folder
into one master workbook (and on 1 sheet). i need to do this every day
as new workbooks go into the folder. Because of this i need to delete
the workbooks that i have copied so that they are not copied again the
next day. Can anyone help please? I'm sure its fairly easy and maybe
just a change by the "close workbook" line, but if you don't know then
you don't know i guess. Thanks in advance. Here is the code i'm using:

Sub ImportData()

Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim LastRow As Long

'\\ Disable Application Defaults to remove screen flicker, messages,
Etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

'\\ Set folder to work from
Path = "\\Holding folder\" 'Change path as needed
FileName = Dir(Path & "\*.xls", vbNormal)
If FileName = "" Then
MsgBox "There are no files awaiting import", vbOKOnly
Exit Sub
End If
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)

'\\ Copy from workbooks and paste into this workbook.
If Range("A4") < 0 Then
LastRow = Range("A65536").End(xlUp).Row + 1
Range("A4:G" & LastRow).Select
Selection.Copy
Windows("Merging Tool.xls").Activate
If Range("A4") < 0 Then
LastRow = Range("A65536").End(xlUp).Row + 1
Range("A" & LastRow).Select
ActiveSheet.Paste
Else
Range("a4").Select
ActiveSheet.Paste

End If

End If

'\\ Close Workbooks

Wkb.Close SaveChanges:=True
FileName = Dir()

Loop

'\\ Re-enable application defaults
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True




--
kardifflad

Look in the vba help index for KILL
  #3   Report Post  
Junior Member
 
Posts: 23
Default

sorry i can't find anything relevant under kill.
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,514
Default deleting workbooks as i go

kardifflad brought next idea :
sorry i can't find anything relevant under kill.


You need to look again! 'Kill' is the VBA command statement used to
delete files.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,514
Default deleting workbooks as i go

This sounds like a good scenario for using ADODB to grab the data from
each workbook without having to 'open' them in Excel. Once the data is
entered into your consolidation worksheet you simply delete the file
and move on to the next one (until Dir = "")! This approach will
obviate much of the overhead in tow with having to open/close each
workbook, and make your process much faster and efficient<IMO.

Here's a good place to get started using ADODB with Excel files...

http://www.appspro.com/conference/Da...rogramming.zip

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
Newbie question, XL2003, VBA deleting/adding modules in multiple workbooks KR Excel Programming 6 May 1st 06 08:43 PM
Deleting Duplicate workbooks Ozzie via OfficeKB.com Excel Programming 7 March 20th 06 09:54 AM
how prevent formula in cell from deleting when deleting value???? sh-boom New Users to Excel 1 September 30th 05 06:12 PM
deleting worksheet code in generated workbooks Winshent Excel Programming 0 September 29th 04 04:50 PM
Deleting Hyphens or Dashes from multiple cells without deleting the remaining content rmaloy Excel Programming 5 February 9th 04 01:59 AM


All times are GMT +1. The time now is 03:40 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"