Problem:
I have a counter cell: A1 with a value of =COUNTIF(B:B;"FOO")
This gives me a current "count" of all instances of "FOO" in column B.
I have a value cell C1 with a formula of: =IF(B1="FOO";"FOO_" & A1;)
This gives me a result of FOO_1 if "FOO" only exists once in Column B
Question:
I want to be able to reference the value of A1 at the time I write out the contents to the cell of C1. When A1 updates, I do not want C1 to be modified. C2 should now take the update of A1 (which would now be 2) and C2 would be: FOO_2 for example:
A1 = 1
B1 = FOO
C1 = FOO_1
A1 = 2
B2 = FOO
C2 = FOO_2
C1 still remains FOO_1 based on the value of A1 before a new row was added to column B
Looking for an automated way to create an ID, like an increment value in MySQL, and not looking for a solution that involves a person copy / pasting..
The problem with your approach is that, A1 will always change to reflect the COUNT, you can't use a value that WAS. What else is going into the remaining cells in Column A that you could not use the Count in the adjacent row as #JMax suggests?
If you have to increment with fixed Counts, I would recommend you think of a simple macro on a change event of whichever cell you are wanting. For instance everytime you enter "FOO" or "BAR" or anything, it would do a COUNTIF of the Range in B:B and concatenate the result entering it in the adjacent C.
No reason to depend upon A1 at all, and it possibly changing.
This example may get you started. And the Countif may use a ';' for your version of Excel
It assumes you are doing data entry and then moving to the next cell down. It also checks whether you have already made an entry in the adjacent cell in C so it does not change the count if you revisit a cell.
Which means it will be wrong in that adjacent cell unless you delete the value in C first before making a change. Of Course If you have 4 FOOs and then delete one you will still have FOO_4 and it won't change so if you change the 4th FOO to BAR first delete FOO_4. If the Increment HAS to match actual counts for some other reason, I would not rely upon this for that.
Sub doIncrement()
If ActiveCell.Column = 2 And ActiveCell.Offset(-1, 1) = "" Then
ActiveCell.Offset(-1, 1) = ActiveCell.Offset(-1, 0) & "_" & WorksheetFunction.CountIf(Range("B:B"), (ActiveCell.Offset(-1, 0)))
Else: Exit Sub 'or do something else
End If
End Sub
Then call this in
Private Sub Worksheet_Change(ByVal Target As Range)
Call doIncrement
End Sub
If you want to stick to a formula solution, you can replace the A1 formula by:
=COUNTIF($B$1:B1;"FOO")
That would do the trick when you drag and drop your formula.
Related
Good day,
I'm at a loss on this problem.
I have a group of cells that contain words, like apple, this word would be the value. It is separated by a symbol for completing the math. They can be changed by the user to make custom calculations.
Cell A1 is "apple", B1 is "+", cell C1 is "apple", cell D1 is "*", cell E1 is "apple", call F1 is "=" and cell G1 is the suggested total, in this case would be "6".
It would be posted as | apple | + | apple | * | apple | = | 6 |
The legend holds the value for the word, so if you enter 2 in the legend, apple would be 2.
The logic would determine that the formula would be 2+2*2= if written in excel, I would like to combine all the cells and calculate this.
I tried using =sum, sumproduct, concate and the like to no avail.
Any head way I did make, I ended up getting BEDMAS wrong as it calculated it as 2+2=4*2=8, instead of the correct 2*2=4+2=6.
Anyone know of a way to combine these cells and properly sum the values to the correct total?
Thank you for your time!
Go to the Name manager and create named range Eval, into Refers to field add formula:
=EVALUATE(CONCATENATE(VLOOKUP(Sheet1!A1,Sheet1!$A$3:$B$5,2,0),Sheet1!B1,VLOOKUP(Sheet1!C1,Sheet1!$A$3:$B$5,2,0),Sheet1!D1,VLOOKUP(Sheet1!E1,Sheet1!$A$3:$B$5,2,0)))
In range A3:B5 I have legend.
Change references as you need. Then in cell G1 write formula =Eval.
Sample:
This is a UDF based solution. Its advantage is that it's more versatile and by far easier to maintain if you learn its elements of code. The disadvantage is in that you do have to learn the elements of code and you have an xlsm macro-enabled workbook which isn't welcome everywhere.
The setup is simple. I created a table with columns Item and Value. I placed it on another sheet than the task. In fact, you could make the sheet with the table VeryHidden so that the user can't look at it without access to the VBA project, which you can password protect. I called the table Legend. The item columns has names like apple, pear, orange. The Value column has the numeric values associated with each name.
Note that, since this is a VBA project, the entire list can be transferred to VBA leaving no trace on the sheet. You could have a function to display the value of each item as the user clicks on it and have it disappear as he clicks elsewhere.
Next I created a data validation drop-down in A1 with the List defined as =INDIRECT("Legend[Item]"). Copy this cell to C1 and E1.
Then I created another Data Validation drop-down in B1 with the list as +,-,*,/. This drop-down must be copied to D1.
Now the code below goes into a standard code module. Find the way to create it because it isn't any of those Excel sets up automatically. It's default name would be Module1. Paste the code there.
Function Evalue(Definition As Range) As Double
Dim Task As String
Dim Fact(2) As Double
Dim C As Long
Dim i As Long
With Definition
For C = 1 To 5 Step 2
On Error Resume Next
Fact(i) = Application.VLookup(.Cells(C).Value, Range("Legend"), 2, False)
i = i + 1
Next C
Task = "(" & Fact(0) & .Cells(2).Value _
& Fact(1) & ")" & .Cells(4).Value _
& Fact(2)
End With
Evalue = Evaluate(Task)
End Function
Now you are ready for testing. Call the function from the worksheet with a call like
=Evalue(A1:E1). You can use it in comparisons like =IF(G6 = Evalue(A1:E1), "Baravo!", "Try again"). As you change any of the components the cell with the function will change.
Remember to use absolute addressing if you copy formulas containing the range. If you need to get a result in VBA while testing, use this sub to call the function.
Private Sub TestEvalue()
Debug.Print Evalue(Range("A1:E1"))
End Sub
My Sheet
Here is what I have.
In cells M - U, i count all the instances of the word from cells E, G and I from the legend.
=SUMPRODUCT((LEN(E3)-LEN(SUBSTITUTE(E3,$B$3,"")))/LEN($B$3))
In cells W - AE, I multiply the instances with the value to give me a total each time the word appears.
=SUM(M3*$C$3)
In cell E8 - I8, i add the three possible values together.
=SUM(W3:Y3) so each worded cell is now a number.
I'd like to take the cells E8 - I8 to make a calculation in k8 and so on.
So, each cell is put together to make an
=SUM(E8:I8)
statement, which all works except E11 - I11 which equates to 26 instead of 43.
I'm creating an Excel file using vb.NET but I have the following problem/situation:
I want to add a formula to my cells on specific row and on specific starting position, for example: from F5 to Z5. The main problem is that I need to do two formulas the first one ONLY on the first cell (F5 for example) and from F5 to Z5 another formula. The other problem is for example, I have data from A1 to Z5 then I insert a blank row and then I have data from A7 to Z7.
At the moment I have a code which allows me to move from cell F5 to Z5 one cell per one cell but only insert data (formula) on F5 and G5 because I don't know how to handle the range without letters. This is an example of my code:
'' LastLetter is equal to the index o Z Column (I always know which is the last column letter.)
For workingOnCell As Integer = 7 To lastCellLetter Step 1
If itsFirtsCell = 1 Then
Dim select As Excel.Range = CType(SW.Cells(5, workingOnCell), Excel.Range)
'' Formula only for first cell
select.Value = "=F2+F3+F4"
itsFirtsCell = 0
Else
Dim select As Excel.Range = CType(SW.Cells(5, workingOnCell), Excel.Range)
'' Formula for all the rest of cells
'' Problem starts here because formula for second cell it's ok but for the third cell should be =G5-H2-H3-H4 and here is where I don't know how to handle cell letters
select.Value = "=F5-G2-G3-G4"
End If
Next
Other problem with above code is that if I have data on A7:Z7 range I should do the same thing but my code only going to work with F5:Z5.
How can I make my code works as expected or how can I solved it?
I'm just wondering if anyone can solve a simple query in Excel. Basically I want a randomly generated string from an array.
The formula I have is this:
=INDEX({"Day","Night","Mixed"},RANDBETWEEN(1,3))
This works, however, whenever I switch between worksheets the values change and are not fixed once randomly selected.
Anyone have any ideas?
Go to options -> formulas -> enable iterative calculation
Then use a formula like this in B1:
=IF(A1="","",IF(B1="",INDEX({"Day","Night","Mixed"},RANDBETWEEN(1,3)),B1)
If you empty out A1 then B1 will also be empty. If you put anything in A1 then B1 will choose randomly and stay with it till you empty out A1 again (where B1 will also be empty again)
Alternatively just copy you formula and paste "values only"... but the formula will be gone that way...
You can use a self referencing UDF, something like this would work:
Function RandInArray(MyString As String, ByRef Target As Range)
Dim MyArr As Variant
If Target.Text = 0 Then
MyArr = Split(MyString, ",")
RandInArray = MyArr(Application.WorksheetFunction.RandBetween(LBound(MyArr), UBound(MyArr)))
Else
RandInArray = Target.Value
End If
End Function
In B1 I have the formula: =RandInArray("Day,Night,Mixed",B1) Note its self reference to B1
Basically the formula says if you already have a value then don't refresh it but if you don't have a value then pick one randomly from the list passed in.
If you hit F2 (edit cell) and press enter you will force it to recalculate and get a new (or the same as is the rule of the randbetween) value, if you press F9 (recalculate) it will not change.
I need to work with Excel to which I am absolutely new. I am looking for a VBA solution to speed up some daily work. Here is my case: I need to check cells of Column C which has negative, positive and netural values and then insert sysmbols in corresponding cells of Column B.
For instance, if C4 has a positive value (2,345), B4 should have &u& as symbol. If C5 has negative value (-12.98), then B5 should have &d& as a symbol. And if C6 has a netural value (0.000), then B6 should be inserted with &n& as a symbol.
An empty B column already exists and values are all in Column C.
As Kyle says, all you need for this is a simple (well, nested) if-formula:
=IF(C1<0;"&d&";IF(C1>0;"&u&";"&n&"))
The format is pretty straightforward, the if has three parts separated by semicolons. The first is the logical test, what you test for, in this case if the value in C1 is smaller (or larger) than 0. The next is the value you want in the cell if the the statement in the test is true, and finally what to put in if it is false.
In this case, we test again if C1 is not smaller than 0, to see if it is larger than zero, that is why there is another if-statement inside the first one.
To apply the formula to your entire column, just copy it down the entire way, and the cell it refers to should update automatically.
Give this a try:
Sub WhatEver()
Dim C As Range
Set C = Intersect(ActiveSheet.UsedRange, Range("C:C"))
For Each cc In C
ccv = cc.Value
If ccv <> "" Then
If ccv = 0 Then
cc.Offset(0, -1).Value = "&n&"
ElseIf ccv > 0 Then
cc.Offset(0, -1).Value = "&u&"
Else
cc.Offset(0, -1).Value = "&d&"
End If
End If
Next cc
End Sub
Simply - if any cell in Column B contains thisvalue then append to the adjoining cell in Column A with sometext.
How is this done?
A simple if statement. For example:
=IF(ISNUMBER(SEARCH(thisvalue, B1)), sometext, "")
EDIT: The ISNUMBER(SEARCH(thisvalue, B1)) searches for thisvalue in B1, and if it finds it, it returns a number (that number being the starting index of thisvalue within B1).
EDIT #2: To append the inserted value to the end of the current value in cell A, use the CONCATENATE formula.
Example:
=CONCATENATE(A1, sometext)
Put this formula in A1, then drag down as necessary:
=IF(B1="thisvalue","sometext","")
EDIT
Using a the Visual Basic Editor, you can update the contents of cell A like this:
Private Sub UpdateColumnA()
Dim x As Long
For x = 1 To 65536
If InStr(1, Sheet1.Range("$B$" & x), "thisvalue") > 0 Then
Sheet1.Range("$A$" & x) = Sheet1.Range("$A$" & x) & "sometext"
End If
Next
End Sub
Repeated runnings of the macro, however, will append the text again; you'll need more validation code if you don't want this to happen.
copy-paste in A1 , considering that you have values in B
=IF(ISNA(VLOOKUP("thisvalue",B:B,1,FALSE)),"",VLOOKUP("thisvalue",B:B,1,FALSE)&"ADDITIONAL VALUE")
it is saying:
if value of vlookup is is empty (if lookup returns nothing) , then show empty value ( double quotes)
but if the value of lookup returns something, then do this lookup and append "ADDITIONAL VALUE" text to found result
I think I have what you are looking for, let me know if you are still interested and if you want me to elaborate further. This formula in cell F2: =IF(ISNUMBER(SEARCH($U$2,E:E)),$V$2,"")&IF(ISNUMBER(SEARCH($U$3,E:E)),$V$3,"")&...
where you are searching for a value that you specify in U2 across all cells in column E:E, if it finds a match it appends the value you specify in V2. To search for multiple words assigning corresponding value simply concatenate as shown as much as you like. I am able to specify hundreds of words (and corresponding values). I hope it helps.