What I understand from this "Workbook_BeforeClose" is that a command should work after when click "X" to close Excel.
My code is;
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Makro").Range("A2") = 0
End Sub
When I reopen this excel workbook, I want to see that Range "A2" is zero ("0"). But I reopen the excel, it is not zero. I cannot understand.
I wonder if I misunderstood this vba code.
If you help me about this, I would be appreciate you.
Try it as,
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Worksheets("Makro").Range("A2") = 0
Me.Save
End Sub
If you don't want it saved then change it to,
Option Explicit
Private Sub Workbook_Open()
Me.Worksheets("Makro").Range("A2") = 0
End Sub
Private Sub Auto_Open()
Sheets("Makro").Range("A2") = 0
End Sub
or
Private Sub Auto_Close()
Sheets("Makro").Range("A2") = 0
End Sub
codes solved my problem.
Thank you.
You can try this method:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Sheets("Sheet1").Range("a2")
.Value = "ABC"
End With
End Sub
Found this question trying to fix my issue, turns out their code was correct.
There's no need for Option Explicit or a Save
Just make sure you have the
Private Sub Workbook_BeforeClose(Cancel As Boolean)
in the ThisWorkbook Window. It won't work if it's in one of the sheets or modules.
Related
Is it possible to get the same effect using ThisWorkbook.Application.Visible = False but only for one Workbook. I mean, I'd like to limiting user interaction only to UserForm, but I need have an access to anothers workbooks. At the moment this function cause hide workbook, but after open some another excel file - all object from userform are not available.
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
Starter.Show modeless
End Sub
Thanks for your support.
Please, create a form, let us say "Starter", having (at least) a button ("btExit"), copy the next code in its code module and show it. If the form in discussion already has some code in Initialize and Terminate events, please add the next code lines, too:
Option Explicit
Private Sub btExit_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
So, you can simple use workbook Open event in this way:
Private Sub Workbook_Open()
Starter.Show vbModeless
End Sub
SOLVED
Is there any way to check if workbook is closing when the code is in workbook_deactivate procedure? so i can inform a different message to users depending on whether they are just leaving to another workbook or they are closing the file. like following
Private Sub Workbook_Deactivate()
if thisworkbook.closing then
msgbox "message1"
else
msgbox "message2"
end if
End Sub
i've searched on the net but no solution at all.
so any help would be appreciated
SOLUTION
i've thought of a trick. i'm putting the value 1 in Z1000(if it is available) in before_close event and in deactivate, i'm checking if Z1000's value. that's it.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Range("Z1000").Value = 1 'wherever is avaliable
Me.Saved = True
End Sub
Private Sub Workbook_Deactivate()
If Range("Z1000").Value = 1 Then
MsgBox "quitting"
Else
MsgBox "deactivating"
End If
End Sub
You can detect that using the BeforeClose Event
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' set Cancel to true to prevent it from closing
End Sub
I have a combo box drop down with search suggestions, made from code here:
http://trumpexcel.com/2013/10/excel-drop-down-list-with-search-suggestions/
It works very well, but when I'm on another sheet and pressing "Enter", the search field randomly pops up in the sheet
It's not even the full box, just the blue field
Any insights on disabling it? The only success I've had is turning calculation to manual, but the workbook needs automatic calculation
Thanks!
I ran into a similar issue with my own VBA version of a smart search bar. How I fixed it was by doing the following:
Private Sub ComboBox1_Change()
If ComboBox1.Value = "" Then Exit Sub '<------ Problem solved.
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
OR
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim SheetWithComboBox As Worksheet: Set SheetWithComboBox = ThisWorkbook.Sheets(1)
If ThisWorkbook.ActiveSheet.Name <> SheetWithComboBox.Name Then
ComboBox1.Visible = False
Else: ComboBox1.Visible = True
End If
End Sub
#Tyeler
Thanks for your help, your thinking helped me think of a way
Private Sub ComboBox1_change()
Dim sht1 As Worksheet
Set sht1 = Worksheets("xxx")
If ThisWorkbook.ActiveSheet.Name = sht1.Name Then
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
Call macro1
Else: Exit Sub
End If
End Sub
I found a solution that, at least for me, works for all sheets.
Private Sub Combobox_Get_Focus()
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
I have a script that refreshes my data connection to a SQL server. I need to the macro to run when the workbook is opened but for some reason it will not. I guess it has to do with the connection refresh. I tried with both Workbook_Open and Auto_Open() and neither are working. Other than the data load the only thing I am doing is filtering and copying data, nothing exotic.
Does the data refresh need permissions outside of the script, is that the problem?
Here are the first couples lines.
Sub Auto_Open()
ActiveWorkbook.Connections("Connection Name").Refresh
other stuff
end sub
You are putting the code in 'ThisWorkbook' object, right.
Private Sub Workbook_Open()
ActiveWorkbook.RefreshAll
End Sub
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub
user6058587 was close, but you just need to add this to ThisWorkbook:
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub
In VBA for excel, I have a userform then I want this to show only for 1 instance. Even if the user re-open it, it won't open again. Is there any code for it? well, I'm also using this code for my login:
Private Sub UserForm_Initialize()
User.Caption = Environ("Username")
End Sub
I'm thinking if i can use this code in my problem. Hoping for a quick response. thanks guys, you're awesome!
Yes, it's possible.
You have to add new sheet. In a cell A1 type 0 (zero), then hide it. In a code which calls UserForm, use this:
Sub ShowMyForm()
If ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=0 then MyUserForm.Show
End Sub
In a form:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=1
ThisWorkbook.Save()
DoEvents
End Sub
If you don't want to add an extra sheet just to store one bool, you can set a custom document property like this:
Private Sub Workbook_Open()
On Error Resume Next
Dim test As Boolean
test = Me.CustomDocumentProperties("UserFormShown").Value
If Err.Number = 0 Then Exit Sub
UserForm1.Show
Me.CustomDocumentProperties.Add "UserFormShown", False, msoPropertyTypeBoolean, True
End Sub
If the property hasn't been set yet it will throw an error, so trapping an error lets you know if you've set the property (and shown the form).