Excel VBA Userform Initialize Error - excel

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()'.

Related

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

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

Using the value of a textbox in a userform to activate another textbox on a worksheet

Firstly, I am as beginner as beginner gets.
I am trying to take the value from a text box embedded in a user form and then using it in another line of code to activate another text box on a worksheet.
Private Sub CommandButton1_Click()
UserForm2.Hide
Dim FieldName As String
FieldName = UserForm2.TextBox1.Value
Worksheets("Quote").FieldName.Activate
End Sub
I hope you can see what I'm attempting from this code. The error is "Run-time error '438': Object doesn't support this property or method".
The value obtained from the textbox is identical to the name of the other one and has been tested to see if it can be retrieved or not.
If someone could tell me the correct functions to use, it'd be much obliged.
Concerning the name of your Sub - Private Sub CommandButton1_Click, I assume that the button is in a form and the TextBox is in a worksheet. In general, there are two types of TextBoxes in Excel - ActiveX control or a Form control and they are handled differently:
What is the difference between "Form Controls" and "ActiveX Control" in Excel 2010?
If you are not sure whether the TextBox is an ActiveX control or a ControlElement, then the following would work. Somehow.
Private Sub CommandButton1_Click()
On Error Resume Next
Dim NameOfTextBox As String
NameOfTextBox = UserForm2.TextBox2.Value
Worksheets(1).OLEObjects(NameOfTextBox).Activate
Worksheets(1).Shapes(NameOfTextBox).Select
Unload Me
On Error GoTo 0
End Sub
Caution: This is a good example of a "quick and dirty" code, consider only using it for learning reasons.
In production, it is really a good idea to use the Forms as they are supposed to be used (see https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form) and try to avoid On Error Resume Next.

1004: Application-Defined or Object defined error after switching from Excel 2013/2016 to 2010

it's a very simple macro and I read tons of threads, none of which resolves my case. I get the .Caption line highlighted. I tried to Dim the form as new form and it does not help as well.I have a form which I want to call from a button which is apparently not done correctly from my side.
Sub BtnAdd_Click()
frmAddCB.Caption = "Add Transaction"
frmAddCB.Show
End Sub
Cheers,
When executing this step by step with F8, just before the line that was highlighted on my standard module when executing with F5, it jumps to the Private Sub UserForm_Initialize() sub which is in the Form code itself. In that Initialize sub I had this line of code which was causing the error in Excel 2010:
TransTypeComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("TransType").RefersToRange))
After I changed this to the old-school way with a loop, it started working properly. Probably something about my dynamic range or something about the RefersToRange is not working the same way after Excel 2010.
For Each TT In Range("TransType")
With Me.TransTypeComboBox
.AddItem TT.Value
End With
Next TT

VBA Add-In Not Working

I have a fully functional Macro that I'm trying to convert to an Add-In so I can use it on all excel documents. However I can't get it create a menu. Please Help.
First Things First:
I'm using Office 2011-Excel on a Mac running Mountain Lion
My .xlam file is saved in the correct folder Applications/Microsoft Office 2011/Office/Add-Ins
I've installed it correctly using the Tools/Add-Ins menu
My code is in the This Worksheet section of the .xlam file
My Add-In's source code is viewable from all documents
My code as follows:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_Open()
On Error Resume Next 'Just in case
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete 'Delete any existing menu item that may have been left.
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add 'Add the new menu item and Set a CommandBarButton Variable to it
With cControl 'Work with the Variable
.Caption = "P Wave"
.Style = msoButtonCaption
.OnAction = "runSheet()"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete
On Error GoTo 0
End Sub
I can't get my Macro menu to appear. Please help
Add your command buttons, or even your own Tab, on the Ribbon although you may have to learn a bit about RibbonX XML. It will give compatibility back to Excel 2007 (I think).
This link may get you started.
Also, This visual designer may be more exciting to get you going. I haven't used it but Andy Pope is the man on all things VBA!

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