issue with vba count - excel

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

Related

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

How to count differences between Cells

I have VBA which comparing 2 cells. Each cell can contain between 1 and 3 different parameters ant parameters are trimmed by the "," comparison is made by simple double for loop(check code). Thing what i can't figure it out is that: How to modify code and get number of unique entries, example
cell 1 [music, art, science]; cell 2 [art, music]; When i run my for loops i get 2 matches(which is fine) but how to count number of unique words in this case should be 3.
I have tried to enter this part of code but its not working well num_possible = num_possible + 1
game_tags_parts = Split(Cells(11, 2), ",")
game_tags_parts_j = Split(Cells(11, j), ",")
num_matches = 0
num_possible = 0
For m = LBound(game_tags_parts) To UBound(game_tags_parts)
num_possible = num_possible + 1
For n = LBound(game_tags_parts_j) To UBound(game_tags_parts_j)
If Trim(game_tags_parts(m)) = Trim(game_tags_parts_j(n)) Then
num_matches = num_matches + 1
End If
Next n
Next m
Actual result should be number of unique words used in those cells, in some cases i get 3 matches, example cell 1 [scifi, space, star] cell 2 [star, space, scifi] and its in total 3 matches. Modification should provide me an number 3 as number of unique words used in both cells. Or in this case where i have cell 1 [art, music, science] and cell 2 [scifi, space, star] where program gives me 0 same words and modification should give me a number 6 as unique used words.
One easy way to get a unique count is to use a Dictionary object:
game_tags_parts = Split(Cells(11, 2), ",")
game_tags_parts_j = Split(Cells(11, j), ",")
Dim myDict As Object
Set myDict = CreateObject("Scripting.Dictionary")
For Each v In game_tags_parts
If Not myDict.Exists(v) Then myDict.Add v, v
Next v
For Each v In game_tags_parts_j
If Not myDict.Exists(v) Then myDict.Add v, v
Next v
MsgBox "unique count: " & myDict.Count

Generating all possible combinations for values in n columns - where n can vary (VBA)

I want to generate all possible combinations for unique values in n columns. Please consider the scenario below:
In one sheet i am taking an input from the user where he/she can input the number of columns for which they want to generate the combinations and then can enter the unique values in each of the columns. Let's say that the user entered 10 as input, he will automatically get 10 input columns as below:
Now, in each of the columns, user will input the variable name and the unique values of that variable. Once this is done, they will click on a button, which will generate all possible combinations for these n variables in a separate tab.
Need your help on how to generate these combinations using vba, considering n can vary upto 50.
Thanks!
Here is part 1 of 2 (a simple loop based on input with formatting). In the next step, the user would enter the data and maybe someone else can provide you part 2 of the macro!:
Sub VarCount()
Dim inp As Variant
inp = InputBox("How many variables?")
If IsNumeric(inp) And inp > 0 And inp <= 50 Then
Range("A1").Value = "Var Count"
Range("A1").Font.Bold = True
Range("A1").Font.Italic = True
Range("A2").Value = "Var Name"
Range("A2").Font.Bold = True
Range("A2").Font.Italic = True
For i = 2 To inp + 1:
Cells(1, i).Value = i - 1
Cells(1, i).HorizontalAlignment = xlCenter
Next
Else: MsgBox ("Please enter an integer between 1 and 50 and re-run the macro.")
End Sub

BASIC nested loops: filtering rows

I'm using LibreOffice Calc to store 16 RGB colors (red in B column, green in C and blue in D), then I want to paint the background of 16 cells with each respective color. Each color is in one row.
At the moment I've been able to achieve this partially: not only 16 cells are painted, but 48 (16x3)! It seems I need to "group" the nested loops. This is my code/macro:
function bgcolor()
Dim Doc, Sheet, CellPaint As Object
Dim CellR, CellG, CellB As String
Doc = ThisComponent
Sheet = Doc.Sheets.getByIndex(0)
For i = 1 to 16 step 1
For j = 1 to 3 step 1
CellR = Sheet.getCellByPosition(j+0,i).getValue()
CellG = Sheet.getCellByPosition(j+1,i).getValue()
CellB = Sheet.getCellByPosition(j+2,i).getValue()
CellPaint = Sheet.getCellByPosition(j+6,i)
CellPaint.CellBackColor = RGB(CellR,CellG,CellB)
next j
next i
bgcolor=CellPaint.CellBackColor
end function
This is the result:
The first column of colors (H) is what I need, exactly. Other two columns appear there, so: how do I fix my nested loops? Probably I need to filter them using a simple if statement, but I'm not sure.
EDIT - For reference: the solution should be similar to this one, however I've read it's not possible on OpenOffice (I assume neither on LibreOffice).
Thanks!
Just drop the inner loop -- You don't need to iterate horizontally:
function bgcolor()
Dim Doc, Sheet, CellPaint As Object
Dim CellR, CellG, CellB As String
Doc = ThisComponent
Sheet = Doc.Sheets.getByIndex(0)
For i = 1 to 16 step 1
CellR = Sheet.getCellByPosition(1,i).getValue()
CellG = Sheet.getCellByPosition(2,i).getValue()
CellB = Sheet.getCellByPosition(3,i).getValue()
CellPaint = Sheet.getCellByPosition(7,i)
CellPaint.CellBackColor = RGB(CellR,CellG,CellB)
next i
bgcolor=CellPaint.CellBackColor
end function

Combination Generator yet keep in order

Wondering if anyone could help me. I'm stumped. It's been ages since I used excel....
I have 9 columns with different values in each cell, different numbers of cells per column.
I need a formula/macro to spit out all combinations of the cells and yet still remain in the exact same order of the columns.
For example
Columns:
D / 003 / 23 / 3 / 3R / C / VFX
... / 005 / 48 / 3 / 12 / .. / VDF
... / 007 / ... / 1 / ... /... / HSF
And it spits out like this:
D0032333RCVFX
D0032333RCVDF
D0032333RCHSF
D0034833RCVFX
D0034833RCVDF
and so on....
and so on.....
Presumably you will want to call this function with a "serial number" - so that you can call "the Nth combination". The problem then breaks into two parts:
Part 1: figure out, for a given "serial number", which element of each column you need. If you had the same number of elements E in each column it would be simple: it's like writing N in base E. When the number of elements in each column is different, it's a little bit trickier - something like this:
Option Base 1
Option Explicit
Function combinationNo(r As Range, serialNumber As Integer)
' find the number of entries in each column in range r
' and pick the Nth combination - where serialNumber = 0
' gives the top row
' assumes not all columns are same length
' but are filled starting with the first row
Dim ePerRow()
Dim columnIndex As Integer
Dim totalElements As Integer
Dim i, col
Dim tempString As String
ReDim ePerRow(r.Columns.Count)
totalElements = 1
i = 0
For Each col In r.Columns
i = i + 1
ePerRow(i) = Application.WorksheetFunction.CountA(col)
totalElements = totalElements * ePerRow(i)
Next
If serialNumber >= totalElements Then
combinationNo = "Serial number too large"
Exit Function
End If
tempString = ""
For i = 1 To UBound(ePerRow)
totalElements = totalElements / ePerRow(i)
columnIndex = Int(serialNumber / totalElements)
tempString = tempString & r.Cells(columnIndex + 1, i).Value
serialNumber = serialNumber - columnIndex * totalElements
Next i
combinationNo = tempString
End Function
You call this function with the range where your columns are, and a serial number (starting at 0 for "top row only"). It assumes that any blank space is at the bottom of each column. Otherwise, it will return a string that is the concatenation of combinations of values in each column, just as you described.
EDIT perhaps the following picture, which shows how this is used and what it actually does, helps. Note that the first reference (to the table of columns of different length) is an absolute reference (using the $ sign, so when you copy it from one cell to another, it keeps referring to the same range) while the second parameter is relative (so it points to 0, 1, 2, 3 etc in turn).

Resources