Can't check ActiveX Toolbox after saving and reopening worksheet - excel

I'm creating an Excel vba-based application in which specific data should be shown, and for filtering this information I'm trying to use ActiveX Chekboxes.
After I create the checkboxes I can check/uncheck them normally, and I use some macros to perform somethings I want, for example, to make sure only one option can be checked I use:
Private Sub CheckBox1_Click()
If Worksheets("Cover").CheckBox1.Value = True Then Range("O8") = 1
If Worksheets("Cover").CheckBox1.Value = False Then Range("O8") = 0
If Worksheets("Cover").Range("O8") = 1 Then
Worksheets("Cover").CheckBox2.Value = False
Worksheets("Cover").CheckBox3.Value = False
Worksheets("Cover").CheckBox4.Value = False
End If
End Sub
The problem is that, after I save this worksheet and reopen it to start working again, I can't check or uncheck the boxes anymore. I also can't format them on Designer Mode anymore (if I right-click one of the checkboxes and select 'Properties' a window to change the properties of the active worksheet appears). And, of course, the macros don't work anymore (Runtime error '438').
Does anyone have a clue about what may be the problem and how to solve it?
Thanks in advance!

Related

Excel VBA - Save Userform Changes

Is it possible to save changes to a Label Caption in a userform in Excel VBA, so that they are permanently saved, and only changed when you enter a new change?
I have checked, that the code is changing the caption, but I cannot get it to stick, so that it is still there next time I open the userform.
Thank you in advance
Private Sub cmdSubmit_Click()
'resets participants email and name
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
Check out this userform example and you'll find answers to many of your questions.
Source of userform
Forgive the lengthy answer initially, but there is a bit of a risk involved with making permanent changes to UserForms through VBA.
In order to change the caption of a Label (or UserForm or any other Control permanently, you will have to "Trust access to the VBA project object model" in order to do it via VBA code. Now, while this is possible it is not usually recommended because it can seriously put a user's PC at Risk should they encounter a Macro developed for nefarious purposes.
(To clarify in case the question is raised, your end user(s) will have to make this Trust setting change on their PC's as well . . . you cannot make the change on your PC, setup the code to work and then hand the file over to another user and have it work on their PC without them making the same change.)
There are methods to do this programmatically, but, this falls into the "nefarious Macro" rabbit hole and would need to be disclosed to the end user you are making this change. . . Research at your own risk.
If you are OK with putting yourself at risk, you can do it using VBA similar to the following snippet I found here. You will have to substitute your UserForm name and Label name as appropriate. I tested it on my own UserForm and it works as expected.
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub
You will need to do some research on how to "Trust access to the VBA project object model" yourself and understand the risks to do this in order for the above code to work.
If I understand the intent of what you are trying to accomplish correctly though, you could achieve this effect without having to put yourself or your end user(s) at risk.
(Most end user(s) typically would not have direct access to the VBA Designer where they would see the UserForm's un-initialized environment.) To do this, you would have to place your code in the UserForm's event.
The following assumes your Me.optProg.Value and Me.optTester.Value are Option Buttons which a user would change. If you create a "Settings" sheet in the file, you can place values in the cells of this sheet and then hide it so the users do not modify them directly. Then, reference the values of the cells and change the appearances of the Option Buttons at the same time as the UserForm's launch. (Additionally, you can set the Click events of the Option Buttons to change the values of the same cells and provide that change to affect the UserForm's Initialize event when called, but this should get you going in the right direction.)
Sub UserForm_Initialize()
'The Range below is completely up to you.
'Since you are using Boolean True/False, a simple "1" or "0" _
is easy to use to make the changes.
If Thisworkbook.Sheets("Some_Settings").Range("A1").Value = "1" Then
Me.optProg.Value = True
Else
Me.optTester.Value = True
End If
'do some other code here as needed to finish initializing the UserForm
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub

How to deselect all other checkboxes when one is selected

I’m writing a simple macro and am a bit rusty in my VBA skills. The macro has a list of options with “yes”, “no” and “maybe” options to choose from. I used checkboxes rather than option buttons so each option doesn’t have to be in an individual frame.
The code I have written (below) deselects the other options when one is selected, but I have to select it again for it to actually be selected which will be frustrating for the user.
Private Sub Yes_Click()
No.Value = False
Maybe.Value = False
End Sub
I have tried adding the additional line
Yes.Value = True
But this then crashes excel when the macro is run.
I can’t find any advice for this problem so any help is greatly appreciated.

Why additional excel window appears

Scenario
I am using a macro whereby I use Application.Visible = False to hide the workbooks. Also I use Application.Visible = True to unhide the workbook. At certain situation, I use Windows(ThisWorkbook.Name).Visible = False and Windows(ThisWorkbook.Name).Visible = True to hide and unhide only the workbook which contains macro.
Problem
I noticed during these operations, some additional excel windows(without any workbook) appear other than the workbook. Please see the picture below. You can see a grey window behind with a name Excel. That is the window I am talking about
If I closed that window, the whole excel will close. Does anyone know why this extra window appearing and how to prevent it from appearing?
I am not sure if this will meet the needs of your specific situation. But, what if you kept Application.Visible = False at the beginning of your code and changed Application.Visible = True to
Application.Windows(ThisWorkbook.Name).Visible = True at the end. This worked for me.
Hiding Excel
With the following code
Sub AppVisibleTrue
Application.Visible = True
End Sub
Sub AppVisibleFalse
Application.Visible = False
End Sub
you are showing or 'hiding' the 'whole' excel application, so you have to 'unhide' it in the same code, otherwise you won't be able to use the open files after you hide it, e.g. open a new workbook, in VBE add a new module and paste the above code into the module. Now, stay in VBE!!! Run the 'False' Sub. You will notice Excel has 'vanished', but you can still find it in the Task Manager's processes. Now run the 'True' Sub. Excel has 'reappeared'.
The following process will make Excel 'vanish'. The only way to close it will be via the Task Manager. If you not too familiar with doing this, just take my word for it.
Close the VBE. Now run the 'False' Sub. Excel has 'vanished'.
To conclude, this is obviously an error in your code, so I would suggest if you want to show a window (worksheet) that isn't 'ThisWorkbook' and you're dancing from one to the other, you should declare a variable
Const strSheet as String = "Sheet2"
Dim oSheet as Worksheet
'...
Set oSheet = ActiveWorkbook.Worksheets(strSheet)
Now you do with oSheet whatever you want.
Try to lose Active, Select and similar methods in the code.
If you would provide the actual scenario with the codes, a better assessment of the problem could be done.
The additional Excel window(s) could be related to your Windows Explorer. If you are previewing a document (in this case an Excel document) then the application to view that document is running in the background.
In this case, forcing the application to be visible will also make the background "preview" windows visible.

Disabling option buttons based upon results of prior option buttons: Excel

I am working on a project that requires me to disable certain option buttons in excel 2010 based on the results of prior option buttons. Some criteria will need multiple if statements, similar to a nested if. Some info; cell B107 in my example reflects which of the 5 option buttons they might have chosen. I have tried the following code but I have not been able to find success. Additionally, how would I make the disabled option button appear grey.
Private Sub OptionButton22_Change()
If Range ("B108") = 1 Then
OptionButton22.Enabled = False
Else
OptionButton22.Enabled = True
End If
End Sub

Unhide Excel Application Session

I have an Excel VBA method (I didn't write it) that runs and one of the first things it does is hide the Excel session Application.Visible = False.
However, when the method has finished, it does not unhide the Excel session so it remains open and listed in the Task Manager but is hidden and seemingly unusable.
Does anyone know, without have the VBE open (so one can access the Immediate Window and run Application.Visible = True), how to unhide this Excel session? At the moment, I'm simply having to kill the session using the Task Manager.
This isn't a massive deal but I'm just interested if anyone knows how to resurrect such a session.
Like I said, it's not a big deal but was just interested if anyone knew of shortcut key or anything to bring it back.
There is no shortcut as such that I am aware of but you can do this.
Open MS Word and paste this code in the VBA Editor. Close all open instances of Excel which are visible and then run and this code. This will make a hidden instance visible. Manually close the instance and repeat the process if there are more instances.
Option Explicit
Sub Sample()
Dim oXLApp As Object
'~~> Get an existing instance of an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
On Error GoTo 0
oXLApp.Visible = True
Set oXLApp = Nothing
End Sub
I am not deliberately using a loop as the hidden instance can have a workbook which you might like to save?
If you want you can convert the above code to a VB Script document which you can directly run from the desktop.
Unfortunately, I don't have the control to make the changes required.
What do you exactly mean? Is the VBA Password Protected? If no then my suggestion is still the same as earlier
This is a case of poor programming. Even if we give a code to close
all hidden Excel instances, that won't help you. Because next time you
run that macro, you will face the same problem again. Why not edit the
existing code and add Application.Visible = True at the end? Is the
VBA password protected? – Siddharth Rout 28 mins ago
A good solution!
Open up Word, assuming you have it, and open the VBA Editor there, then open the Immediate Window (Ctrl+G) and type:
Getobject(, "Excel.Application").Visible = true
and press enter.
I had a similar problem and solved it with code line reordering.
Look for a line like this ActiveWorkbook.Close that might be the reason you cannot unhide the session.
If you can find it, put Application.Visible = True just before it and voila.
as code:
sub runthis()
dim xl as object
set xl = new excel.application 'create session
xl.workbooks.open filename:= "«yourpath»" 'open wb in the new session
xl.visible=true 'this is what you need, show it up!
'rest of the code
end sub
No need for word macro at all.
Open up another excel workbook.
Hit Ctrl+F11 to go to the VBA editor and there yoy will see the running but hidden excel file on the left.
Search the code of the hidden application file for Application.Visible = False and comment it out. Save and restart the file.
Alternatively you can get back the application to show without closing if you type Application.Visible = True in the immediate window (Ctrl+G)

Resources