Hi All,
I currently have a macro set up to run once a workbook is saved/ closed. This works fine unless tehre is more than one workbook open. When I try it with more than 1 different workbook open I get a run error.
My code begins:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
and then performs the macro. I don't really want to specify the filename as this is liable to change. Is there anyway of adding a "this workbook" command to the text string, and if so where would it go?
Thanks
Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
i had the same problem see here
http://www.excelforum.com/excel-prog...workbooks.html
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
Hi,
I hope you are using ThisWorkbook everywhere in the code.
If you post the entire code, I can try to sort it out. Please give it in Code Tags so that it is easy to read.
--Karan--
Hi Karan,
please see the below code
ThanksPrivate Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Sheets("Main").Select ActiveSheet.Unprotect ("password") Dim lRow As Long, lCounter As Long lRow = Cells(Rows.Count, 1).End(xlUp).Row For lCounter = 2 To lRow If Cells(lCounter, 17) = "X" Then With Cells(lCounter, 13).Resize(1, 3) .Value = .Value End With End If Next lCounter Range("A5:O310").Select Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="password" Range("B5").Select Sheets("Open").Select End Sub
Use this code:
--Karan--Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) ThisWorkbook.Sheets("Main").Select ThisWorkbook.ActiveSheet.Unprotect ("password") Dim lRow As Long, lCounter As Long lRow = Cells(Rows.Count, 1).End(xlUp).Row For lCounter = 2 To lRow If ThisWorkbook.ActiveSheet.Cells(lCounter, 17) = "X" Then With ThisWorkbook.ActiveSheet.Cells(lCounter, 13).Resize(1, 3) .Value = .Value End With End If Next lCounter ThisWorkbook.ActiveSheet.Range("A5:O310").Select Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal ThisWorkbook.ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="password" ThisWorkbook.ActiveSheet.Range("B5").Select ThisWorkbook.Sheets("Open").Select End Sub
many thanks, will give it a go.
Anytime![]()
Ok, sort of works. The problem is – if I have 2 workbooks open (both with the same code) and I click on the big red X – and select “Yes to all” on the save option it still flags up and error on one of the workbooks.
now did you bother to read the link i gave you?
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks