I'm new to excel but now i'm stuck at something.
The one thing i try to achieve is that if i add a specific word into a textbox, another cell gets +1 (so if 0 and text has been entered in the textbox, it changes to 1 etc.)
so for example:
Cell B2 = Apple
Cell H2 : value of B2
I'd like to get, if possible, one or two textboxes where i could put the type of product and another box for the amount.
Thanks in advance.
OK. Here's a solution.
Set up a named range. I entered 5 different fruit in one column and 5 different quantities in the adjacent column (doesn't matter where but must be adjacent). I named the range "Products" but any other name will do just as well.
I set up a data validation list. I used cell G3 but any other will be equally suitable. I pointed the data validation list to =INDEX(Products,0,1), meaning the first column of the Products range.
Now I added code to the worksheet. This code must be in the code sheet of the worksheet on which G3 is located. That code sheet will have a name like Sheet1 (Sheet1). Don't use a standard code module with a name like Module1. Here is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
' 018
Const Trigger = "G3" ' change to suit
Dim Qty As Long
With Target
If .Address = Range(Trigger).Address Then
On Error Resume Next ' in case not found
Qty = Application.VLookup(.Value, Range("Products"), 2, False)
.Offset(0, 1).Value = Qty + 1
End If
End With
End Sub
Note that the Trigger cell is named as "G3". This must be the cell where you have the data validation drop-down.
This code will run whenever Trigger is changed. You make a selection there and the VLOOKUP function will find the quantity in column 2 of the Products range. If the item isn't found in the list it will return 0 but you can set the cell validation to prevent the entry of an item that isn't in the list. The code will add one to the quantity found and issue the result in .Offset(0, 1), meaning one column to the right of the Trigger cell.
You might want to do other things with your idea. I think the system I suggest can be adapted to whatever you have in mind, including changing the quantity in the Products list.
Related
Good day dear community,
I currently have a problem with VBA/Excel that I can't find a solution to. What I want to achieve is not complicated, but I can't find a way.
Let's assume we have two columns. In any row of one column A the User enter a value and then I start a macro. This macro executes certain instructions. Among other things, this macro ensures that if a cell in column A has a value, then the value "Yes" is entered in the same row in column B. Now my problem: As soon as the user deletes the cell value in column A, the value "Yes" in column B should also be deleted. At first glance you might think that i can use this confdition:
=IF(A1="";"";yes)
The problem is that as soon as the user has entered a value in the cell, "yes" is immediately written in the cell, but this is not desired. Because this task should be taken over by the macro.
As a small side note: I have simplified my problem. Due to the structure of my project, only the macro is allowed to write "yes".
Thanks.
Evaluate Excel Formula in VBA
In your code, you will define the occupied range in column A, and apply the second line appropriately.
Option Explicit
Sub checkColumnRange()
' Some code
' Define the column range...
' e.g.:
Dim rg As Range: Set rg = Range("A1:A10")
rg.Offset(, 1).Value = Evaluate("IF(" & rg.Address(0, 0) _
& "<>"""",""Yes"","""")")
' Some code
End Sub
I have a very long formula in columns I2:HM13 that contains values that need to change depending on what department uses the worksheet and these values may change over the course of the year. This formula repeats in all cells within the stated range with only the reference column/row changing. I would like to create a UserForm were the end user can input the values specific to their department in textboxes and click a commandbutton and the values in the textboxes get inserted into the formula.
I have tried some find and replace codes, but nothing I have tried has work at all. Below is the formula I am trying to modify.
=IF($H2="No","",IF(($B2-I$1)<100,"",IF(((($B2-I$1)*$C2)*1000000)<260000000000,"",IF(((I$1*$C2)*1000000)<330000000000,"",IF(AND(($B2-I$1)>=200,($B2-I$1)<=800,((($B2-I$1)*$C2)*1000000)>=620000000000,((($B2-I$1)*$C2)*1000000)<=920000000000,I$1>=375,I$1<=420,((I$1*$C2)*1000000)>=680000000000,((I$1*$C2)*1000000)<=790000000000,$C2>=1300,$C2<=2100),1)))))
The above is a small portion of the formula of interest. The values I am trying to changed based on the textbox inputs are all the values after the less than, less than or equal to, greater than, or greater than or equal to symbols.
For example in textbox 1 the user inputs 150, 150 would then replace all "100" in the formula. If the user inputs 270000000000 in textbox 2, all the "260000000000" would be replaced with 270000000000.
Your best approach would be to create sheet-scoped names which you then use in your formulas: your userform can then change the "refersto" values for those names to the user-supplied values.
Eg:
With ActiveSheet
.Names("FOO").RefersTo = txtName1.Value
.Names("BAR").RefersTo = txtName2.Value
End With
On your sheet:
=FOO/BAR
Found this solution to my question:
Private Sub CommandButton1_Click()
Dim cell As Range
For Each cell In Range("I2:HM2")
cell.Formula = Replace(cell.Formula, "100,", TextBox4.Value + ",")
cell.Formula = Replace(cell.Formula, "260000000000,", TextBox5.Value + ",")
Next cell
End Sub
I would need to repeat this format for all the values I would want to change per department.
I need help with clearing/changing a dependent drop down list in excel 2013, without using a macro, if it's possible. I've tried different workarounds, but nothing is working:
This is one of the things I tried:
=INDIRECT(VLOOKUP($A$8, lookuptable, 2, FALSE))
This doesn't work, the dependent (child) drop down cell is not being cleared/changed when the parent (A8) drop down is changed, causing a miss-match.
Thanks!
The technique for dependent data validation is to use range names for each dependent data list. The range name is the same as the value selected in the parent list. Example: the parent data validation cell has a list of countries.
You also build several lists, one for each country, with the cities of that country. The range names for each list are the country names.
Apply the country DV to A1.
Then build another range name to use in the dependent data validation for the city cell, e.g. ListRange. The formula to build the range name is
=Indirect($A$1)
The dependent data validation then points to the range that has the same name as the value in cell A1. This is a dynamic range and will change when the selection in cell A1 changes.
This will not clear any existing choice in the dependent data validation cell. E.g, if your first DV cell has a list of countries and the dependent DV has a list of cities, selecting a country in A1 will change the DV list for the city cell and a city can be selected (France > Paris). But if the country cell is changed to Italy, the city cell will still show Paris, while the DV drop down now lists cities in Italy.
In order to clear the selected city when the country cell is changes, you will indeed need VBA.
This is the best I have come up with, pick the country in cell A2 and the city will change BUT only according to the position specified in cell A4. I hope it may help you, cheers.
I have a sample piece of code I use for a similar instance, one dropdown list with 3 options using an indirect formula to provide different choices per option.
I use named ranges and indirect formula via data validation.
When I change my list value in my dropdown cell "D2" in this example, it will then clear the second dropdown list in "F2" (2 columns to the right or offset by 2). This then enables the user to select from new choices dependant on the option change...
Hope this helps.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$2" Then
If Target.Validation.Type = "$F$2" Then
Application.EnableEvents = False
Target.Offset(0, 2).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
I am trying to set up a collection of spreadsheets for others to use. I am putting labels on the first row for each column, I think of them as headers in that case. And I know how to name a column in Excel (at least 2010 lets you do this).
If I have columns with the headers "higher" and "lower", and the columns have those names also, then the formula "=higher+lower" in a given row would use the values from those columns in that row to calculate the result.
I would like to end up with the descriptive column name being the same as the header value. I'd like a way to either create the headers from the column names, or create the column names from the headers, so I don't have to enter them twice. I have a lot of columns, and multiple spreadsheets to do this with; I'm just trying to save typing them all twice, and both initially and to keep them updated as they change.
Manually: Select the desired columns and go Formulas Ribbon > Create from Selection, tick "Top row" and hit OK. Repeat when you've changed a value in row 1.
With VBA: Use this code in the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("1:1"), Target) Is Nothing Then
For Each cel In Target
Debug.Print cel.Column
Columns(cel.Column).Select
Selection.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
cel.Select
Next cel
End If
End Sub
If you copy and paste the same value into multiple cells, Excel will create the first name and then prompt for the other cells if you want to replace the existing name.
Changing a cell in row 1 will create a new range name. The old range name will still remain in place.
If you have values that can be interpreted as a cell address, like A1, Excel will add a _ sign to the range name, like A1_. If you enter numbers into row 1, Excel won't create range names.
Let's say I have three sheets in Excel. Sheet 1 is the Master sheet, sheets 2 and 3 contain different sets of information with the same headers that feed into the table in the master sheet. Is there a way to make it such that I could edit information in sheet 1 and sheet 2 will change AND vice versa so I can edit info in sheet 2 that will update the master sheet?
You could solve it by having Vlookup-formulas in your Master sheet. That way, if you change anything in sheet 2 and 3 the Master will automatically be updated.
If the user changes anything in the Master sheet, you will have to build logic in VBA on that. One way to go is to format the Master sheet so that there is something that helps the VBA know what the formula should be in the edited cell, and also to know from where the data should come. Loosely one could set up the Master sheet like this:
Row 1 is hidden and contains the template formulas
Row 2 is hidden and is completely empty (this will make less problems with filtering)
Row 3 contains headers
Row 4 and down contains the data, using the formulas define in row 1
Add the Change event on the Master sheet, that sees if the changed cell was one with a formula. If so, it will examine the template formula to identify from where the data should come. Then it will update that cell in Sheet 2 or 3 with the new value that is entered in the Master sheet. After this, it will overwrite the value manually entered in the Master sheet with the formula from the template row.
The big job here is to write a parser that understands from which cell the vlookup will get it's value.
One thing that I overlooked is that the CHANGE event is triggered only ONCE if the user pastes several cells in one go. The TARGET will then contain several rows or columns.
So this is some kind of skeleton using the above idea...
Option Explicit
Dim ChangeEventDisabled As Boolean 'Flag for disabling the Change event
Public Sub Disable_ChangeEvent()
ChangeEventDisabled = True
End Sub
Public Sub Enable_ChangeEvent()
ChangeEventDisabled = False
End Sub
Sub Worksheet_Change(ByVal Target As Range)
Dim updatedValue As Variant
Dim SourceCell As Range
'While the MasterSHeet is populated intially, we don't want this event to do anything
If ChangeEventDisabled Then
'There are chenges being done in teh sheet that should not trigger updates of the source-sheets.
Else
'Only run the code if it was a data-cell that was changed
If Target.Row > 3 Then
'We are in the rows containg data
'Did the changed cell contain a Vlookup formula before the user changed the cells value?
If UCase(Cells(1, Target.Column).Formula) Like "=VLOOKUP(*" Then
'A vlookup normally populates this cell.
'To know from where the data normally comes, I will need to put back the formula in the changed cell.
'So, first save the new value that we will write in the source cell
updatedValue = Target.Value
'Insert the formula again in the cell
'As we will now CHANGE a cell in the Masterr sheet, a Change event will trigger. Disable it temporarily
Disable_ChangeEvent
Cells(1, Target.Column).Copy Destination:=Target
Enable_ChangeEvent
'Find out from which cell the data is being fetched by the Vlookup
Set SourceCell = MyMagicParsing(Target)
'Update the source-cell with the new value
SourceCell.Value = updatedValue
End If
End If
End If
End Sub
Function GetSourceCell(Target As Range) As Range
'This function should decipher the formula in the cell Target, and figure out from where
'the data is actually coming. It shoudl return the range which is the source of the data.
'As I dont know how to do that quickly, I just hardcode the cell that is the source.
GetSourceCell = Worksheets("Sheet2").Cells(67, 3)
End Function