Search and replace partial string in vb.net code - string

I have the following code as an example:
A = 1 - A1 + 40
B = 1 - B2 + 40
C = 1 - C3 + 40
How can I use REPLACE in Visual Studio so that it misses A1, B2 and C3 and substitutes the rest with a different code, so it looks like the following code for example?:
A = 2 * A1 + 5
B = 2 * B2 + 5
C = 2 * C3 + 5
It's a simple example, but I've found myself in situationes where I would just like VS to only REPLACE a certain part of the code and miss other parts. I was trying to look for something like this:
SUBSTITUTE 1 - ** + 40 WITH 2 * ** + 40, if you get what I mean.

A regex replace would be probably the easiest and most flexible way to solve this, i.e. using the alternate | operator to define alternative characters to replace with Z1: Demo
(?:A|B|C)5
You could also alternate between specific cell coordinates in the same way, e.g.
(?:A5|B6|C7)

Related

Get Sum of Products in same column

I have the below data set:
Let's assume it starts in A1. I would like to calculate the Total Costfor January by taking each Count and multiplying by the Rate below it.
I currently have the following, nonsensical formula:
Total Cost = (B1 * B2) + (B3 * B4) + (B5 * B6) + ... + (Bn + Bn+1) for n > 0
Is there a whizzy way to do this using an Excel formula? Perhaps something with SUMPRODUCT()? But I can't seem to get that to work the way I need it...
Simplified example:
=SUMPRODUCT(--(A1:A5="Count"),B1:B5*B2:B6)

Increment a number within a formula by 2 each time

As in this image:
I have the following formula in merged cellset F229:F231, which works correctly:
=OFFSET('Food Diary'!$A$2,31*(ROWS($F$18:F229)-3),)
This pulls data from another worksheet - in this case the value 73.0 is pulled (there are similar formulas in the cell range H229 to M231.
Essentially I want the -3 part at the end of above OFFSET formula to increment by two each time I copy and paste the three row set. So in the merged cellset of F232 - F234 it would be -5 and in the next one it would be -7, then -9 etc.
It's a bit of a hack but this would result in the correct data being pulled. This is not a work spreadsheet, just a personal log to record my food etc so doesn't have to be ideal.
Change -3 to +2*Row()/3 + c, where c is a modifier to ensure that your first row lines up
For example, if the first row is line 4, and you want the value to be 2:
- 2 * Row() / 3 + c
- 2 * 4 / 3 + c
- 8 / 3 + c
- 2.666 + c
c = - 1/3
- 2.666 - 1/3
- 3
- 2 * Row() / 3 - 1/3
Then, when you copy it down to Row 7:
- 2 * Row() / 3 - 1/3
- 2 * 7 / 3 - 1/3
- 14 / 3 - 1/3
- 4.666 / 3 - 1/3
- 5
Use one of the following formula for your counter starting at -3 and changing by -2 every three rows. You can either start using row A1 or you can reference your current cell and make some adjustments to the formula to achieve the same result.
=-3-2*(ROUNDUP(ROW(A1)/3,0)-1)
or
=-3-2*(ROUNDDOWN((ROW(G229)-ROW($G$229))/3,0))
both will work for generating the number you are looking for
Your final formula might look like:
=OFFSET('Food Diary'!$A$2,31*(ROWS($F$18:F229)+(-3-2*(ROUNDUP(ROW(A1)/3,0)-1))),)

Order of formula calculation in Excel 2016

I would like to use the result of a formula A in another cell that is a part of the formula A and avoid circular reference. Also, iterative calculation doesn't sound like what I need.
Lets say I have a table with a single column of which each cell uses a formula that evaluates the state of the whole table and it does so from the top most cell to the bottom.
For example, formula is =XXX+SUM($A$1:$A$4) where XXX is the content of each cell and the table looks like this initially:
A
--
1 | 3
2 | 5
3 | 1
4 | 6
Lets say I have a trigger for calculation start and the first cell starts calculation:
A1 is now 3 + (3+5+1+6) = 18
after that A2 starts calculation and A2 is now 5 + (18 + 5 + 1 + 6) = 35
after that A3 starts calculation and A3 is now 1 + (18 + 35 + 1 + 6) = 61
after that A4 starts calculation and A3 is now 6 + (18 + 35 + 61 + 6) = 126
So the final table would look like this:
A
--
1 | 18
2 | 35
3 | 61
4 | 126
Please understand that this is a simplified example, I am aware this calculation can be optimized but that is not what I need.
Is there a way to avoid VBA?
Only VBA will allow you to avoid hitting the circular reference problem.
If you are prepared to separate your output from your input then you can use Excel to replicate the calculation steps or with a bit of analysis you can simplify the calculation steps (though I suspect this is what you term as optimizing). Picture below shows both

"Consolidation" algorithm name / implementation

Not quite sure how to describe this, but I have a word game I like to play that I'd like to implement as a computer program.
The basic gist is that you look at the values of the letters (A=1..Z=26), and consolidate the letters into the fewest possible, and that are the closest possible to each other.
As an example:
s t a c k
Sum the values
19 + 20 + 1 + 3 + 11 = 54
Find the fewest number of letters:
ceil(54/26) = 3
Choose letters closest to each other
54/3 = 18
Letters to be displayed should be rrr.
That happens to be an easy example. What would it look like when you need to have, say, rrs (if your initial string was 'a stack' instead)?
Does this already have a name that I can lookup and implement?
I think your problem boils down to this: given n and k, find numbers r1, r2, ..., rk such that sum(r1 + r2 + ... + rk) = n and max(r1, r2, ..., rk) - min(r1, r2, ..., rk) is as small as possible.
The solution is pick r = floor(n / k), and set n mod k of the numbers to be r + 1, and the rest r.
For example, if n = 55 and k = 3 (your example), we have floor(55/3) = 18 and 55 mod 3 is 1, so the solution is 19, 18, 18.
All that remains is converting between numbers and letters.

Find name of the last cell with data in a row

I've searched for an answer, but it seems that I can only find VBA-based solutions, which appear no longer be an option in Excel 2008.
I'm trying to return the name of the last cell in a row that contains data. It looks like this:
+===========================================================+
A / B / C / D / E (columns)
+===========================================================+
1 || [Formula] / 3 / 4 / 5 / [blank]
2 || [Formula] / 7 / 8 / [blank] / [blank]
3 || [Formula] / 9 / 10 / 11 / 12
(rows)
+===========================================================+
and the end result would look like this:
+===========================================================+
A / B / C / D / E (columns)
+===========================================================+
1 || D1 / 3 / 4 / 5 /
2 || C2 / 7 / 8 / /
3 || E3 / 9 / 10 / 11 / 12
(rows)
+===========================================================+
It looks like your data is sorted from left to right.
If it is, you could start with something like =MATCH(MAX(C4:K4);C4:K4) to retrieve the column index of the last cell (max) of the row. From there, it's easy to buid the address:
=ADDRESS(ROW();MATCH(MAX(C1:K1);C1:K1);4)
Edit: combining ADDRESS with COUNT or COUNTA (great idea from Dustin Geile) does not require the items to be sorted:
=ADDRESS(ROW();COLUMN()+COUNT(B1:Z1);4)
=CHAR(COUNTA(B1:ZZ1) + 65) & ROW()
You can try this arrayformula:
{=ADDRESS(ROW(B2),MAX(IF(ISEMPTY(B2:H2),0,COLUMN(B2:H2))))}
Validate with CtrlShiftEnter
If you are looking for the last cell with data, you can write a quick function and place it in a VBA module in your workbook.
Public Function LastCell(inRange as Range)
LastCell = inRange.End(XlToRight).Address
End Function
Or just omit the '.Address' if you want to get a range object pointing to the cell.
You can then use it in a formula like...
=LastCell(A1)
Regards
Ray

Resources