How to keep the cell reference in VBA - excel

Lets say I have the following data:
A B C
1 =B1
2 =C2
3 =C3
I want to write a macro than can add text around the values in column A without losing the reference.
For example
A B C
1 [Hello].[1] 1
2 [Hello].[20] 20
3 [Hello].[10] 10
As an example for A1 I use:
.cells(1,1) = "[Hello].[" & .cell(1,2) & "]"
This give me the wanted value but in the end I lose the cell reference in A1.
I rather want this:
A B C
1 [Hello].[B1] 1
2 [Hello].[C2] 20
3 [Hello].[C3] 10
Of course with the actual value of the reference and not just the addresses.

With .cells(1,1)
.value = "=" & .cell(1,2).address
.format= ""[Hello].[" 0 "]""
End With
this should do. more info on this excel exotic syntax on that random website https://exceljet.net/custom-number-formats
best of luck, but consider using another language than vba

Related

how to reference a specific cell in a formula if other cells match a specific value

Thanks so much for looking at my question! I am trying to create a formula that subtracts a specific value from another formula. However, that specific value may change.
Example:
A B C D
1 1 100 =(2000 - ( if A = 1, i want to subtract the C value where B =1))
1 2 250
1 3 310
1 4 .
2 1
2 2 =((2000 - ( if A = 2, i want to subtract the C value where B =1))
2 3
2 4
3 1
3 2
3 3
3 4
(A,B,C,D are the columns)
Hopefully this makes sense! I am trying to subtract the C value that goes along with the B1 value for each different A.
I was thinking an index match of some sort but wasnt exactly sure how to do that when the A's change. Thanks so much in advance for help!
INDIRECT or INDEX functions can help you. See this answer.
Would something like a nested if function work for you here? For example:
=IF(A2=1,IF(B2=1,2000-C2,"Enter calculation if B2<>1"),"Enter calculation if A2"<>1)
If this works, then you can simply copy/paste the function down the rows in column D.

Excel: sort names by given values

I had to do a little work with excel but I am stuck at the following point:
A B C D E F ....
1
2
3 "A" 1
4 "B" 0
5 "C" 1
6 "D" 2
7
8
9
My table looks something like this.
In B3:B6 I do have 4 names, in C3:C6, I do have the corresponding scores.
Now, I would like to order the names by their given points and display them in E3:E6
I am not that familiar with excel so I am very happy for any help.
Greetings,
Finn
First get the numbers in order with Large Function:
=LARGE($C$3:$C$6,ROW(A1))
Put that in F3 and copy down 4. It will list the numbers in order.
Then in E3 put this formula:
=INDEX(B:B,AGGREGATE(15,6,ROW($C$3:$C$6)/($C$3:$C$6=F3),COUNTIF($F$3:F3,F3)))
Which looks up the corresponding name.

Excel - Include Row in Sum Based on Comparison to Following Row

Paging All Excel Wizards,
I am trying to see if there is a way to have a one-line SUMIF or something similar to sum up the following criteria in an Excel spreadsheet:
Sum the values of Column C if
If Column A = "Chizzle"
AND If Column B is >= Column B of the next row
Sample Data:
A B C
Type Level Value
__________________
Chizzle 1 23
Chizzle 2 10
Bobbles 3 1.5
Bobbles 3 2.6
Chizzle 2 5.5 <- Should Be counted
Cobbles 2 1
Chizzle 1 3.3 <- Should Be counted
I have tried using something like this:
=SUMIFS(C1:C1000,A1:A1000,"Chizzle", B1:B1000, ">=" & B2:B1001 )
Unfortunately the B2:B1001 part isn't working and it is selecting all values.
If there is a way to do this with a one line calculation, without having to add an additional column? That would be awesome but I'm not sure if it is possible.
Thanks!
Try this SUMPRODUCT():
=SUMPRODUCT(($A$2:$A$8="Chizzle")*($B$2:$B$8>=$B$3:$B$9)*($C$2:$C$8))

Is it possible to add/subtract in one cell, and have it subtract/add in another cell that already has a value?

Is it possible to add/subtract in one cell, and have it subtract/add in another cell that already has a value?
I am thinking it may be a if function but I can not wrap my head around how I would write out the formula.
Let's say you have 2 columns B and C that already contain data.
And if you Add a number to B you want that number to be subtracted from C.
My recommendation is to write a macro that will work as follows:
First the user selects the two columns and then runs the macro
For each row
Cell c = getCell("C" + row);
double cval = c.Value;
c.type = FORMULA;
c.Formula = "=" + (cval + getCell("B" + row).Value) + "-B"+row;
c.Recalculate()
Example:
Original:
A B C
1 Gas 5 10
2 Air 8 12
Replace with:
A B C
1 Gas 5 =15-B1
2 Air 8 =20-B2
so you only change B, and the value of C is automatically calculated.

Run a simulation several times with different parameters and store values

Consider a spreadsheet which performs some computations based on a fixed value (in the rxample below, D3) and is iterative. E.g.
D3: 4
B3: 12
B4: 58 (=B3*$D$3+10)
B5: 242 (=B4*$D$3+10)
B6: 978 (=B5*$D$3+10)
Total = 1290 (=sum(B2:B5))
Now, suppose I wanted to try out different values of D3 (let's call this P) and store the different totals I get, i.e.
P Total
4 1290
5 2252
6 3618
7 5460
How would I do this with Excel? A macro? Please note that the above example is a simplified version of the real thing. It should be clear that I need to compute B3-B6 so I can compute the sum.
Update:
Each computation requires several columns. E.g. we would use values on B3,B4, .. and C3,C4, ... .
A macro can do this. If B7 contains the sum formula, try this
Sub RunSimulation()
Dim p as long
for p = 4 to 7
Range("D3")=p
Debug.Print Range("B7")
Range("L" & (p-1)) = Range("B7").Value
next
End Sub
EDIT: added a line for storing the results, as requested.
If you don't want to enter the sum formula in your sheet, you can calculate the total in VBA either:
Dim total as Long
Dim row as long
total = 0
for row = 2 to 5
total = total + Range("B" & row)
Next
Debug.Print total
(Use Double instead of Long for total if you are dealing with floating point numbers.)
Usually it is done in following manner:
A B C D
1 x tmp1 tmp2 Total
2 3 $A2+10 $B2*10+10 $C2*$B2 The formulae are just for example.
3 $A2+1 $A3+10 $B3*10+10 $C3*$B3
4 $A3+1 $A4+10 $B4*10+10 $C4*$B4
Excel has capabilities to automatically increment indices in formulae.

Resources