Please ads & support readers
I have a 3x3 table numbered from 1-9 as shown in the attachment.
Now how do I get the random number in this table shown in column E1?
And a little more upgrading (for myself) that is for example: E1 random cell B2 - ie 6 (according to his picture) - is always poured the same ink B2.
To actually get a random value from your table, which presumably might not always have the numbers 1 to 9 you can use this formula in E1
=INDEX($A$1:$C$3,RANDBETWEEN(1,3),RANDBETWEEN(1,3))
For differently sized tables you can use this generic formula
=INDEX(Table,RANDBETWEEN(1,ROWS(Table)),RANDBETWEEN(1,COLUMNS(Table)))
You can use conditional formatting to highlight which number was picked
Select the range A1:C3 and apply conditional formatting with this formula
=A1=$E$1
format as required
You can use the following code to act as random number generator, and print result on cell E1.
Sub random_num()
'Initialize the random number generator
Randomize
Dim wk As Worksheet
Set wk = ThisWorkbook.Sheets("x")
'Random whole number between 1 and 9 :
random_number = Int(9 * Rnd) + 1
wk.Cells(1, "E").Value = random_number
End Sub
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 have two worksheets with data from different sources. I need to copy the data to a single worksheet and remove duplicates. To achieve this objective, I need all the data formatted the same on both worksheets. All of this is already coded except with one column of data I am having issues. These columns contain a representation for percentage. In worksheet A, the value is showing as .4386 which equates to 43.86%. I use this code that converts the value without issue:
Worksheets("Verification").Range("F2:F2000").NumberFormat = "0.00%"
In worksheet B, the same data is shown as 43.86, but the above code changes it to 4386.00%. I also tried changing this line to .NumberFormat = "General\%" and this almost works, but returns a value of 44%. What do I need to add to my code to get this to show 43.86% on worksheet B?
Sorry for the slow reply in comments - I will just submit an answer.
Like Ralph said, it's really better to make sure they are the same number.
43.1 and .431 are not the same number.
For Each c In [A1:A10]
If c.Value < 1 Then
c.Value = c.Value * 100
End If
c.NumberFormat = "0.00\%"
Next c
Results:
You are stating that .4386 on worksheet A is the same data [...] as 43.86 on worksheet B. So, Excel is correct to convert 43.86 to 4386.00%. Maybe you need a conditional formatting: when the number is smaller or equal to 1 then format it "0.00%" and otherwise format it as "0.00""%""".
Yet, I would assume that you'll be running into problems when comparing the data between the sheets with this solution. Hence, I would divide all numbers on sheet B by a 100 first to really make them comparable.
Note, that just by making numbers "look alike" they are not the same. Example: write in cell A1 the value 1000 and in cell B1 also 1000. Then change the number format for A1 to 0 and the number format for B1 to 0, (or to 0. outside the US). A1 will show 1000 while B1 will show 1. If you ask in cell C1 =A1=B1 you will get a TRUE as the answer.
I have a range of cells which I want to do some math on. But I also want those cells to contain some text.
For instance I want the sum of A1 and B1 where A1 contains the number 10 and "z001" and B1 contains the number 20 and "Z004".
Then I want the formula to ignore the text, and just come up with 30.
Is this possible?
For a quick solution, type "=Left(A1, 2) + Left(B1, 2)" into C1. Drag this equation down the rest of your range and you should get the results you want, provided the numbers you are adding are all 2 digits.
You can also use VBA if you need to run the same equation on multiple cells.
If you can get the same results by just removing the letters, try:
For i = 58 To 127
'Change out str with the variable name you have assigned to your cell value.
str = Replace(str, Chr(i), "")
Next i
58 and 127 represent the first and last positions in a range of characters on the Ascii table that are not numerals http://www.asciitable.com/
If you just want to include the first two numbers of each cell in your equation and ignore the "Z00#", you can try:
strLeft = Left(str, 2)
This will reduce your string down to the first two characters of each cell.
You can look here for other ways to remove characters you don't want.
http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=269:excel-vba-string-functions-left-right-mid-len-replace-instr-instrrev&catid=79&Itemid=475
Here is in example of how you would implement something like this with simple addition.
Dim a as range
Dim b as range
Dim aLeft as integer
Dim bLeft as integer
Dim cleft as integer
a = Worksheets("WorksheetName").Cells(A1).Value
b = Worksheets("WorksheetName").Cells(B1).Value
aLeft = Left(a, 2)
bLeft = Left(b, 2)
cLeft = aLeft + bLeft
Worksheets("WorksheetName").Cells(C1).Value = cLeft
This would add the first two digits of cells A1 and B1 then display the result in C1.
As I see it, you have 2 options:
Search for the number within each cell and sum it up (see below).
Split the columns to have 1 column of numbers and one of codes (e.g.
"z001"). See on the "Data" tab on the ribbon and click "Text to
Columns" on the Data Tools group.
The first option would be the quickest and more straightforward. You need to make a third column where the sum will be and then use, for example, the function LEFT. This function allows you to retrieve characters from a cell. See example below:
To get "30" I have used the following formula on C2:
=LEFT(A2,2)+LEFT(B2,2)
Note this is not ideal since this formula is looking for 2 characters every time. If you have a scenario with the following code "5z005" it won't work because it will try to sum "5z" as if it was a number. In that case you're better off finding a pattern (code = "number" "z" "number") and splitting the columns as I said on option 2.
I want to make a series of tables that each contain 25 values, that come from a set of 30 values. How can I quickly, and randomly, produce these tables? I'm wondering if there is a way in excel, or will I need to program something, myself? If so, which language would be the easiest (Python, C, Java)?
Edit: The 25 values would include no repeats. In other words, I'm looking for random combinations (30C25) of the values.
You can eventually add the following custom User-Defined function and then use it as an array formula. Add the code to code module Module1:
Public Function RandUnique(src As Range) As Variant
Dim v As Variant: v = src
Randomize
Dim i As Integer, j As Integer, temp As Variant
For i = 1 To src.Rows.count
j = 1 + Int(Rnd * src.Rows.count)
temp = v(i, 1): v(i, 1) = v(j, 1): v(j, 1) = temp
Next
RandUnique = v
End Function
Once you have added this UDF,
Select the destination range, enter in the formula bar the following formula
=RandUnique($A$1:$A$30) ' <~~ set it to your source range of 30 values
Then press Ctrl+Shift+Enter
Please note that the randomization procedure used is rather basic, so that not all the combinations have really equal probability, but it is fair enough, unless you are using it for some deep statistical analysis, in which case you might need a perfect randomizer.
Name a list of your thirty values in Excel in rows greater than 25 (say List30), then in A1 copied down to A25 and all copied across to suit:
=INDEX(List30,RANDBETWEEN(1,30))
To exclude repetitions (so not random choices) you might enter your list in A1:A30 and in B1 copied down to suit:
=RAND()
then sort A:B on ColumnB and copy A1:A25 to paste say to D1. This way only one set is generated at a time (the sort/copy/paste would have to be repeated after each paste).
Here is an interesting way that requires no VBA nor any manual sorting.
Enter your source list in the range A1:A30.
In cell B1 enter this formula:
=CHOOSE(RANDBETWEEN(1,7),7,11,13,17,19,23,29)
In the range C1:C30 enter this formula:
=INDEX(A$1:A$30,MOD(ROWS(A$1:J1)*B$1,30))
In cell D1 enter this formula:
=RANDBETWEEN(1,30)
Now select any 25 contiguous vertical and empty cells and enter this formula:
=INDEX($C$1:$C$30,MOD(D$1+ROWS($A$1:$J1),30)+1)
Now copy the 25 cells and paste as values somewhere for your 1st table. Press F9 on the keyboard to get a fresh 25; copy and paste as values somewhere for your 2nd table. Press F9 on the keyboard to get a fresh 25; copy and paste as values somewhere for your 3rd table. Keep repeating for as many tables as you need.
.
Please note that while this will look very random (with no repeats) it is not random at all. It's a complex interference pattern that will appear completely random unless you are the Rain Man.
I'm trying to set a formula for a cell in Excel but I have a strange problem. With the line:
Range("BG4").Formula = "=CustomSum(BG:BG)"
it works fine and the correct formula is set into the cell. But I have another line of code:
Range("BH4").Formula =
"=if(R1C1<>"""","""",if(CustomSum(BH:BH)=0,""geplant!"",CustomSum(BH:BH)))"
This line produces a formula in the cell which looks like this:
=IF($A$1<>"";"";IF(CustomSum(BH:(BH))=0;"geplant!";CustomSum(BH:(BH))))
which is unacceptable since it doesn't work (parentheses in parameter are inserted automatically).
How do I avoid that?
CustomSum looks as follows:
Public Function CustomSum(rng As Range)
Dim Sum As Double
Sum = 0
For row = 10 To 48 Step 2
If IsNumeric(Cells(row, rng.Column).Value) Then
Sum = Sum + Cells(row, rng.Column).Value
End If
Next
CustomSum = Sum
End Function
For the sake of an answer, copied from a Comment by #simoco:
"=if(R1C1<>"""","""",if(CustomSum(BH:BH)
... you can't mix R1C1 and normal formulas
I had the same issue, and found a response here: Absolute referencing - Inserting the equivalent of '$' in an equation written in R1C1 notation.
Long story short, since you have R1C1 at the beginning of your formula, the BH:BH needs to be in R1C1 coordinates as well. BH is column 60. I'm not sure how to specify a whole column as you would in BH:BH, but if your total rows will always be under some large number (500? 6000? depending on your data), you could specify a large range and hopefully have it work fine.
If you have a title for your column, and your data starts row 2, I would write it like this:
Range("BH4").Formula =
"=if(R1C1<>"""","""",if(CustomSum(R2C60:R6000C60)=0,""geplant!"",CustomSum(R2C60:R6000C60)))"