Excel VBA - Ambiguous Name Prompt - excel

I get the ambiguous name prompt with this excel vba:
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
ActiveWorkbook.CustomDocumentProperties(“DepartmentCode”).value = Range(“DepartmentCode”)
ActiveWorkbook.ContentTypeProperties(“DepartmentCode”).value = Range(“DepartmentCode”)
End Sub
There is another Public Sub Workbook_BeforeSave event in the same workbook which is also required. How do I work around this.
I have no knowledge of VBA. I found the string above on google. I am basically trying to synchronize data in excel with columns in SharePoint.
Would appreciate help on this.
How can I work around it? Sorry, I have no knowledge of vba. Just copied from google

Copy all the code in one event (that is, everything after the Private Sub line and before the End Sub line) and paste it into the other event, either right after the Private Sub or right before the End Sub. Then remove the whole event you copied from.

Related

Information About method like Workbook_afterSave

I am Doing project using excel.
In Excel is there any method like Workbook_afterSave means if i change the cell value and save my workbook then according that cell value functions will be execute.
So anyone can suggest me method like workbook_afterSave in ThisWorkbook module?
Thanks In advance.
Here is an example using the Workbook_AfterSave event:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
If Success Then
MsgBox ("The workbook was successfully saved.")
End If
End Sub
Here's a list of all Excel events.

Compile Error Opening Workbook in Excel 2007

I have a code that I am running that is stored in ThisWorkbook. It was originally Private Sub Workbook_Activate and Private Sub Workbook_Deactivate. This was causing some unexpected results when switching between open workbooks. So I have changed the code to what is below. Only now, I get a compile error message (the Private Sub Workbook_BeforeClose is highlighted when I get this error message) that says:
Compile error:
Procedure declaration does not match description of event or procedure having the same name.
I am not savvy enough in VBA to understand this error message or a good fix for it. All help and suggestions are welcome.
Private Sub Workbook_Open()
Call AddToCellMenu
End Sub
Private Sub Workbook_BeforeClose()
Call DeleteFromCellMenu
Call DeletePopUpMenu
End Sub
It should be:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
The Cancel parameter is there to let you prevent the workbook from closing - e.g. if certain conditions haven't been met. Setting Cancel to True stops the workbook from closing - see the Workbook.BeforeClose documentation

Workbook_Open() won't execute Excel 2011

Using Excel 2011 (should be same as Excel 2010)
Code is under the "ThisWorkbook" module in Excel
Events are enabled
Macros are enabled
I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.
Private Sub Workbook_Open()
ActiveSheet.Range("BL4").Value = "Open is working"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
First make sure you have put this in this in the right place and have macros enabled.
Then, try adding this line to the workbook_open method:
MsgBox "HELLO"
Do you see the msg box? You're choice of cell looks a bit strange
Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)
FInally, if a plugin calls something like this line, it could cause your events not to fire..
Application.EnableEvents = False
So make sure you have tested it with no other sheets or addins open.

Saving Personal.xls automatically

I use Excel 2003 on a Windows 7 Professional setup.
In my Personal.xls file, I have compiled snippets of code/formulae that I have picked up from different places, to be available for ready reference when I use Excel. These contain a number of volatile functions such as cell(), rand(), today() etc. As a result, when I close Excel, it asks me whether I would like to save Personal.xls.
I would like to keep my Personal.xls as it is, and yet disable the popup somehow. I am fine with saving, not saving, either way, as I won't be changing Personal.xls.
I have tried the following code in my personal.xls in the Workbook_BeforeClose section
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
But it doesn't seem to work. I have tried some variations, including .Save and .Saved=True, and also tried to alternatively use ThisWorkbook., Me. & Workbooks("PERSONAL.xls"). However, Excel still asks me save Personal.xls
I have also tried to disable calculations in my Personal.xls viz.
Private Sub Workbook_Open()
For Each ws In ThisWorkbook.Worksheets
ws.EnableCalculation = False
Next
End Sub
This doesn't solve the problem either. Finally I tried to do a 'ThisWorkbook.Save' after changing calculation mode to manual, but that doesn't change anything either.
Am I missing something here? Any advice would be appreciated. Thanks in advance!
You want:
Me.Save
In your workbook BeforeClose event.
Why not just force-shut the document?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close savechanges:=True
End Sub
For what I was trying to do I am actually using savechanges:=False. Both ways worked.
I just remove the Cancel As Boolean
Private Sub Workbook_BeforeClose()
ThisWorkbook.Save
End Sub

Trap delete key

My problem is fairly trivial: I need to apply logic to the delete button in Excel. In a related question I asked for means to clear cells in a pivot table, and realising now that this might not be the right approach, this is yet another alternative I'm considering. Unfortunately, I admittedly have little experience with Visual Basic, and this task is proving such a challenge (quite surprisingly).
Is it possible to trap (as in listen to) the delete-key in Excel and trigger a subroutine in VBA when pressed?
I'd appreciate all kind of input! Thanks!
Yes.
You case use Application.Onkey to assign code to keys
If you
(1) add this code to a sheet module that contains your pivottable (right-click your sheet tab, View Code and past the code below)
This code activates and then de-activates the intercept when the user enters then leaves this particular sheet:
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "Intercept"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{DELETE}"
End Sub
(2) add this code to a regular module
Sub Intercept()
MsgBox "user just pressed delete"
End Sub
then delete on that sheet will be trapped and the user given a message

Resources