Microsoft Form - If Checkbox Cell Value Greater Than - vba

First post on this forum - I have a fillable form which I would like to do the following.
There are two check boxes, (Legacy form item), labelled H and N, depending on which one is selected the value of another input box (Legacy form item) should be equal to or greater than a value. E.G if N is selected, the input cell should be greater than 0.5 if H is selected the cell should be equal to or greater than 0.5.
Can anyone impart any wisdom on this one, I would then set it to run a check once the user has input the value, based on the checkbox option and value input into the cell.
Thanks in advance,
Daniel

You have 3 objects and you need to react on any change. So I'd put something like this into the code section of the form:
Private Sub cbN_Click() ' name of checkbox N is cbN
proc_chg
End Sub
Private Sub cbH_Click()
proc_chg
End Sub
Private Sub tbx_Change() ' name of textbox is tbx
proc_chg
End Sub
Private Sub proc_chg
Dim rVal as Single
rVal = Val(Me.tbx.value)
If cbN And rVal > 0.5 Then
tbx.BackColor = vbGreen ' OK
ElseIf cbH And rVal >= 0.5 Then
tbx.BackColor = vbGreen ' OK
Else
tbx.BackColor = vbRed ' wrong
End If
End Sub
This will capture every change on the form and set the background color of the inputbox accordingly. (Please complete the logic in proc_chg, this is just an example.)

Related

Select a specific row from ComboBox and add amount in other Column from same row in DataGridView VB.NET

I'm using VB.NET for a small project. I have a DataGridView with some rows and columns.
I want to send amounts inside a specific row & column. For this purpose I'm using a ComboBox to select the receiver, but I cant manage to select the specific row & column for the amount.
Here is a photo of the form:
[
I managed to add the receivers inside the ComboBox and to add an amount, but in all rows.
This is code (it auto generates the rows for now).
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rowId As Integer = Me.DataGridView1.Rows.Add()
Dim row As DataGridViewRow = Me.DataGridView1.Rows(rowId)
row.Cells("Column1").Value = "UNITED BANK"
row.Cells("Column2").Value = "1000"
row.Cells("Column3").Value = "ExampleInfo"
Dim rowId2 As Integer = Me.DataGridView1.Rows.Add()
Dim row2 As DataGridViewRow = Me.DataGridView1.Rows(rowId2)
row2.Cells("Column1").Value = "FREE BANK"
row2.Cells("Column2").Value = "2000"
row2.Cells("Column3").Value = "ExampleInfo"
Dim bolAdd As Boolean = False
Dim strValue As String
For Each myRow As DataGridViewRow In Me.DataGridView1.Rows
bolAdd = True
strValue = myRow.Cells("Column1").Value
For Each myItem As String In Me.ComboBox1.Items
If myItem = strValue Then bolAdd = False
Next
If bolAdd AndAlso Not (strValue Is Nothing) Then Me.ComboBox1.Items.Add(strValue)
Next
If Me.ComboBox1.SelectedIndex = -1 Then
Me.ComboBox1.SelectedIndex = 0
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
rowX.Cells(1).Value = Val(rowX.Cells(1).Value) + Val(TextBox1.Text)
Next
End Sub
End Class
With this code, I'm able to add an amount, but to all rows.
I want to add an amount only to the row I selected in the ComboBox.
Your current code in the Button1_Click event is adding the amount from the text box to ALL the rows in the grid regardless of “what” value is selected in the combo box.
From what you describe, you want the amount from the text box to be added to rows that “match” the value in the combo box. The current code is never checking this, so the value from the text box is added to all the rows in the grid.
Therefore, the code needs to check to see if the value in Column1 "Receiver" “matches” the value in the combo box. If it DOES match, then add the value from the text box to the current value in Column2 "USD."
It is unclear if there may be more rows in the grid that have the same Column1 "Receiver" value and if you want to update those values also. I am assuming that there will only be ONE row in the grid that “matches” the value in the combo box. Therefore, once we find this value and add the amount, then we are done and the code will return without looping through the rest of the rows in the grid.
So is what you need to do is alter the code in the button click event to do this checking as described above.
There are a lot of checks we need to make. We need to check that the combo box has a value to compare to. Also, before we check ANY cell value, we need to make sure the cells value is not null BEFORE we try and call the cell's Value ToString method.
In addition, the cell MAY have a value, however, it may not be a number, so we have to check and make sure the value in the cell is an actual number and the same would apply to the text box.
So, walking through the code below would go something like…
If (Int32.TryParse(TextBox1.Text, addedValue)) … checks to see if
the text box amount is a valid number.
If (Not String.IsNullOrEmpty(targetComboValue)) … checks to make
sure the combo box value is not empty.
Then we start the loop through all the rows in the grid.
If (Not rowX.IsNewRow) … checks to see if the row is the “new” row
which we will ignore this “new” row.
If (rowX.Cells("Column1").Value IsNot Nothing) … checks to make
sure the “Receiver” cell is not null
If (rowX.Cells("Column1").Value.ToString() = targetComboValue) …
checks to see if the “Receiver” cells value matches the value in the
combo box.
If (rowX.Cells("Column2").Value IsNot Nothing) … check to make sure
the “USD” cell is not null.
And Finally,…
If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) … check to see if the value in the “USD” cell is a valid number.
If all these checks succeed then add the value from the text box to the current value in the “USD” cell.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim targetComboValue = ComboBox1.SelectedItem
Dim addedValue As Int32
Dim currentValue As Int32
If (Int32.TryParse(TextBox1.Text, addedValue)) Then
If (Not String.IsNullOrEmpty(targetComboValue)) Then
For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
If (Not rowX.IsNewRow) Then
If (rowX.Cells("Column1").Value IsNot Nothing) Then
If (rowX.Cells("Column1").Value.ToString() = targetComboValue) Then
If (rowX.Cells("Column2").Value IsNot Nothing) Then
If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) Then
rowX.Cells("Column2").Value = currentValue + addedValue
Return
End If
End If
End If
End If
End If
Next
End If
End If
End Sub
I hope this makes sense.
You can edit the desired row just by using a syntax as follow:
Me.DataGridView1.Rows(1).Cells(2).Value = "your string"
So if you want to select corresponding to your combo index :
Dim myId As Integer = Me.ComboBox1.SelectedIndex
Me.DataGridView1.Rows(myId).Cells(2).Value = "your string"
But this assuming that the count of combo and DataGridView are same.
If not you should think about to add an id Column in your DataGridView ...
If you mean something else please explain, I will edit.

Comparison of two listbox in VBA

I am new at VBA.So kindly help me on this matter.
My UserForm has two ListBox controls in it, each with two columns. For example, ListBox1
Name Item
A 20
B 30
and listbox2:
Name Item
A 20
B 40
When I click a CommandButton, the procedure below attempts to compare both ListBox controls and returns whether or not the data in each column of data is correct. I believe the best approach would be to first compare Column 1 of ListBox1 with Column 1 of ListBox2. If those are identical, then compare the second columns of both ListBox controls. The procedure is supposed to return a MsgBox that says "Correct" if all columns are identical. Otherwise, the program should return a mismatch error. Here is the code I've tried so far.
Private Sub CommandButton1_Click()
Dim p As Integer, Tabl()
Redim Tabl(0)
For i = 0 To ListBox1.ListCount - 1
p = p + 1
Redim Preserve Tabl(p)
Tabl(p) = ListBox1.List(i)
Next i
For i = 0 To ListBox2.ListCount - 1
If IsNumeric(Application.Match(ListBox2.List(i), Tabl, 0)) Then
Msgbox"Correct"
End If
Next i
End Sub
Unfortunately, the program only calculates the first column repeatedly. How can I compare multiple columns?
Based on your description and a small evaluation of what your code does, you may be overthinking this. How about the following?
Private Sub CommandButton1_Click()
Dim myMsg As String
Dim byeMsg As String
myMsg = "Same name chosen."
byeMsg = "Those names don't match."
If ListBox1.Value = ListBox2.Value Then
MsgBox myMsg
Else
MsgBox byeMsg
End If
End Sub
Of course, instead of displaying a message using MsgBox, you could just as easily replace it with any code you need.

Check if a Range in Datagridview contains an "," if yes change it to ":"

My application contains a Datagridview, the user has the possibility to enter values like: Day, Time, how many hours did he work etc. The problem is that my second application calculates with this data. It has to be in a certain format.
Like time should be "09:15", but i noticed that some users are using "09,15" instead. Can you help me guys, I need a code that can check if a Range in Datagridview contains some " blacklisted char" and if yes, replaces it with the right one.
Thanks for all.
Do not save values as a string.
Validate input string straight in the needed type.
Then validated values pass to the second application.
In this case you don't need "blacklisted chars"
Private Sub DataGridView_CellValidating(sender As Object,
e As DataGridViewCellValidatingEventArgs)
If e.RowIndex < 0 Then Exit sub
Dim dgv As DataGridView = DirectCast(sender, DataGridView)
Dim columnIndexWithInteger As Integer = 2
Dim columnIndexWithDate As Integer = 3
Select Case e.ColumnIndex
Case columnIndexWithInteger
Dim temp As Integer
If Integer.TryParse(e.FormattedValue, temp) = False Then
e.Cancel = True
End If
Case columnIndexWithDate
Dim temp As Date
If Date.TryParse(e.FormattedValue, temp) = False Then
e.Cancel = True
End If
End Select
End Sub
In DataGridView, you have one handle that allows you to check the validity of an edited cell : CellValueValidating. It is called before the user change is taken into account (CellValueChanged event).
You can find example and explanation here :
- https://msdn.microsoft.com/en-us/library/ykdxa0bc(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/7ehy30d4(v=vs.110).aspx
You can also have a look at CellValueChanged, CellValueValidated and CellEndEdit events.

VBA code multiple if condition and put complete datein respective column

I am creating macro and I am stuck in creating the main page of VBA code. we have a sheet named "Renewal" in which all the customer information we dump. Column C we have customer number, Column D We have product type Dental, LIFE & Dis. Column A we have to put complete data, and Column B we have to put annual premium. Now I want the main page where I can one input box1 in which I put customer number, combo box1 in which 3 option I will get "DENTAL, LIFE, DIS., input box2 Date of completion, and input box3 annual premium. If input box1 and combo box1 condition satisfy then date will put on same row of column A and annual premium in column B respectively.
The wording of your question was very interesting, maybe a screenshot might be better. Ok this is what I have crafted. I am sorry I am not sure this code will work 100% as I have not got Word installed on this machine so had to write it in notepad, so may not be the best solution. As suggested in one of the comments - you have to add add() in the button1_click event. Hopefully this has been of some use.
Private Sub add() 'Add the add sub routine to the button1_click
Sheets("Renewal").Activate() 'makes the sheet activate
if textbox1.text <> "" and textbox2.text <> "" and textbox3.text <> "" and combobox1.text <> "" then 'condition
dim pos as integer 'used to find next position
pos = findNext() 'adds next position value
activesheet.cells(1,pos).value = textbox2.text 'adds Date of completion
activesheet.cells(2,pos).value = textbox3.text 'adds annual premium
activesheet.cells(3,pos).value = textbox1.text 'adds customer number
activesheet.cells(4,pos).value = combobox1.text 'adds product type
'unsure if combobox1.text is correct.
end if 'end of the condition
End sub 'end of sub routine
Private function findNext() 'this function finds the next position in sheet
dim x as integer 'used as a counter
x = 1 'counter = 1
do 'start of iteration
x = x + 1 'counting
loop until activesheet.cells(1,x).value = "" 'end when cell (A, counter) has no value
return x 'returns the counter value
End function 'end of function
P.S. If you are a complete begin beginner - This video may help you with the ui: https://www.youtube.com/watch?v=hCIpMwdKCgE

Excel VBA: Searching for value in listbox based on value set in textbox

I am trying to write a code for a search button which searches a listbox based a specific input set in a textbox.
The values searched are always numbers, and the listbox contains values from a single column.
The code i wrote can be found below but i don't understand why it is not functional.
Legend:
SearchButton: A Button which upon clicking is supposed to initiate the search
SearchBox: The textbox which will contain the search value
AvailableNumberList: The listbox which contains the data
Thanks for your help :)
Private Sub SearchButton_Click()
Dim SearchCriteria, i, n As Double
SearchCriteria = Me.SearchBox.Value
n = AvailableNumberList.ListCount
For i = 0 To n - 1
If SearchCriteria = i Then
AvailableNumberList.ListIndex = i
End If
Next i
End Sub
Is this what you are trying?
'If SearchCriteria = i Then
If AvailableNumberList.List(i) = SearchCriteria Then
Also use Exit For once a match is found :)
Additional to #Siddharth Rout solution, this code allows to search in the ListBox even if the TextBox does not have the full word/number:
Private Sub SearchButton_Click()
Dim SearchCriteria, i, n As Double
SearchCriteria = Me.SearchBox.Value
n = AvailableNumberList.ListCount
For i = 0 To n - 1
If Left(AvailableNumberList.List(i),Len(SearchCriteria))=SearchCriteria Then
AvailableNumberList.ListIndex = i
Exit For
End If
Next i
End Sub
Thanks everyone for their code! =D