How to call the textbox on the form in a module? - excel

I'm having trouble calling the textbox on the form inside a module. Every time I run it, it throws a Compile Error: Method or data not found. This is my code:
Sub formInitialize()
ThisWorkbook.txtDate.Value = ""
End Sub
Thank you for your great help!

Thank you for your time. I have already figured out my problem.
I should not be using the ThisWorkbook, I should be calling the formName. The code is as follows:
Sub formInitialize()
formName.txtDate.Value = ""
End Sub
Thank's again!

Related

Possible syntax mistake? "Expecting =" In a Sub call from a UserForm

I want to call a Sub I declared at it gives a compilation error saying that it expects a =.
The Sub call is in a UserForm_Initialize event procedure.
The code is as follows.
In a module:
Public Sub FillCb(Ar() As String, Cb As ComboBox)
Cb.Clear
For I = 1 To Application.CountA(Ar)
Cb.AddItem (Ar(I))
Next I
End Sub
In the UserForm code:
Private Sub UserForm_Initialize()
LblDate.Caption = Date
FillCb(LibrosNoPrestados, CbLibro)
End Sub
This code is giving me error.
I analized the code line by line using the debugger and commenting the las line inside the Initialize event, and it works fine up to that point. The error is thrown at compile time in the
FillCb(LibrosNoPrestados, CbLibro)
The rest of the code is not needed here since as I said it works fine, but the syntax in that last line must be wrong and I can't see the mistake.
A VBA "feature". If you are calling a sub routine without the "Call" keyword then don't use parentheses, if you use the "Call" keyword then you need the parentheses.
Eg
Call FillCb(LibrosNoPrestados, CbLibro)
Or
FillCb LibrosNoPrestados, CbLibro
Here's Microsoft's documentation:
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/call-statement

VBA clear ActiveX combobox list

How to clear Excel ActiveX ComboBox list with VBA. I expected this code would work:
Sheet1.OLEObjects(1).Clear
but it raises
runtime error object doesn't support this property or method.
I am puzzled because this works:
Sheet1.OLEObjects(1).Name
returning the name TempCombo.
I still fail when I try this:
Sheet1.TempCombo.Clear
It returns error:
Runtime error Unspecified error.
What else should I check?
If you want to clear the ActiveX ComboBox list, you may try something like this...
Sheet1.OLEObjects(1).ListFillRange = ""
Or more precisely this...
If TypeName(Sheet1.OLEObjects(1).Object) = "ComboBox" Then
Sheet1.OLEObjects(1).ListFillRange = ""
End If
If the combobox has a name, you can just refer to the name. Like
With mySheet
.cbMyComboBox.ListFillRange = vbNullString
End with

VBA Form returns Compile error method or data member not found when label clearly exist

I'm trying to do it use a label to pop up, via a click, a user form, enter data and unload it into the label but VBA does not seem to recognize the label. I can make the form appear, but when I want to feed to data to the label the code crashed with the error message "Compile error method or data member not found"
Private Sub ok_Click()
Sheet1.faktnr = Me.faktnr
Unload Me
End Sub
That's all the code, the label and textbox is named faktnr. This is probably a silly question but I feel stupified by this.
Try this:
Private Sub ok_Click()
Sheet1.Range("faktnr") = Me.faktnr
Unload Me
End Sub

Excel VBA - Use an existing string in called sub

I'm pretty new to this so apologies in advance
I'm half way through a userform in Excel and I'm trying to cut some fat off my code by using Call - I have 12 buttons that all do the same thing, the only difference is that each buttons sub is dependant on the buttons caption. My problem is that I can't figure out a way to use a String I've already declared in the Buttons Sub, then use it in the called Sub. I know you can do it, but my googling skills have failed me :(
Please could someone show me how to do this? Hope that all makes sense...
Here is a very small snippet of my code, but you get the jist:
Public Sub CommandButton4_Click()
Dim Name As String
Name = CommandButton4.Caption
Call Sort1
End Sub`
And the other one (Also tried this as function for the sake of trial and error)
Public Sub Sort1(Name As String)
Label11.Caption = Name
Sheets(Name).Select
End Sub
What you're referring to is passing an argument to another subroutine or function. Let's say you want to use a function a lot of times to get the first letter of a string. A sample of this is:
Function LeftOne(StrSample As String) As String
LeftOne = Left(StrSample, 1)
End Function
The above function can be used inside another function or subroutine provided you meet its requirement: StrSample. By declaring StrSample As String in the arguments field of the function, you are basically requiring that any calls to this should require a string to be passed to it. Anything else would throw an error.
The full line LeftOne(StrSample As String) As String can be read as: "I am function LeftOne. Pass me a string and I'll return to you a string after doing something with it." Note that the name StrSample is an arbitrary name.
Anyway, calling the above is as simple as:
Sub MsgInABox()
Dim StrToFeed As String
StrToFeed = "BK201"
MsgBox LeftOne(StrToFeed) 'Returns B.
End Sub
In your example, if you want to pass Name to Sort1, your attempt is absolutely correct.
Let us know if this helps.
You hat to give your sort1 procedure the parameter name.
call sort1(name)
or
call sort1(CommandButton4.Caption)

Why is this VBA code wrong and how can I fix / avoid?

I currently have this code
Private Sub Worksheet_Change(ByVal Target As Range)
WorksheetChanged(Target, Range("AB3").CurrentRegion, Range("B18:B19"))
WorksheetChanged(Target, Range("AE3").CurrentRegion, Range("B20:B21"))
End Sub
But it throws me a "Compile Error: Expected =", I have no idea why and I don't know where an = would go, any help? Thank you in advance.
If you want to use brackets, you need to assign to a variable or in some cases, use Call, if you do not need to assign, skip the brackets:
WorksheetChanged Target, Range("AB3").CurrentRegion, Range("B18:B19")

Resources