Complex cross-worksheet Excel formula with string manipulation - excel

I am generating an Excel workbook from C# code, but the code will end there as the workbook must be distributable to people who don’t have access to the VSTO stuff.
The workbook contains:
A) “Worksheet 1” that contains a dropdown list which gives “1” or “2” depending on the selected item.
B) “Worksheet 1” also has several cells in a grid layout which currently contain “1”.
C) On “Worksheet 2” there are string values in cells A1 and A2 (which relate to the drop-down items), e.g.
C01010101
C01010102
D) On “Worksheet 3” in the same locations as the Worksheet 1 cells (see B above) there are related string values which look like this: (note that these values could be any length)
checkbox|001|C010101010101
checkbox|002|C010101010102
checkbox|002|C010101010103
etc
What I need to do is add a formula to the cells at B so that rather than just containing "1" they result in a value according to the following logic:
N1 = selected index (1-2) of drop-down (in A above)
S2 = string value from “Worksheet 2”, cell range “A” & N1
S3 = the 3rd pipe delimited field value from “Worksheet 3”, from the cell which corresponds with the one in “Worksheet 1” (i.e. same address, C5, B6 etc.)
If S2 matches the left end of S3, return “1” otherwise return “0”
Finally, if possible, I would like to colour the cells so that if 0 is returned they have a grey background, but this must be unchanged if someone overtypes the cell manually with a 0.
I don't expect anyone to provide the entire solution - although of course that would be great! - but all suggestions as to how I can achieve this would be gratefully received!
[Edit]
Images attached.
Note that although the output grid is displayed with ticks, these actually contain a "1" if ticked or "0" if unticked.
It's the "1" that the formula will be replacing, because the cell should only be "valid" if the appropriate list-item is selected - if that makes sense :$
[/Edit]
[Edit2]
Ok this is my progress so far:
=IF(LEFT(MID(F17,FIND("|",F17,FIND("|",F17)+1)+1,999),LEN(E17))=E17,1,0)
For this simplified (:o) example F17 is the cell containing the pipe delimited string and E17 contains the drop-down value to match.
Now I need to work out how to reference a range using a row number read from the drop-down...
[/Edit2]

This is the final formula I came up with:
=IF(
LEFT(
MID(
Sheet2!V19,
FIND(
"|",
Sheet2!V19,
FIND(
"|",
Sheet2!V19
)+1
)+1,
999),
LEN(
INDIRECT(
"Sheet3!"&ADDRESS(T16,1)
)
)
)=INDIRECT(
"Sheet3!"&ADDRESS(T16,1)
),
1,
0
)
where Sheet2 is the sheet containing the pipe delimited strings, Sheet3 column A contains the drop-down lookup values, V19 is the current cell, T16 is the cell containing the drop-down.
As required, this causes the current cell to display 1 if the drop-down lookup matches the third word in the pipe delimited string, or 0 otherwise.
I'm going to the pub...

Related

Combining cells unless a cell contains certain text

I have an Excel spreadsheet with 7 cells (in a column) with data in, these are C13 to C19
I have a formula that combines all the data in these cells together and puts it into one cell, this formula is =C13&", "&C14&", "&C15&", "&C16&", "&C17&", "&C18&", "&C19 and works fine. However, can I alter this formula to miss out on any cell that contains the text "Nothing else carby"?
You may, in Excel 2019, use:
=TEXTJOIN(", ",,IF(C13:C19<>"Nothing else carby",C13:C19,""))
If "Nothing else carby" can be a substring inside a cell's value, try:
=TEXTJOIN(", ",,IF(ISNUMBER(FIND("Nothing else carby",C13:C19)),"",C13:C19))
Confirm through CtrlShiftEnter
The formula should not be that long, as you can see:
=TEXTJOIN(",",TRUE,IF(C13:C19="Nothing","", C13:C19))
(I just used "Nothing" instead of the whole word for readability reasons.)
Explanation of the parameters:
"," : delimiter: between every cell content, a comma is added.
TRUE : treatment of blank cells (don't put them).
IF (...) : In case cell content is "Nothing", put an empty string.
In combination with the previous parameter,
just one comma will be put in the result:
"a,b,c,e,f,g" and not "a,b,c,,e,f,g" (see result).
Used data:
a
b
c
Nothing
e
f
g
Result:
a,b,c,e,f,g

combine all cells, numbers and symbols to a sum

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.

If values in Column B = specific text, insert specific text into a value in Column A

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.

Add common prefix to all cells in Excel

I have a column with some text in each cell.
I want to add some text, for example "X", at the start of all cells. For example:
A B
----- >>>> ----
1 X1
2 X2
3 X3
What is the easiest way to do this?
Type this in cell B1, and copy down...
="X"&A1
This would also work:
=CONCATENATE("X",A1)
And here's one of many ways to do this in VBA (Disclaimer: I don't code in VBA very often!):
Sub AddX()
Dim i As Long
With ActiveSheet
For i = 1 To .Range("A65536").End(xlUp).Row Step 1
.Cells(i, 2).Value = "X" & Trim(Str(.Cells(i, 1).Value))
Next i
End With
End Sub
Select the cell you want to be like this,
Go To Cell Properties (or CTRL 1)
under Number tab
in custom
enter
"X"#
Select the cell you want to be like this, go to cell properties (or CTRL 1) under Number tab in custom enter "X"#
Put a space between " and # if needed
Select the cell you want,
Go To Format Cells (or CTRL+1),
Select the "custom" Tab, enter your required format like : "X"#
use a space if needed.
for example, I needed to insert the word "Hours" beside my numbers and used this format : # "hours"
Enter the function of = CONCATENATE("X",A1) in one cell other than A say D
Click the Cell D1, and drag the fill handle across the range that you want to fill.All the cells should have been added the specific prefix text.
You can see the changes made to the repective cells.
Option 1:
select the cell(s), under formatting/number/custom formatting, type in
"BOB" General
now you have a prefix "BOB" next to numbers, dates, booleans, but not next to TEXTs
Option2:
As before, but use the following format
_ "BOB" #_
now you have a prefix BOB, this works even if the cell contained text
Cheers, Sudhi
Michael.. if its just for formatting then you can format the cell to append any value.
Just right click and select Format Cell on the context menu, select custom and then specify type as you wish... for above example it would be X0. Here 'X' is the prefix and 0 is the numeric after.
Hope this helps..
Cheers...
Go to Format Cells - Custom. Type the required format into the list first. To prefix "0" before the text characters in an Excel column, use the Format 0####. Remember, use the character "#" equal to the maximum number of digits in a cell of that column. For e.g., if there are 4 cells in a column with the entries - 123, 333, 5665, 7 - use the formula 0####. Reason - A single # refers to reference of just one digit.
Another way to do this:
Put your prefix in one column say column A in excel
Put the values to which you want to add prefix in another column say column B in excel
In Column C, use this formula;
"C1=A1&B1"
Copy all the values in column C and paste it again in the same selection but as values only.
Type a value in one cell (EX:B4 CELL). For temporary use this formula in other cell (once done delete it). =CONCAT(XY,B4) . click and drag till the value you need. Copy the whole column and right click paste only values (second option).
I tried and it's working as expected.

Dynamically changing dropdown in excel

The scenario is as follows,
I have a list of options to be filled in one cell, say A1. The options can be filled using Data Validation-List which causes a dropdown to appear in A1. Now I want B1 to contain a dropdown, the entries in which change on the basis of the data in A1.
As a contrieved example, suppose A1 offers the choices Mammals, reptiles and amphibians. If I select mammal, I should get man, cat, dog as an option in B1. If I select Reptiles, snake and lizard appear as an option in B1. If I select amphibians, I should get Frogs and toads.
I would like to avoid using VBA for this.
Use the INDIRECT formula in the validation list as is clearly explained here:
www.contextures.com/xlDataval02.html
Here you go, a solution completely without VBA. It's using actual combo box controls from the "Forms" toolbar:
Add three extra worksheets to your workbook. I called them "domain", "data" and "animal"
on sheet "domain", I did:
enter (from cell A1 downwards) "mammals", "reptiles", "amphibians"
defined a name for range "domain!$A:$A": "Domain"
defined a name for range "domain!$B:$1": "DomainChoice"
on sheet "data", I did:
enter (from cell A1 downwards) "man", "cat", "dog"
enter (from cell B1 downwards) "snake", "lizard"
enter (from cell C1 downwards) "frog", "toad"
on sheet "animal", I did:
in A1, entered the following formula
=T(INDIRECT("data!R" & ROW() & "C" & DomainChoice; FALSE))
filled this formula down to, say, row 50.
defined a name for range "animal!$A:$A": "Animal"
defined a name for range "animal!$B:$1": "AnimalChoice"
on the main worksheet, I created two combobox controls:
in box 1, I defined the properties ("Format Control...") as follows:
"Input range:" - "Domain"
"Cell link": - "DomainChoice"
in box 2, I defined the properties as follows:
"Input range:" - "Animal"
"Cell link": - "AnimalChoice"
Now should "mammals", "reptiles", "amphibians" appear in box 1, and the contents of box 2 should change based on the selection.
Look at the various sheets to see what happens behind the scenes. The only requirements are that the order of the values in the "domain" sheet corresponds to the columns on the "data" sheet, and that can only be as many animals as there are rows filled with the formula on the "animal" sheet.
You can hide the three helper worksheets, if you want.
The formula, explained:
T( // returns an empty string unless the argument is a text value
INDIRECT( // returns the value at the given reference string
"data!R" // build a dynamic reference string to "data!R1C1"
&
ROW() // the current row, changes as you fill down the formula
&
"C"
&
DomainChoice // defined name of "domain!$B:$1", contains box 1 choice
;
FALSE // flag to indicate that the formula is in "R1C1" format
)
)

Resources