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
Related
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 <> ""
I have a UserForm DataInput in the same VBA project as the module RunOB. These are in the Excel Project Benchmarking.xlsx. I have a command button on the DataInput form in which I would like the module RunOB to run when pressed. Additionally I am passing String arguments from the form to the module.
'Header of 'Run OB' Module
Sub OrganizeBenchmarks(bidNum As String, name As String, _
state As String, year As String, category As String)
The button click method is:
Private Sub CommandButton1_Click()
'...'
End Sub
If possible, it would also be helpful if I could set up a keyboard shortcut to open the DataInput UserForm while I was in the Excel project.
Seems pretty straightforward?
Private Sub CommandButton1_Click()
OrganizeBenchmarks "your", "arguments", "would", "go", "here"
End Sub
Something for you to help developing your own code .........
'Public enum type to add a set of particular vbKeys to the standard key set
Public Enum typePressKeys
vbNoKey = 0
vbExitTrigger = -1
vbAnswerKey = 100
vbLaunchKey = 102
vbPrevious = 104
vbNext = 106
vbSpecialAccessKey = 108
End Enum
Public Sub OrganizeBenchmarks()
Dim stopLoop As Boolean, listOfYears As Variant, bidNum As String, name As String, state As String, year As String, category As String
'For example
listOfYears = Array(2017, 2018, 2019, 2020, 2021, 2022)
stopLoop = False
Load DataInputForm 'Assuming this is the name of your UserForm
With DataInputForm 'Assuming that this name would be an input control in your UserForm
.yearCB.List = listOfYears 'comboboxList control
.yearCB.ListIndex = 2
.Tag = vbNoKey
End With
DataInputForm.Show
Do
If DataInputForm.Tag = vbOK Then
'I have assumed you would use these names for input controls on your UserForm
bidNum = DataInputForm.bidNum_TextBox.Value
name = DataInputForm.name_TextBox.Value
state = DataInputForm.state_TextBox.Value
year = CStr(DataInputForm.yearCB.Value)
category = DataInputForm.category_TextBox.Value
Unload DataInputForm
stopLoop = True
ElseIf DataInputForm.Tag = vbCancel Then
Unload DataInputForm
Exit Sub
Else
stopLoop = False
End If
Loop Until stopLoop = True
'Place additional code here to take whatever actions
End Sub
My customer wants to be able to autosum values within a cell. For example, they type in a new value, and this value will be added to the previous one, all within one cell (they don't want another field).
So far, I have 2 forms that will calculate the new value with one they input into the calculator, but it only functions for one cell.
Option Compare Database
Function MyVal(lnIn As Long)
If Len(Me.entry) > 1 Then
Me.entry = Replace(Me.entry, ".", "") & lnIn
'Me.entry = Left(Me.entry, Len(Me.entry)) & "." & Right(Me.entry)
Else
Me.entry = Me.entry & lnIn
End If
End Function
Private Sub calc_Click()
Me.Balance = Me.Balance - (-(Me.entry))
Me.entry = ""
End Sub
Private Sub clear_Click()
Me.entry = ""
End Sub
Private Sub Command16_Click()
Dim MyBalance As Currency
MyBalance = Me.Balance
DoCmd.Close
Forms!frmHours.Form.SetFocus
Forms!frmHours.Form.Balance = MyBalance
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Balance = OpenArgs
End Sub
Is there a way to move the "selected" cell value into the table I have (replacing the old value), do the calculations, and then move the new value back into the selected cell?
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
I have a macro which run function (clear each named range depend ot application.caller.name) if radio button was clicked
Sub Clear_Click()
Dim s, f, arr
s = ActiveSheet.Shapes(Application.Caller).Name
arr = Array("NamedArray1", "NamedArray2", "NamedArray3", "NamedArray4")
Select Case s
Case "Clear7"
For i = LBound(arr) To UBound(arr)
ThisWorkbook.Worksheets("info").Range(arr(i)).value = ""
Next i
Case Else
f = arr(Right(s, 1) - 1)
ThisWorkbook.Worksheets("info").Range(f).value = ""
End Select
End Sub
It works ok.
Now i need to click Clear7 radio button from other function
So if i do
Sub test()
Application.Run ActiveSheet.Shapes("Clear7").OnAction
End Sub
I got error on s = ActiveSheet.Shapes(Application.Caller).Name as there are no Application.Caller i think.
So how to click radio button from other function?
If you're using Application.Caller but you want to run the code without someone needing to click the button then here's how you can do it.
NOTE: since Clear_Click has an argument, it won't show up in the "assign macro" list when attaching it to a button, but you can type its name directly in the box and that will work fine.
Sub Clear_Click(Optional callerName As String = "")
Dim s, f, arr, cn As String, i
Dim sht As Worksheet
cn = IIf(Len(callerName) > 0, callerName, Application.Caller)
'Debug.Print cn
Set sht = ThisWorkbook.Worksheets("info")
arr = Array("NamedArray1", "NamedArray2", "NamedArray3", "NamedArray4")
Select Case cn
Case "Clear7"
For i = LBound(arr) To UBound(arr)
sht.Range(arr(i)).Value = ""
Next i
Case Else
f = arr(Right(s, 1) - 1)
sht.Range(f).Value = ""
End Select
End Sub
Sub test()
ClickIt "Clear7"
End Sub
'run a macro attached to a shape and pass its name as a parameter
Sub ClickIt(sName As String)
Application.Run ActiveSheet.Shapes(sName).OnAction, sName
End Sub