Trying to compare two strings in excel - vba

I have something like this:
A1: 0069
B1: 030069
In every case, I need to check if A1 is equal to B1 if I were to ignore the 03. I have a lot of cells, and the 03 is just there because its the format of the data. However, A1 can be multiple different lengths, so I can't just use the MID formula and just check B1 after the 03 because I would need to make multiple cases depending on how long A1 is.
Originally, I get the A1 data by using the formula:
D1 =FIND("-",C1), where C1 is A1 in the format: 0069-XXXX-XX.
I then use:
A1 =LEFT(C1,D1-1), which gives me the number 0069 as you see above. This way, I'll always have the correct length when I actually start the real problem.
My question is, can I use some kind of concatenation to just add a 03 to A1 and then see if it is equal to B1. I've tried using & and CONCATENATE, but because they are in general terms, I get the #VALUE error. Maybe I'm just not typing it in correctly, but yea.
Thanks!

You can just use & correctly in A1:
="03" & LEFT(C1, D1 - 1)

To compare these values you could use a formula like below:
=IF(A1=RIGHT(B1;4);"true";"false")
Actually it is not necessary to use the formula in A1 nor D1, you can use your data from column C straight away:
=IF(LEFT(C1;4)=RIGHT(B1;4);"true";"false")

If the pattern is Column C is nnnn-XXXX-XX then no helper column (Columns A and D) is necessary:
=MID(B1,3,4)=LEFT(C1,4)

Related

Excel formula to translate text in one column and show result in another column

I am using Excel 2016 and have text in another language in column A (currently French but could be another in another spreadsheet) and I want to put a formula in e.g. cell B1 which will translate cell A1 and show the result in B1. I then want to drag the formula down (if possible) so then this formula translates each cell in column and puts the result in the corresponding cell in column B e.g. B2 will have the translation in English for A2 and B3 will have the translation for A3 and so on...
Is this possible?
To answer your question:
"Yes"
...it is definitely possible.

subtract the last two non-empty cell in excel

This is I think a simple problem but I can't seem to find the right solution for it. I don't know if VBA is needed for it. Basically I just want to subtract the last two non-empty cell in Excel. Example: I have the cell A1 and cell B1 and then subtract the value of A1 from B1 and place this value at the same row but another column C1 and so on. I have read this to get the last non-empty cell but I need two non-empty cell and perform operation on them.
=LOOKUP(2,1/(A:A<>""),A:A)
Is there an easier way to do this? Or should I use VBA?
EDIT:
the reason is I'm making a somewhat similar to a balance sheet but a simple one, the user will just enter deposited value at the first column and expenses at the second column and then display the balance in the third column.. and vice versa.
EDIT:
Sample
Put this in C4 and copy down:
=SUM($A$3:$A4)-SUM($B$3:$B4)
If you want to copy the formula past the data so it will automatically fill when data is inserted you can do what #Dirk stated:
=IF(A4&B4<>"",SUM(A$3:A4)-SUM(B$3:B4),"")
Put it in C4 and copy down as far as desired.
This way you can fill the column with the formula and as the data is filled it will change from the empty string to the running total.
EDIT:
Regular formula:
=IF(ROW($C1)=1,OFFSET($C1,0,-2)-OFFSET($C1,0,-1),OFFSET($C1,-1,0)+OFFSET($C1,0,-2)-OFFSET($C1,0,-1))
in cell C3 put "=A3-B3" in cell C4 put "=C3+A4-B4" then copy down.
This will start the balance at 3000 in C3 then add any deposits or minus any withdrawals to the rest of column C.

Excel: A function to replicate built-in change in relative references when copying formulas

I need a function that will do what Excel does automatically when you dreag a formula: change the referneces automatically.
For example:
In A1 I have "= A2 + A3"
If i copy this to say C3 it will have: "= C4 + C5"
I need to WRITE a formula in C3 that will produce this.
Any ideas? VBA solution is also welcome
CLARIFICATION:
In need this to be as general as possible.
Meaning A1 can contain ANY formula of any type, containing references to other cells.
for example: "= A2 + A3" or "= VLOOKUP(A2, $C$1:$E$7, 2, True)"
In need to move have this formula, whatever it is, copied to another cell (say C3), w/o the built in copy/paste, and have the references (that aren't set with $) change relatively.
I thought there might be a function to write in the destination file to do this.
I have tried writing an Eval function, and i managed to copy the formula from A1 and have it evaluated in C3, but the references would not change
This question lacks a bit of clarity, but I think this might be what you're after:
=SUM(OFFSET(C3,1,0,2))
This will sum the two cells directly below the given cell (in this case, cell C3). That is, it offsets C3 by 1 row, 0 columns, and grabs a height of 2 cells and then passes the result to the SUM function.
This VBA code would do what you are looking by setting the formula in the active cell:
ActiveCell.FormulaR1C1 = "=R[+1]C+R[+2]C"
You can use the Indirect() function using relative reference style.
For example, if you were in A1 and wanted to sum B1 & C1, it would look as follows:
A1: =INDIRECT("RC[1]",0)+INDIRECT("RC[2]",0)
That will change as you move the cell around to always sum the 2 cells to the left of the cell.
For your specific example (A1 = A2 + A3 || C3 = C4 + C5), it would be as follows:
=INDIRECT("R[1]C",0)+INDIRECT("R[2]C",0)
Hope that does the trick!!

Add Numbers Until Value Reached

I want to have a threshold value in one cell(A1) and take it as a reference for adding cells.
Suppose I have
A1 - 10
A2 - 4
A3 - 2
A4 - 3
A5 - 4
A6 - 6
I want to add cells based on A1(Threshold).
As A1 is 10, cells from A6:A5 should be added - Result:10
If A1 is 6 then cell A6 should be returned- Result:6
If A1 is 16 then cells from A6:A3 should be added - Result:19
Is this possible without VBA? Can i get count of number of cells in return along with sum?
I've added some progression SUM operation with ROW and OFFSET for the following. Note that I have modified and added to your sample data for more thorough results.
      
The SUM formula in C2 is =SUM(OFFSET($A$2,0,0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1,1)) anf the COUNT is derived in D2 with =MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1. TBH, I didn't experiment much with zeroes in the data as I was unsure whether you would want to count them in the progression or not.
You can use the INDIRECT() function.
=SUM(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
for the count use
=COUNT(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
It's hard to hit a moving target but for your revised parameters try the following.
=SUM(OFFSET($A$6,0-MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,)),0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1,1))
 
=MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1
While Excel expects to calculate in a 'down-and-to-the-right' progression, the OFFSET() function will accept parameters to both relocate the starting point and reshape the height and width of the range of cells being summed. Generally, working 'down-and-to-the-right' will allow to leave some breathing room for expansion but you will have to be very careful that you do not attempt to move upwards past row 1 (#REF! error).
OFFSET function
The formula could be simplified if there was any guarantee that nothing of numerical value was ever going to be below A6 but that point has not been addressed so my formulas halt the sum operation at A6.

Macro for Calculating the No of months(count) between Two months

I want to write a macro to calculate the no of Months(Count) between two months (strings)
Means, I want to know how many months are there between January to August
The result should show as 8.. I know how to write using Dates(Numbers), But using Month strings i have no idea..It would be of great help if anyone guide me on this ?
The question isn't too clear, but if I understand you correctly then you can use the following formula:
=MONTH(DATEVALUE("1" & A1 &"2014"))-MONTH(DATEVALUE("1" & B1 &"2014"))
Where A1 and B1 each contain a month name as a string. So for example if A1 was August and B1 was January, then the formula would show '7' as the output.
Using VBA, you can use the same code, replacing A1, B1 and C1 with the relevant ranges:
Range("C1").Formula = "=MONTH(DATEVALUE("1" & A1 &"2014"))-MONTH(DATEVALUE("1" & B1 &"2014"))"