Else Box keeps activating but the If solutions are satisfied - vba

Private Sub Dutybox_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L17").Value = Me.Dutybox.Value
Calculate
If Me.ComboBox18.RowSource <> "" And Me.RankCombo.Value <> "" And Me.Exambox.Value <> "" And Me.Dutybox.Value <> "" Then
Me.ComboBox18.RowSource = "=Rate"
Else
MsgBox "Please Select all the above values"
End If
End Sub
So the IF parts are all filled, and it should fill ComboBox18 with =Rate (a named range) but it keeps popping up the Msgbox
Item 1
Private Sub ComboBox18_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L18").Value = Me.ComboBox18.Value
End Sub
Item 2
Private Sub RankCombo_Change()
ThisWorkbook.Worksheets("User Dashboard").Range("L15").Value = Me.RankCombo.Value
End Sub
Item 3
Private Sub Exambox_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L16").Value = Me.Exambox.Value
End Sub
Code to push range to ComboBox18
Private Sub ComboBox18_Intialize()
MsgBox "combo box"
Me.ComboBox18.List = Application.Transpose(Names("Rate").RefersToRange.Value)
End Sub
I can't figure out why Msgbox is populating when the values are satisified.
Thank you in advance

One way to debug this program is to verify the output in Immediate window,
Please break your code at
If Me.ComboBox18.RowSource <> "" And Me.RankCombo.Value <> "" And Me.Exambox.Value <> "" And Me.Dutybox.Value <> "" Then
On immediate window type the following and press enter to verify the conditions
?Me.ComboBox18.RowSource <> ""
?Me.RankCombo.Value <> ""
?Me.Exambox.Value <> ""
?Me.Dutybox.Value <> ""
all the above four condition should return true.

I suspect that your expressions might not properly handle Null values.
Try using Nz() like this: Nz(Me.RankCombo.Value,"") <> ""
You can also add code to check your assessments:
Debug.Print "Combo18", Me.ComboBox18.RowSource <> ""
Debug.Print "Rank", Me.RankCombo.Value <> ""

Related

VBA Change value in textbox based on whether other textboxes are empty

Struggling with some code here, looking for help. I am trying to figure out the correct syntax for updating a textbox value based on whether another textbox has a value or not.
Breakdown
Textbox1 name = arisk_box
Textbox2 name = adjust_box3
Textbox3 name = rr_box
This is what I have so far:
Private Sub arisk_box_Change()
If arisk_box.Text <> "" And adjust_box3.Text = "" Then
rr_box.Value = arisk_box.Value
ElseIf arisk_box.Text <> "" And adjust_box3.Text <> "" Then
rr_box.Value = adjust_box3.Value
End If
End Sub
*If arisk_box has a *VALUE* and adjust_box3 *DOES NOT* then rr_box = the value from arisk_box
Elseif arisk_box AND adjust_box3 both have a *VALUE* then rr_box = the value from adjust_box3
The simplest way it to use 2 separate macros, one for each textbox change event. But it may not work as smoothly as you want it to. I sure someone will have a more advance method.
Private Sub adjust_box3_Change()
If arisk_box <> "" And adjust_box3 = "" Then rr_box = arisk_box
End Sub
Private Sub arisk_box_Change()
If arisk_box <> "" And adjust_box3 <> "" Then rr_box = adjust_box3
End Sub

VBA Variable as CommandButton#

I'm rewriting some code and had a thought, but can't seem to get my syntax right to execute it properly. I want to use a for loop to populate an array of commandbuttons as well as control their visibility. I just need help with my syntax to define which CommandButton number I'm working on in the loop. For instance, CommandButton1, CommandButton2, etc.
Public Sub LoadLots(sName As String, streamLots() As String)
Label1.Caption = sName
For o = 1 To 9
If streamLots(o) <> "" Then
CommandButton& o &.Caption = streamLots(o)
CommandButton& o & .Visable = True
Else
CommandButton& o & .Visable = False
End If
Next
End Sub
Use the Userform.Controls collection to reference the commandbuttons by name.
Public Sub LoadLots(sName As String, streamLots() As String)
Dim btn As MSForms.CommandButton
Label1.Caption = sName
For o = 1 To 9
Set btn = Me.Controls("CommandButton" & o)
If streamLots(o) <> "" Then
btn.Caption = streamLots(o)
btn.Visible = True
Else
btn.Visible = False
End If
Next
End Sub

Include loop counter in object name [VBA]

Basically I wrote a code, which is to be used in userform. The thing is that userform is created by other macro (amount of checkboxes differs, depends how many words string strNamn contains, that is why userform must be created by macro).
I would like to, somehow, include loop counter in the line:
If UserForm1.CheckBox0.Value = True Then
to make it like this:
If UserForm1.CheckBox(i).Value = True Then
But it obviously doesn't work like this :(
Any suggestion how to declare checkbox to include the counter in the line?
Code in UserForm1 to execute macro looks like:
Private Sub cmd_1_Click()
Call clicker
End Sub
Macro code:
Sub clicker()
Dim strNamnOK As String
Dim strNamn As String
Dim strNamnA() As String
strNamn = "one, two, three, four"
strNamnA = Split(strNamn, ", ")
Dim intAmount As Integer
intAmount = UBound(strNamnA)
strNamnOK = ""
For i = 0 To intAmount
If UserForm1.CheckBox0.Value = True Then
strNamnOK = strNamnOK & " " & strNamnA(i)
End If
Next
strNamnOK = Left(strNamnOK, 12)
MsgBox strNamnOK
End Sub

UserForm ComboBox

I have a UserForm that has one ComboBox and a TextBox. The TextBox needs to do a vlookup for the value of the ComboBox but ONLY if that value exists in the list, if not, I want nothing to appear in the TextBox so the user can enter new information.
This is how far I have got:
Private Sub TextBox1_Enter()
If cbocolor.Value <> "" Then
Dim evalStr As String
Dim check As Variant
evalStr = WorksheetFunction.VLookup(cbocolor.Value, worksheets("CONTACTS").Range("allcontacts"), 2, False)
check = Evaluate(evalStr)
If VarType(check) = vbError Then
TextBox1.Value = "Enter new info"
Else
var1 = WorksheetFunction.VLookup(cbocolor.Value, Worksheets("CONTACTS").Range("allcontacts"), 2, False)
TextBox1.Value = var1
End If
You should be able to do it all with one line:
Private Sub TextBox1_Enter()
If cbocolor.value <> "" Then
TextBox1.value = WorksheetFunction.IfError(Application.VLookup(cbocolor.value, _
Worksheets("CONTACTS").Range("allcontacts"), 2, False), "Enter New Info")
End If
End Sub

Identifying VBA routine

Hi to give some context the code below is from an Access database that was left to me from the previous employee, Unfortunately I am not very good at VBA.
I would appreciate any help in identifying its purpose.
Private Sub Command83_Click()
On Error GoTo Err_Command83_Click
Dim stDialStr As String
Dim PrevCtl As Control
Const ERR_OBJNOTEXIST = 2467
Const ERR_OBJNOTSET = 91
Const ERR_CANTMOVE = 2483
Set PrevCtl = Screen.PreviousControl
If TypeOf PrevCtl Is TextBox Then
stDialStr = IIf(VarType(PrevCtl) > V_NULL, PrevCtl, "")
ElseIf TypeOf PrevCtl Is ListBox Then
stDialStr = IIf(VarType(PrevCtl) > V_NULL, PrevCtl, "")
ElseIf TypeOf PrevCtl Is ComboBox Then
stDialStr = IIf(VarType(PrevCtl) > V_NULL, PrevCtl, "")
Else
stDialStr = ""
End If
Application.Run "utility.wlib_AutoDial", stDialStr
Exit_Command83_Click:
Exit Sub
Err_Command83_Click:
If (Err = ERR_OBJNOTEXIST) Or (Err = ERR_OBJNOTSET) Or (Err = ERR_CANTMOVE) Then
Resume Next
End If
MsgBox Err.Description
Resume Exit_Command83_Click
End Sub
Const ERR_... are Error Codes
The script checks whether PrevCtl is a Text-, List or ComboBox and sets the string of stDialStr depending on the Box. In the end it starts an external AutoDial program with the given parameter.
Application.Run "utility.wlib_AutoDial", stDialStr