Fergus
Well-known Member
- Joined
- Mar 10, 2004
- Messages
- 1,174
Based on what I have culled from various posts on the Board I am trying to write a code to make a workbook save itself under a new name and then clear the contents of some cells. I want it to do this every time the contents of one cell are changed. The changing of this cells' contents will also cause another cell to change and this latter cell will be used as the name for the newly saved workbook.
So far I have managed to get the code to work as a macro which I can initiate from the Tools menu, but when I try to make it a worksheet change event nothing happens.
Below is the code as far as I have managed to get it. Can anyone tell me why it's not working when the contents of cell L5 are changed.
Any help will be most gratefully received.
So far I have managed to get the code to work as a macro which I can initiate from the Tools menu, but when I try to make it a worksheet change event nothing happens.
Below is the code as far as I have managed to get it. Can anyone tell me why it's not working when the contents of cell L5 are changed.
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$L$5" Then
Application.EnableEvents = False
Dim rng As Range, ac As Integer, dn As Integer, i As Integer
Application.ScreenUpdating = False
If Dir("D:\My Data\Test Bed Excel\" & "Muster_" & [R5].Value & ".xls") = "" Then
Set rng = Range("H12,Q36")
dn = 1
Do While dn < 25
ac = 1
Do While ac < 11
rng.Cells(dn, ac).Value = ""
ac = ac + 1
Loop
dn = dn + 1
Loop
ActiveWorkbook.SaveCopyAs Filename:="D:\My Data\Test Bed Excel\" & "Muster_" & [R5].Value & ".xls"
MsgBox "your new weekly Muster has just been saved!"
Workbooks.Open "D:\My Data\Test Bed Excel\" & "Muster_" & [R5].Value & ".xls", 3
Workbooks(1).Activate
ActiveWorkbook.Close SaveChanges:=False
Exit Sub
Else
i = MsgBox("Overwrite existing file...?", vbYesNo)
Application.DisplayAlerts = False
If i = 6 Then
ActiveWorkbook.Save
Workbooks.Open "D:\My Data\Test Bed Excel\" & "Muster_" & [R5].Value & ".xls", 3
Workbooks(1).Activate
ActiveWorkbook.Close SaveChanges:=False
Else
MsgBox "Then you need to enter a new week start date"
Exit Sub
End If
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub
Any help will be most gratefully received.