Excel - Is it possible to declare variables for specific words? - excel

I'm working in a test cases spreadsheet in Excel, last week I ran into a problem, the DEV team decided to change a lot of 'field' labels of the application under test. What I had to do is go to my Excel spreadsheet and modify the labels' name one by one.
What I want to do now is define the names of the labels as variables and then make the changes directly from the variables instead of doing it by occurrence.
For example, I wrote:
"The user inputs the username in 'Name' field".
I want 'Name' to be pulled from a variable so next time they decide to change the label name, e.g: from 'Name' to 'User-Name', I have to only change the variable name from 'Name' to 'User-Name' rather than doing this manually in each occurrence.
Any idea on how can I do this in Excel?
Thanks in advance!

Named Ranges might be beneficial to you. You can refer to a Range of cells by a name and if you ever add columns or rows the Named Range will automatically increment the reference to the Range. Look under the Formulas menu and click on Name Manager. From there you can define a name for any cell range you would like, say:
MyRange = A1:C1
and refer to it in VBA like:
Range("MyRange")
On a WorkSheet you can simply refer to it like this
=MyRange

Sure. Just make a 2-column list in a separate worksheet, where column A is the variable name and column B is the corresponding value. Everywhere you want to reference a specific variable name, perform a VLOOKUP() against that 2-column list.
="The user inputs the username in '"&VLOOKUP("Name",Sheet1!$A$1:$B$30,2,FALSE)&"' field."
Or you wouldn't even need to do the VLOOKUP if you chose to use a one-column list with no field labels. You could refer directly to the cell you want with concatenation, i.e.:
="The user inputs the username in '"&Sheet1!$A$1&"' field."
Even easier, which wouldn't require any special entries would just be to do a global find/replace, which is made more convenient since your fields are enclosed by single quotes. I.e., there's nothing stopping you from replacing all occurrences of 'Name' with 'User-name'.

For example, I wrote: "The user inputs the username in 'Name' field".
If this is in an Excel sheet, you can just concatenate a cell reference into your text. For example, if the field header "User-Name" is in A1 on Sheet1, you would use a formula like this:
="The user inputs the username in '" & Sheet1!$A$1 & "' field."

If you're trying to do this in VBA code, you can always declare constant string variables.
Const strName as String = "Name"
then use it in the code, for example:
MsgBox "The user inputs the username in " & strName & " field"
Hope that helps :)

Related

Microsoft Excel Data Validation

I have a excel sheet and i have created a data validation function. Its a conditional dropdown but I can not input my equation in data validation field. Its says too many arguments. My equation is =IF(A4="Capital",'New List'!$C$2:$C$42,IF(A4="Pier Caps",'New List'!$C$43:$C$84),IF(A4="Keystones",'New List'!$C$85:$C$86),IF(A4="Round Louvre",'New List'!$C$87:$C$88),IF(A4="Open Rings",'New List'!$C$89:$C$90),IF(A4="Pyramids",'New List'!$C$91:$C$92),IF(A4="Plinths",'New List'!$C$93:$C$94),IF(A4="Columns",'New List'!$C$95:$C$96))
What i am missing here? Is it too long? Is there any other way to achieve this?
Thanks in advance
Create individual named ranges for each of your lists. For example the range
Capital refers to 'New List'!$C$2:$C$42
Pier_Caps refers to 'New List'!$C$43:$C$84
etc. Note that there cannot be spaces in named ranges. Use the underscore _ character instead. Now you can use this in the data validation:
=INDIRECT(SUBSTITUTE($A$4," ","_"))
In words: Take cell A4, replace any spaces with underscores and return the range with that name.

Assistance on a Particular Nested IF Excel Formula

Here's a quick summary of what I am trying to do:
I'm trying to set up an Excel workbook that will allow users to paste the results of a SQL query into a RawData worksheet and have multiple other worksheets then grab that data and display it in various formats (graphs, charts, etc.).
This particular formula that I'm trying to write is supposed to look at a certain column in RawData, compare the number listed there to a "key" in the Key worksheet, and then return the text equivalent to the ID displayed in RawData in a new worksheet called StylizedData
For example, if RawData lists 1, then StylizedData will list "Configuration" because 1 is associated with "Configuration" in the Key.
Here is the formula:
=IF(RawData!F60=Key!$C$2,Key!$D$2,
IF(RawData!F60=Key!$C$3,Key!$D$3,
IF(RawData!F60=Key!$C$4,Key!$D$4,
IF(RawData!F60=Key!$C$5,Key!$D$5,
IF(RawData!F60=Key!$C$6,Key!$D$6,
IF(RawData!F60=Key!$C$7,Key!$D$7,
IF(RawData!F60=Key!$C$8,Key!$D$8,
IF(RawData!F60=Key!$C$9,Key!$D$9,
IF(RawData!F60=Key!$C$10,Key!$D$10,
IF(RawData!F60=Key!$C$11,Key!$D$11,
IF(RawData!F60=Key!$C$12,Key!$D$12,
IF(RawData!F60=Key!$C$13,Key!$D$13,
IF(RawData!F60=Key!$C$14,Key!$D$14,
IF(RawData!F60=Key!$C$15,Key!$D$15,
IF(RawData!F60=Key!$C$16,Key!$D$16,
IF(RawData!F60=Key!$C$17,Key!$D$17,
IF(RawData!F60=Key!$C$18,Key!$D$18,
IF(RawData!F60=Key!$C$19,Key!$D$19,
IF(RawData!F60=Key!$C$20,Key!$D$20,
IF(RawData!F60="",""))))))))))))))))))))
That whole process is working correctly all the way up until I get to the point where a row in RawData is empty. When the row is empty, it is displaying "No Subcategory", which is the text equivalent of Key!$C$2 and is contained in Key!$D$2. I'm wanting it to display nothing, which I'm trying to accomplish with that last snippet (IF(RawData!F60="","")).
Can anyone help me out here?
Thanks in advance.
Try,
=iferror(vlookup(RawData!F60, Key!$C$2:$D$20, 2, false), text(,))
=if(iserror(vlookup(RawData!F60,KeyArray,2,0)),"",vlookup(RawData!F60,KeyArray,2,0)
If the lookup value isn't found the cell gets a "" value. Otherwise it will search for the value contained in F60 in your key array and return the value two cells to the right.
With Vlookup() your array/range must have the values you are searching for in the first column. Your column/array must also include the column of values you want to return. For instance you would probably use something like $C$2:$D$20 for your key array. It also helps if your key values in your key array are sorted.
Good luck!

Excel - Replace cell link in formula with value in cell

I'm trying to figure clean up a spreadsheet - but I'm trying to avoid typing out a lot of hardcoded values that are referenced in formulas.
So right now, I have a function that takes a string
=FunctionThatWantsAString(A1,SomeOtherInputs)
In Cell A1 I have "SomeString"
Is there a way to easily replace the cell link "A1" in the formula with the string "SomeString"?
Thanks
The simple solution is to go to the Formulas ribbon > Defined Names > Name Manager > New [or simply select A1, and then click on the NameBox which says "A1" and type in "SomeString"]
This allows you to name a specified range. You can then refer to that name either within an excel worksheet or within VBA.
Edit for additional request
If you don't want A1 to have to hold the text "SomeString", you can actually use the name manager to create a name which refers to a string. ie: you can have the Name SomeString be equal to "asdf". Then, use ctrl+f to find and replace all instances of "A1," in your worksheet with "SomeString".
Alternatively , just use ctrl+f to find and replace "A1" with ""asdf"", and have your results hardcoded directly in all cells. Probably not recommended to do that though, for maintenance purposes.
If you are using a User Defined Function, you should check if that first parameter is a cell value, and if so, get the value.
Public Function test(st As Variant)
If TypeName(st) = "Range" Then
'get the value of the range or use the string passed in
'or if TypeName(st)="String"
End If
test = TypeName(st)
End Function

Excel - Convert String with if condition to a formula

I'd like to use something like the EVALUATE-Function in Excel for if-statements.
I've got the following issue: I'd like to use Excel to validate my data. I've got three sheets:
the real data I'd like to check. Each row represents a customer and each column some data. The columns have specific names like “age”, “name”, …
the description of the checks I’d like to perform. Each row represents one check and I’ve got 3 columns: 1 check_id – an identifier of each check; 2 check_desc – a description of the check that every normal person can understand like “Age below 18”; 3 rule – the Excel Formula as a string like If(age<18, “error”, “no error”)
the place where sheet 1 and 2 should come together. Each row should represent one customer and each column one check.
Now, if I’ve got for example check_1 “If(age<18, “error”, “no error”)” and the customer data 10 and 20, then the check for the first customer should fire and the check for the second shouldn’t.
If the data is changed, and the age is set from 10 to 18, then everything should be fine, or if the rule is changes to “If(age<21, “error”, “no error”)” then the new condition should be applied to all data.
Is something like this possible?
With the evaluate function only ‘simple’ formulas work.
Thanks in advance,
Martin
Attached you can find the
Excel-Sample File
You will definitely need some VBA here. Make a custom EVAL function:
Public Function EVAL(ByRef Rng As Range, Formula As String) As Variant
Dim RngAddress As String
RngAddress = "'" & Rng.Parent.Name & "'!" & Rng.Address(External:=False)
EVAL = Evaluate(Replace(Formula, "$", RngAddress))
End Function
Then you can easily evaluate your values with formulas passed as text ($ is for parameter):
=EVAL(A1, "IF($<21,""error"",""no error"")")
(note the escaped double quotes). But you would rather pass formula from another cell - then you can specify formula in cell with single quotes:
IF($<21,"error","no error")
=EVAL(A1, B1)
I personally would rename check_desc!B2 to "Check_1" (named range) and then refer to that. You can use the INDIRECT function as well once you've renamed your column header "Check_1" as well.
Note: the value in this case should be "18" instead of "age below 18". You can of course change the number format to "age below "0.
If the relations are changing too I would insert a table that would have uniform formulae changing in each cell when changed in one cell.
The only issue you would then face is the non-expanding nature of your table. You can use VBA for this, but maintaining formulae increases your accountability even if it would be slightly easier not to deal with nasty nested functions.

Excel spread sheet if statement

I'm working on an excel spreadsheet and want an if statement in a cell that allows user input if a certain condition is met, and calculates a value otherwise. Something like
=if(condition true, whatever user wants, 5*$A$1,)
Is there a way to do this?
You won't be able to have the user-input in the same cell as your formula. (without using VBA)
To do it without VBA you will need to use at least 2 cells, one with your formula, and one for the user value
There are a couple of ways you can do it with VBA
Heres a simple one, but would not really recomment it, if lots of cells use this it you'll get lots of inputboxes!
usage: =IF(condition, UserInput(), false result)
Public Function UserInput() As Integer ' used integer as an example
Dim Result As Variant
Result = Application.InputBox("Enter an Integer", "Input Required", , , , , , 1) ' inputbox, the final 1 makes it only accept numbers
If VarType(Result) = vbBoolean Then
UserInput = 0 ' the default value
Else
UserInput = CInt(Result) ' make sure its an integer
End If
End Function
Another one, would involve using the selection change and cell change events to read the initial value of the cell being changed, and allow the change (adding the value into the initial formula's "true" block or deny the changes by reverting the cells formula to the initial one.
You either need to use a Macro to update only null columns or you need to allow user to enter values in another column and then merge the values in this column, third option is to fill it with formulas and allow people to edit it to any value if they want only values
=IF(C11="Economic",120,IF(C11="DBServer",480,IF(C11="Gamer",120,IF(C11="Custom",M15,"null"))))
My example was to build an optimal computer given certain constraints. There was a drop down with Economic, DBServer, Gamer, and Custom as options. If you chose economic, then 120 would show up in the cell, DbServer meant 480, etc. If you selected custom, then it would refer to cell M15 which was a user input that didn't affect the code of the cell you wanted the final number in.

Resources