Can a text box in a UserForm contain formula? - excel

I have had a look online for an answer to this but no joy with coming across one as of yet. Is it possible to have a text box in a UserForm be dynamic?
My current idea is to have 3 columns of text boxes, in Column 1 & 2 the user need to enter there data and was wondering if in Column 3 there is a way to show the value involving Column 1 and Column 2?
If this isn't possible, are you able to reference a cell on a sheet and then get the useform to display the cells value?
Ideally i would like this to be possible as it provides the user with a quick check to ensure they've entered the data right.
Edit 1
TextBox18.Value = (TextBox1.Value + TextBox10.Value) / 2
I found that this line of code allowed me to reference two text boxes dynamically but textbox 18 concatenating rather than added the numbers. Is there a way round this?
Thanks

Alternatively something like this could help, this will add the values and not concatenate them:
TextBox3.Value = (Val(TextBox1.Value) + Val(TextBox2.Value))

Use the TextBox_Change() event method to dynamically update values.
Example:
Sub TextBox46_Change()
TextBox46.Value=(TextBox1.Value + TextBox36.Value) / 2
End Sub

Related

How do I take all values in a column and change their values

There are a lot of questions on how to multiply all values by some other cell or to move all values to another cell based on some value, but what I want is to take, in the example image below:
All the values that I have selected and divide by 2. I do not want another column, I just want to change all those values in the spread sheet and divide them by 2, the values themselves should change.
I have not found an answer for this any where and I sure it is super simple. For example, in:
base_damage_mod selected column, 0.03 would become: 0.015.
The only way I know to do this is manually, and that's a lot of work ...
Whats the easiest way to do this?
The easiest way to do this is by writing a macro, like in the following example:
Sub Divide_by_2()
For Each c In Selection:
If c <> "" Then
c.Value = c.Value / 2
End If
Next c
End Sub
In order to launch this, you need to select your cells (no need to copy, or press Ctrl+C), and then launch the macro.
As far as the source code is concerned, this is pretty obvious, except for the c <> "" part: I have added this in order to avoid the value 0 being filled in in empty cells.
Is there a way to do this without VBA, without macros?
Yes, there is, but it involves you creating a new column, in there type a formula, then copy the values of that formula into again another column and remove the first two columns, in other words: it's quite Messi :-)
If column C is empty (if not, temporarily insert a column), enter a 2 there next to every used column D item (*).
Copy all of column C, and "Paste Special" onto column D using Operation>Divide.
(*) If there are too many items to manually do the "2", copy this formula down column C =IF(ISBLANK(D1),"",2) and it will add them. After this, convert column C from formulas to values by copying it and using "Paste Values" to paste it back. (Special Operations won't work on formulas)

Excel VBA Dynamically refer to UserForm TextBox and get value

lets say in a UserForm I have two textboxes, TextBox1 and TextBox2.
How can I dynamically refer to and get the values of these TextBoxes?
Depending on the value of a TextBox, certain other actions should be triggered.
However, I can only make it work, if I explicitly name the TextBoxes and cannot figure out, how to do it dynamically. Can anybody help me?
This works:
If TextBox1.Value > 0 Then
Do something
End If
If TextBox2.Value > 0 Then
Do something
End If
Since there are actually more than 2 TextBoxes, I imagined doing something like this but it does not work:
Dim i as Integer
For i = 1 to 2
If ("TextBox" & i).Value > 0 Then
Do something
End if
Next i
So I guess my question is, how can I combine a string (one part of the name of the TextBox, with a variable (here i) to get another variable (?) which represents the TextBoxes and from which the value can be read? Or is there another/better solution to my problem?
Kind regards

Hide rows based on variable in Textfield of Userform

i'm wondering if someone could help me with a little issue.
I have a textbox in an userform where people can add numbers for example 3. I need an macro when a checkbox is on then a few rows needs to be hidden. For example total range is B3 to B50 and if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
Sub CommandButton1_Click()
If UserForm12.CheckBox2.Value = True Then
Rows("[B4+Karel]:B55").Hidden = True
End Sub
Could some one help me to fix this?
if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
First hide all rows in the range and then show only the relevant rows. I am assuming that the name of the textbox is TextBox1. Change as applicable.
Is this what you are trying? (Untested). Also I have not done any error handling. I am sure you can take care of that?
Rows("3:50").Hidden = True
Rows("3:" & 3 + Val(TextBox1.Text)).Hidden = False
Note: When you are hiding/showing rows, you do not need the column names. You can directly work with row numbers as shown above.

Using vlookup in vba but updating the userform live

I have an userform with a few texbox and i want that when you put the value on the first one scan the data sheet and if it found the same value it fill the rest of textboxes, i think i can manage to do it if i do it in two steps. First take the value of textbox1 as variable and with it scan the data an generate the second userform with the data already paste on it. But is there a way to do it live? at the moment i put data in textbox1 shows the data of the rest of the columns on the others textboxes?
Also i was trying to do more or less the same thing in a sheet with an vlookup formula (VLOOKUP(A27,BDD!A:B,2,FALSE)) and it worked but the problem is that i want the formula to change the value of the cells only in case it found a mach in the data and also i don't want the formula in the cells i want to change so i can put new data without problem.
Lets say i have in the sheet "bdd" numbers in the first columns and names in the second. I want in another sheet to put a number and if that number already exist in the bdd i want to have the name near to it but i dont want the formula near to the numbers because i want to be able to put new numbers and names if necessary.
bdd:
101 Antonio
102 Luis
sheet
101 Antonio (At the moment i finish writing 101)
103 Peter (nothing happens because it isn't in the bdd yet and i have to type peter to complete that line or put 103 peter in the bdd)
Sorry if it wasn't clear i tried :P Thanks in advance
If data is in this range
Private Sub TextBox1_Change()
'Skip if value is not there
On Error Resume Next
'take any random cell to store textbox1's value
Range("C4").Value = TextBox1.Text
'Vlookup to get the value
TextBox2.Text = Application.WorksheetFunction.VLookup(Range("C4").Value,Sheet1.Range("A:B"), 2, 0)
End Sub

Excel filter Multiple Value in One Cell

I am looking for help on filtering multiple values in one cell delimited by a comma.
For example I have a spreadsheet with the following data:
Column A (Risk) Column B (Risk Mitigation)
Risk A Requirement1, Requirement2
Risk B Requirement2, Requirement6, Requirement7
Risk C Requirement1, Requirement3, Requirement9
When I filter on the 'Requirement Mitigation' Column I would like to see check boxes for the following:
Requirement1
Requirement2
Requirement3
Requirement4
Requirement5
...
Requirement9
So for my example when I only check the Requirement 1 filter box, only 'Risk A' and 'Risk C' rows would be displayed
Currently when I filter it does by the unique cell value, which gives me
Requirement1, Requirement2
Requirement2, Requirement6, Requirement7
Requirement1, Requirement3, Requirement9
Please note that above is just an example and I have thousand of individual requirements, so it would not be as simple as having one column per requirement.
I open to any suggestions including creating vbscripts.
Any help would be greatly appreciated.
This will be tricky.
First you'll need to create a form that will contain a list box and populate that list box with the Requirements you have in their correct syntax.
Then you'll need to create a loop that will go through every cell in a column and save each of them to a string, you could make a one dimensional string array to save them all in that as well.
Then you'll need a loop that will go through each string and use the split function to seperate the strings with a delimeter, you'll need to set your delimiter to ", " to ensure it seperates the strings at the commas.
Then you'll need a loop that will run after you've made your selection from the listbox in your form. this loop will go through every string in your 2D array and check if it contains any of the requirements in your listbox. If it is not, run the code: Range("B" & x).EntireRow.Hidden = True where x is the row, you can use one of the variables in your loop for this.
If you need more specific information that this I may need to see what you've already done, it is possible as well to use this method to automatically populate the list box in your form.

Resources