If TextBox = 1 thru 3 Then? - excel

This is more of a code quality question. I have this line that I'm working with. Len(TextBox.Text) = 1 Or Len(TextBox.Text) = 2 Or Len(TextBox.Text) = 3 Is there a simpler way of writing this? Like a sort of: If Len(TextBox.Text) = 1-3?

select case Len(TextBox.Text)
case 1 to 3
...
end select

Related

issue with vba count

hi everyone i am having problem with the vba. I Want the division box to count the number of txt boxes is been filled for example if txtbox1 > 0 then txt.divide = txt.divide + 1 similarly if other text boxes is been used then it will keep adding 1 like if there 2 text boxes used then division value will be 2 etc
any can help?
If Trading_calculator1.txt_currency1.Value > 0 Then
Trading_calculator1.txt_divide.Value = txt_divide + 1
ElseIf Trading_calculator1.txt_currency2.Value > 0 Then
Trading_calculator1.txt_divide.Value = txt_divide + 1
End If
To test if a TextBox is empty, you can do Len(TextBox.Text) > 0. This returns TRUE when there is any text in the textbox.
You can make this dynamic by using a loop to search every control in the userform (Userform.Controls collection). This way you don't need an IF statement for every textbox individually. You can use the TypeName Function to test if the control is a "TextBox" and then check if it is not empty.
Dim TextBoxCount As Long, ctrl as Variant
For Each ctrl In Trading_calculator1.Controls
If TypeName(ctrl) = "TextBox" Then
If Len(ctrl.Text) > 0 Then TextBoxCount = TextBoxCount + 1
End If
Next
txt_divide = TextBoxCount
I solved it here's what I used:
Dim divide As Integer
divide = 0
If Trading_calculator1.txt_currency1.Value > 0 Then divide = divide + 1
If Trading_calculator1.txt_currency1.Value = "" Then divide = divide - 1
Trading_calculator1.txt_divide = divide
it worked for me
the first if statement will add one if the text box value change
the 2nd if will change it back if there is no value and return it in 0

Reverse ComboBox: How to programmatically select an item in ActiveX ComboBox based on a TextBox value?

I have a multi-column Combobox, and tried to search for a codes for awhile now, but to no avail.
Here's my combobox looks like:
emp_id emp_name
--------- ---------------
<blank> - Select -
1 James Bond
2 Jason Statham
3 Sylvester S.
4 Chuck Norris
where columnwidths = "0,100" and and columnheads = False, so it will only show the emp_name
What I'm trying to do is, programmatically select and display an item from the combobox based on a variable
So far the closest logic to what I'm trying to achieve is this:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.Value = .List(i) '~Trying to select and display 'Sylvester S.' in combobox
Exit For
End If
Next i
End With
But I'm getting Run-time error '380': Could not set the Value Property. Invalid property value.
Thank you in advance!
You need to use ListIndex:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.ListIndex = i
Exit For
End If
Next i
End With

changes of colums randomly of excel file randomly using libra office

I have an excel file consist of 1000x3 words. I wrote 5 rows here as an example.
Target choice1 choice2 choice3
booklet criquet nutmeg luciole
argoman border phobie liom
mouche libellule taplet luciole
abrot guêpe terrier greeca
However I know currently all the words in choice2 are correct answer, I want to randomly mix the place of choice 1 and choice 2 and choice 3 using libra office such as below:
Target choice1 choice2 choice3
booklet nutmeg criquet luciole
argoman phobie liom border
mouche taplet libellule luciole
abrot terrier guêpe greeca
I am new to excel and I don't know how to work with it. Please advice me how to do it and in advance I appreciate all the comments.
Sorry for the late reply.
You can use something like this:
Sub MixTheValues()
Dim x, y, r
Dim Words As New Collection
For x = 2 To Cells(Rows.CountLarge, "B").End(xlUp).Row
For y = 2 To 4
Words.Add (Cells(x, y).Value)
Next y
For y = 2 To 4
r = Int(Words.Count * Rnd + 1)
Cells(x, y).Value = Words(r)
Words.Remove (r)
Next y
Next x
End Sub
x starts on row 2 and goes to the last row.
y defines the columns you want to use (in this case, 2 to 4 or B to D
Note that this shuffles the answers, but because it is completely random, some of them may not move at all.
Results:

deviding an even and an uneven number

I am very happy to discoverd this site. I get very good help. Hope you guys can help me with another problem. I want to round a number. Lets say I have a number 39 if I devide this into 2 then I get 18.5. Which makes very logical. But when you are counting in persons then you can't cut a person in half. So I am looking for a formule in vba. I tried the round function
group=39
devideby = 3 'devideby can be 2 or 3
test = round(group/devideby)
If I do this I get test=20. I want to have 2 or 3 separate answers: if devided by the number 2 then I want to have 20 and 19. If devided by 3 then I want to have 13, 13 and 13. Is there a way to solve this?
You could try something like this:
Dim arrOutput(2)
Select Case divideby
Case 2
arrOutput(0) = Int(group / divideby)
arrOutput(1) = group - arrOutput(0)
arrOutput(2) = 0
Case 3
arrOutput(0) = Int(group / divideby)
arrOutput(1) = Int((group - arrOutput(0)) / 2)
arrOutput(2) = (group - arrOutput(0)) - arrOutput(1)
Case Else
MsgBox "Error"
End Select
It's not very elegant but I think it does what you are asking

Problems with Loop

I am trying to solve an issue..
On a column, I have some stock numbers (activecell). And I have one Sales number.
I have to subtract this Sales Number for the stock numbers until it's a 0. As an example:
Sales Number = 500
Result = 500 - StockNumber1
Result2 = Result - StockNumber2
Result3 = Result - StockNumber3
I am trying to figure out a way to do it in a loop.
Maybe something like:
i = 0
Do While SalesNumber > Activecell.Offset(i,0)
SalesNumber - ActiveCell(i,0)
i = i + 1
Loop
However, I have not found a solution yet.. There are like 3 stock numbers, usually.
How can I do to stop the loop when the stock numbers are over? Does anyone have an idea?
I dont know I am clear enough.. Any doubts, please ask!
Thanks for any help!
This?
Dim i, SalesNumber, nRowMax
nRowMax = Rows.Count
SalesNumber = 500
' my stock number starts at the cell A1:
i = 1
Do While SalesNumber > 0
' use this to avoid infinite loop:
If (i > nRowMax) Then
Exit Do
ElseIf (IsEmpty(Cells(i, 1))) Then
Exit Do
End If
SalesNumber = SalesNumber - Cells(i, 1)
i = i + 1
Loop

Resources