Declaring control as specific type(s) - excel

I have a userform with multiple control types (TextBox, ComboBox, Button, Check box, Label).
I want to check the value in each TextBox and ComboBox and change the color of the box to red if there is no value.
The problem using "Controls" collection is that some controls do not have a value property i.e. Label.  So I want to write code where there will be no need to define the type of control inside the body of the loop as the type is already specified at the declaration level.

Loop through the controls and if textbox or combobox and value ="" then change color to red
Private Sub CommandButton1_Click()
Dim crt As Control
For Each crt In Me.Controls
If TypeName(crt) = "TextBox" Or _
TypeName(crt) = "ComboBox" Then
If crt.Value = "" Then
crt.BackColor = vbRed
Else
crt.BackColor = vbWhite
End If
End If
Next
End Sub

Related

Change caption of toggle button

I have two toggle buttons, each on a different sheet.
The code of the toggle button is stored on the sheet level:
Private Sub MoveButton_Click()
ClickToggleButton "move", ActiveSheet.Name
'ToggleButtonPressed sSHEET2NAME
End Sub
The ClickToggleButton function is stored on the module level (because I want to use the same function for both buttons).
In my case while clicking one toggle button, the other button on the other sheet must be pressed, this works out well:
'switch button state to the same over the 2 sheets
Select Case sheetName
Case sSHEET1NAME
Worksheets(sSHEET2NAME).OLEObjects(sBUTTONMOVENAME).Object.Value = Worksheets(sSHEET1NAME).OLEObjects(sBUTTONMOVENAME).Object.Value
Case sSHEET2NAME
Worksheets(sSHEET1NAME).OLEObjects(sBUTTONMOVENAME).Object.Value = Worksheets(sSHEET2NAME).OLEObjects(sBUTTONMOVENAME).Object.Value
End Select
Now I only need to change the text (caption) on both buttons depending on the value you are in, I got error 438 "Object doesn't support this property or method" on this line:
Worksheets(ActiveSheet.Name).OLEObjects(sBUTTONMOVENAME).Caption = sButtonDescription
I guess it has to do with the fact where my code was stored ? I can change the value of the object but can't change the caption.
Thanks for your help.
You set the caption of the button like you set the value - you need to use the Object-property of the OLEObject.
The OLEObject is just a wrapper around different kind of objects, while the Object-property is the button (or checkbox, or textbox or...) itself:
ActiveSheet.OLEObjects(sBUTTONMOVENAME).Object.Caption = sButtonDescription
You can also set other properties like Font, Color and the like:
With ActiveSheet.OLEObjects(sBUTTONMOVENAME).Objectws.OLEObjects(1).Object
.Font.Size = 14
.FontBold = True
.FontName = "Broadway"
.ForeColor = vbRed
.BackColor = vbYellow
End With

Adding a tooltip to a ComboBox in Excel with VBA

I've added two Dropdown (aka ComboBox) to a Sheet
Using this piece of code I can access the Dropdown but how can I add a tooltip on the Dropdown?
The best solution would be to show a different text for every item but if there is only an unique tooltip for the whole dropdown I can change it after selecting every item.
Sub DropDown1_Change()
Dim s As Object
Set s = ActiveSheet.Shapes(Application.Caller)
s.ToolTip = "Example"
Debug.Print s.ControlFormat.Value
End Sub
This is a forms combobox, it would not have a tooltip capability, but you can make it look like it has a tool tip.
Place a hyperlink with a screen tip underneath the combobox, when you mouse over the combobox the screen tip will pop up.
You can place the hyperlink on many cells if you intend on stretching the combobox over many cells.
Like this
Here is a 20 second clip
http://www.screencast.com/t/ZbkEOyXntItk
You can get the range of the combobox with application.caller.
Assign each combobox to this macro, then you would only need one macro.
Sub DoIt()
Dim r As Range
r = ActiveSheet.Shapes(Application.Caller).TopLeftCell
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:=r, ScreenTip:="5435435345", TextToDisplay:="ddddddddddddddddddd"
End Sub
Following is my code:
Private Sub ComboBox1_Click()
' Adding new items
ComboBox1.AddItem ("S")
ComboBox1.AddItem ("M")
If ComboBox1.Text = "S" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Strong" ' Add your text here
End With
End If
If ComboBox1.Text = "M" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Moderate" ' Add your text here
End With
End If
End Sub

Populating Combobox on a Second UserForm

I've created a Userform that requires the user to fill in a number of text boxes and combo boxes. A portion of the form has a command box that opens a second Userform (Userform 1). I've managed to set it up, but the combo boxes for the second userform won't populate. I'm using the exact same code and reference the same LookupLists (a Excel Worksheet). I've also tried to give the combo box a specific value that I've entered manually (cboSENTPROJ1.Value = 9), but that doesn't work either.
This is the code I have for Initializing UserForm1:
Private Sub UserForm1_Initialize()
Dim cPROJ As Range
Set ws = Worksheets("LookupLists")
For Each cPROJ In ws.Range("Projects")
With Me.cboSENTPROJ1
.AddItem cPROJ.Value
.List(.ListCount - 1, 1) = cPROJ.Offset(0, 1).Value
End With
Next cPROJ
End Sub
Declare you text.value variables as Public Variables (at the top of the module) and then you can call the values in your combobox assignments.

General function to color applicable comboxes

I'm running some VBA code in Excel and I made a a combo box that will turn red and set the focus on it when there is no value entered.
If cmb = "" Then
cmb.BackColor = vbRed
lbl.ForeColor = vbRed
cmb.SetFocus
Exit Sub
Else
cmb.BackColor = vbWhite
lbl.ForeColor = vbBlack
End If
As I have many combo boxes on my form, I want to build a function or procedure that I can call for any combo box that I want.
Can anyone help?
Here's a general function that you can pass a ComboBox to:
Function ValidateComboBox(c As ComboBox) As Boolean
If Len(c.Text) = 0 Then
c.BackColor = vbRed
Controls(c.Tag).ForeColor = vbRed ' Set associated label color, also
c.SetFocus
ValidateComboBox = False
Else
c.BackColor = vbWhite
Controls(c.Tag).ForeColor = vbBlack ' Set associated label color, also
ValidateComboBox = True
End If
End Function
You call it like this, for example when the form is submitted:
' [OK] clicked. Submit form...
Private Sub cmdOK_Click()
If Not ValidateComboBox(ComboBox1) Then Exit Sub
If Not ValidateComboBox(ComboBox2) Then Exit Sub
If Not ValidateComboBox(ComboBox3) Then Exit Sub
...
End Sub
The tricky part is that you have a label that corresponds to each combo box that needs to be updated as well. You can either design the function to accept both a ComboBox control and a Label control and pass both each time, or you take advantage of the combo box's Tag property, as I've done above. For each combo box, just enter the name of the corresponding Label control into its Tag property using the Properties window in the designer.
For example, if ComboBox1 has a label named Label1, then enter "Label1" (without the quotes) into ComboBox1's Tag property. The routine above will look for the label/control with that name and set its color appropriately as well.

Set cell link at run time for Forms Check Box

I am working on pulling data out of a standardized form in excel. There is a Forms Control CheckBox that I need the state of. Apparently the only way to get this is from the cell link, where the value is placed into a cell. The problem is, whomever put this form together did not set a cell link. Is there any way to do this using VBA at run time. There are many of these forms that I must go through, so I'm trying to avoid doing it manually.
I think you are referring to a forms checkbox control placed on a worksheet, in which case you can get the state of the control without setting a cell link. Like this:
Sub HTH()
Dim iLoop As Integer
'// Get value of check box by its index
MsgBox (GetCheckBoxState(1))
'// Get value of check box by its name
MsgBox (GetCheckBoxState("Check Box 1"))
'// Loop through all checkboxes and get values
For iLoop = 1 To ActiveSheet.CheckBoxes.Count
MsgBox (GetCheckBoxState(iLoop))
Next
End Sub
Function GetCheckBoxState(vCheckBox As Variant) As String
Select Case ActiveSheet.CheckBoxes(vCheckBox).Value
Case xlOn
GetCheckBoxState = "Checked"
Case xlOff
GetCheckBoxState = "UnChecked"
Case xlMixed
GetCheckBoxState = "Mixed"
End Select
End Function
If you are referring to a check box control on a userform then as Tim pointed out it should be a case of something like:
MsgBox (UserForm1.CheckBox1.Value)

Resources