UserForm error: Method 'Properties' of object '_VBComponent' failed - excel

I want to edit my UserForm object in VBE with code.
When I use the following, I get an error as in post title:
Sub myTest()
ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub
I tried changing "Caption" to "caption", the same error. Also this error happens when I try to set Width or Height properties, not always at first time, but when I run the code again to resize.
Edit 1:
"Trust access to the VBA project object model" in macro settings is enabled.
Edit 2:
Error does not happen when new workbook and UserForm are created. It does happen when UserForm is recreated in workbook in which an error already happened.
Edit 3:
Adding Unload UserForm1 or Set UserForm1 = Nothing before doesn't help.
Edit 4:
For new workbook, if I create UserForm and run this code from a module, but after right clicking on UserForm -> View Object, then I get an error: "Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." When I confirm, on consecutive macro runs I'm getting an error as in post title.
Edit 5:
The same happens on Excel 2010 and 2016.

May simply try this
Sub myTest()
ThisWorkbook.VBProject.VBComponents("UserForm1").Activate
ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub
It is working in Excel 2007 and expect to work in any version.
May Please refer my Answer to Post After Import of a Userform into VBComponents Properties cannot be read

Related

VBA userform listbox right click menu?

I'm creating a userform in excel. I've created the form, I've populated it with data using a Listbox that reads from a range in excel.
Now I'm trying to add the ability to right click on a row in the Listbox and then perform an action on that line of data form there. Is this possible? I had assumed it was so I was planning on:
Creating a custom popup menu
Working on setting up some sort of event in the userform on right click
Everything else.
I'm stuck on step 1 above. I started by trying to create a custom right-click menu by using the code from Microsoft's website on "showpopup".
https://learn.microsoft.com/en-us/office/vba/api/office.commandbar.showpopup
Private Sub UserForm_Initialize()
Set myBar = CommandBars _
.Add(Name:="Custom", Position:=msoBarPopup, Temporary:=False)
With myBar
.Controls.Add Type:=msoControlButton, ID:=3
.Controls.Add Type:=msoControlComboBox
End With
myBar.ShowPopup
End Sub
This is my code currently, I created a new userform and just pasted the code verbatim from MS's website. When I run the code I get this error:
"Run-time error: '5': Invalid procedure call or argument"
This is the line that's causing the error:
Set myBar = CommandBars _
.Add(Name:="Custom", Position:=msoBarPopup, Temporary:=False)
I'm at a loss on what the error is. Am I on the wrong track here? Am I just doing this whole thing completely wrong?
The reason for the error is that the CommandBar already exists ... before the line that is erroring, add:
On Error Resume Next
CommandBars.Item("Custom").Delete
On Error GoTo 0
... you should also really run the same lines to delete the CommandBar when your UserForm is no longer in use, maybe in the Terminate event
... also your code, as it stands, shows the CommandBar immediately, you likely want to move the myBar.ShowPopup line elsewhere (eg to the appropriate event handler for the ListBox)

VBA File/Path error when ControlSource property of checkbox form is present

I faced Path/file access error when I load the form that has a checkbox with ControlSource property filled. To reproduce issue you can get this file or follow steps below:
Create and open a blank xlsm workbook.
Create Userform1 and add a checkbox to it.
Assign value Sheet1!A1 to ControlSource property of checkbox.
Add following codes to ThisWorkbook module:
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Save and close.
Open workbook. You see the form without error.
Without any doing, close the form and the workbook.
Re-open the workbook. You see Path/file access error
EDIT:
See the problem in screen recorded video from here.

Excel VBA Userform Initialize Error

I've had an Excel VBA addin I've developed over many years. Many users have used this addin on Windows on both Excel 2007 and Excel 2010; it has even worked on Macbook. For the first time, a user is getting an error in Windows Excel 2010 when clicking a button I've added to a ribbon to bring up a form.
The error is 'Compile error: Variable not defined'. I am using Option Explicit throughout the code, but there is no variable being defined when the userform initializes. I've compiled from within the VBA editor using Debug...Compile VBAProject and get no errors. I've googled this and read many posts but nowhere has this error been associated with a userform initializing.
I have this code in the click event of a button I've added to a new ribbon:
Sub DisplayForm4Calc1(ByVal control As IRibbonControl)
On Error Resume Next
frmAA.Show
End Sub
I have this code in frmAA, just initializing some controls:
Option Explicit
Public myPrevious As Boolean
Private Sub UserForm_Initialize()
On Error Resume Next
refNumerator.value = ""
refDenominator.value = ""
MultiPage.value = 3
End Sub
The error is highlighting the line 'Private Sub UserForm_Initialize()'.

Method 'Close' of object _Workbook failed when using UserForm in Excel 2011

I have already looked at these two posts:
Closing a Userform with Unload Me doesn't work
Error when closing an opened workbook in VBA Userform
They both suggest that when you want to close a file from Form code, you need to Unload the Form first (using Unload Me). However, if I Unload, I have a global array that's getting dereferenced.
Take a look at my code below though (which crashes on assigning global_int(0,0) to test). I can't Unload the Form unless I remove the array. Is this really the only solution to this problem?
Create a fresh excel file. In it, create a Userform. On that, create a Command Button with the following Click event code and global declaration:
Private global_int(2, 10) As Integer
Private Sub CommandButton1_Click()
global_int(0, 0) = 23
Dim filename As String
Dim opened_workbook As Workbook
filename = Application.GetOpenFilename() ' User selects valid Excel file
Set opened_workbook = Application.Workbooks.Open(filename)
' File operations would occur here
Unload Me
opened_workbook.Close ' Exception thrown here
Dim test As Integer
test = global_int(0, 0)
MsgBox "If you got here, it worked!"
End Sub
I'm just adapting someone else's code to work on a Mac, so I'd like to avoid completely refactoring if possible.
Thanks.
based on what I can understand is you have a userForm and the code is inside there. You can't unload the user form from inside the userForm code and expect the rest of the code to work. One option would be to write the code in a separate module. Call the user form to run from there

Error when closing an opened workbook in VBA Userform

In a subroutine, I want to open a workbook, do some reading from it, and close it.
For some reason, I get an error:
Run-time error '1004':
Method 'Close' of object _Workbook failed
I have identified a minimal code snippet to reproduce the problem.
Create a fresh excel file. In it, create a Userform. On that, create a Command Button with the following Click event code:
Private Sub CommandButton1_Click()
Dim filename As String
Dim opened_workbook As Workbook
filename = Application.GetOpenFilename() ' User selects valid Excel file
Set opened_workbook = Application.Workbooks.Open(filename)
' File operations would occur here
opened_workbook.Close ' Exception thrown here
MsgBox "If you got here, it worked!"
Unload Me
End Sub
What really perplexes me is that this error doesn't happen with the same code when the Command button is not on a userform (on a plain button straight on the worksheet).
I don't even know what else to report or where to look to explain this behavior (besides StackOverflow!). I'm writing VBA using Excel for Mac 2011 and can move to Windows Excel 2010 if it makes a difference.
Yes, in Excel 2011, it is a bug (Undocumented - I haven't found a documentation for it yet). You have to slightly modify the code. Try this
Private Sub CommandButton1_Click()
Dim filename As String
Dim opened_workbook As Workbook
filename = Application.GetOpenFilename() ' User selects valid Excel file
Set opened_workbook = Application.Workbooks.Open(filename)
Unload Me
opened_workbook.Close
MsgBox "If you got here, it worked!"
End Sub
I had this exact problem on Excel 11 on Mac (Worked fine Excel 2013 on Windows), only the error occurred in a module sub that was called from the UserForm.
If somebody (like me) is trying to use the workbook.close method from a sub/function in a module (or another location) that is not inside the UserForm itself you can't use 'Me'. 'Me' is only usable within the UserForm code itself.
Instead of 'Unload Me' use the unload function and the name of your UserForm.
Unload UserFormName

Resources