I have some VBA code in an Excel workbook.
I have a cell which I would like to use to close the workbook without saving (instead of using the usual (X) close button on the top right corner of excel.
If I close the workbook using the (X) button, it works great the following code.
However if I press the "CLOSE" button cell which is on the worksheet, it gives a 1004 error.
Can anyone help?
ON WORKSHEET WHERE BUTTON IS
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Cells(ActiveCell.Row, ActiveCell.Column) = "CLOSE" Then
CloseNoSave
End If
End Sub
IN THISWORKBOOK
Private Sub Workbook_BeforeClose(Cancel As Boolean)
CloseNoSave
End Sub
IN MODULE
Sub CloseNoSave()
ThisWorkbook.Close savechanges:=False
End Sub
Why don't you just use a single piece of code on your Selection Event
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "CLOSE" Then
ThisWorkbook.Saved = True
ThisWorkbook.Close
End If
End Sub
Related
I created macros in vba but they don't work.
In the ThisWorkbook code I wrote:
Private Sub Workbook_Open()
Worksheets("Sheet1").Activate
End Sub
In the code of Sheet1 (Sheet1) I wrote:
Private Sub Worksheet_Activate()
MsgBox ("hello")
End Sub
but when I open the file nothing happens ...
If Sheet1 is already active when you open the workbook, then
Worksheets("Sheet1").Activate
does exactly nothing. Because you cannot activate what is already active.
Test it by adding 2 Sheets Sheet1 and Sheet2. Then select Sheet2 save the file and close it. Now open it again, it will run Worksheets("Sheet1").Activate and this will trigger the Worksheet_Activate properly.
Also note that MsgBox ("hello") should be without parenthesis as it does not return a value to a variable: MsgBox "hello"
An alternative solution is:
Write in a module:
Public Sub Sheet1_Activate()
MsgBox "Sheet1 Activate"
End Sub
In Sheet1 write:
Private Sub Worksheet_Activate()
Sheet1_Activate
End Sub
And in ThisWorkbook write:
Private Sub Workbook_Open()
If ThisWorkbook.ActiveSheet.Name = "Sheet1" Then
Sheet1_Activate
Else
ThisWorkbook.Worksheets("Sheet1").Activate
End If
End Sub
The idea is to have a new procedure Sheet1_Activate that takes the actual code and is triggered by the Worksheet_Activate as well as by the Workbook_Open in case the sheet is already the active sheet.
Sub auto_open()
MsgBox "HueHueHue"
End Sub
You can't activate the same thing twice.
I want to auto-run this private sub when opening the excel sheet.
I tried using Private Sub Workbook_Open() method but as the first private sub does not have a name, it does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
'
' HideF Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG()
'
' HideFG Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
I hope that it automatically checks cell W16 when opening the excel file and carries on with HideF macro or HideFG macro. Currently, the two macros run once you actual type on the cell after opening the file.
the easiest way is to use the default Module "ThisWorkbook" which gets executed when opening the excel file. You can find it within your VBA Project Explorer on the left side of the window.
Just take the sub you want to execute and copy it into the space.
Its explained in great detail here:
https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
If it is necessary for your usecase this can help you to call a private sub:
Private Sub PrivateCallDemo()
'Module2
Application.Run "Module1.Worksheet_Change"
End Sub
This way your actual Sub could stay in another Module.
You have a few problems. First you don't want Worksheet_Change(ByVal Target As Range)
as that is for events triggers on changes to the workbook, you want Workbook_Open(). This gets stored under ThisWorkbook not a separate module/sheet.
Here is working code, I commented out your ws declaration for testing.
Private Sub Workbook_Open()
'Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
MsgBox "HideF"
End Sub
Sub HideFG()
MsgBox "HideFG"
End Sub
Here is a screenshot of my editor.
G.M. posted a great resource as well found here --> https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
I just put the modules in the same spot for the screenshot, but you can put them separately and still use the Call HideFG method if you want to store your modules separately from the workbook_open event as I would want to.
Is there any way to change Excel cursor for specific Workbook? Can't find any solution for this.
My Workbook is locked for editing cells, only certain cells are available and I want to replace this Excel cursor:
By pointer:
I have went through this but don't understand is it possible to implement it in Workbook?
https://learn.microsoft.com/en-us/office/vba/api/excel.application.cursor
Private Sub Workbook_Open()
Application.Cursor = xlDefault
End Sub
In ThisWorkbook code module, you could do something like this. The idea here is that we capture the initial cursor style when the workbook is opened (the Workbook_Open event calls on changeCursor which stores the current cursor style in the cursor variable. Then we add some more event handlers so that when the workbook loses focus (Workbook_Deactivate) and before it closes (Workbook_BeforeClose) we restore that cursor to its previous style via the resetCursor method. There may be additional edge cases that I haven't considered, but this is the general idea you'll need to implement.
Option Explicit
Private cursor As Long
Private Sub Workbook_Activate()
changeCursor
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
resetCursor
End Sub
Private Sub Workbook_Deactivate()
resetCursor
End Sub
Private Sub Workbook_Open()
changeCursor
End Sub
Private Sub changeCursor()
cursor = Application.cursor
Application.cursor = xlNorthwestArrow
End Sub
Private Sub resetCursor()
On Error Resume Next
Application.cursor = cursor
If Err.Number <> 0 Then
Application.cursor = xlDefault
End If
End Sub
Scenario
I have a userform whereby excel workbook will be hidden while opening using the following method of Application.Visible = False. These are the codes
My userform
show excel button is Commandbutton1
hide excel button is Commandbutton2
This workbook
Codes
Private Sub Workbook_Open()
Call hideExcel
UserForm1.Show
End Sub
Userform1
Codes
Private Sub CommandButton1_Click()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = True
Else
Application.Visible = True
End If
End Sub
Private Sub CommandButton2_Click()
Call hideExcel
End Sub
Sub UserForm_Initialize()
Call hideExcel
End Sub
Private Sub UserForm_Terminate()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = True
Else
Application.Visible = True
End If
End Sub
Sub userform_click()
Call hideExcel
End Sub
Module
Codes
Sub hideExcel()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = False
Else
Application.Visible = False
End If
End Sub
Problem
The problem I am facing is
Open my macro and userform activated. Lets call this file A
Then open another workbook. Lets call this file B
Tried to close file B while workbook A is hidden. But there is a prompt to close file A also and eventually all excel will be closing including my macro file which is A.
Does anyone know what is the problem here?
I don't understand where the problem is? If you are closing last visible (not hidden) workbook, Excel also tries to close all other open workbooks (even if they're hidden). And I think it's normal Excel behavior. You can only avoid to see a prompt, e.g. by setting up Workbook.Saved property to True or by setting up Application.DisplayAlerts property to False or just by saving workbook before closing.
If you don't want to close hidden workbook you just have to make it visible before closing the second workbook.
I'am trying to prevent users from pasting other things than values in the template I'm developing. I use a macro to always paste values in the worksheet (see below). When users switch to another workbook this macro should be disabled.
The problem is that I get error 91 when activating another workbook.
'the macro in a module
Sub AlwaysPasteValues()
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
'the code in this workbook
Public wb As Workbook
Private Sub Workbook_Activate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:="v"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set wb = Nothing
End Sub
Private Sub Workbook_Deactivate()
With wb
.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End With
End Sub
Private Sub Workbook_Open()
Set wb = ThisWorkbook
End Sub
You are changing .MacroOptions on wb which does not exist as a property. MacroOptions is for the Application. Use the same code as the Activate and you should be good.
Private Sub Workbook_Deactivate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End Sub