EXCEL VBA Countifs with dynamic range based on cell value [duplicate] - vba

I'd like to know how to pull cell references from the value of another cell and insert them into a formula.
For a simple example:
In cell A1 I have this: COUNT(B4:H4)
Instead of choosing the range when I set up the formula, I'd like this range to be dynamic, based on the value in other cells.
So, I want to type in cell references: in cell B4, I'd type in C5, and in H4 I'd type in C8. How can I set up my formula to look inside cells B4 and H4, but then return the range that is defined by the values in these cells (C5:C8 in this case...)
So in the end, cell A1 would return the count of C5:C8
I hope that makes sense. It seems like something pretty simple but I can't seem to find info on it.

Use INDIRECT()
=SUM(INDIRECT(<start cell here> & ":" & <end cell here>))

Related

Excel VBA - How To Retain Cell Value Even After The Reference Is Removed

How do you retain the value of a cell, for example cell B1 where B1 = A1, after the value of reference cell, A1 is removed?
No, I'm not looking for Copy & Paste Values (V). Is there a VBA code to do this?
As excel clear every variable values at the end of every subroutines. I would like to keep this value for reference all the time.
You set the value to the value, which will remove the references and formulas.
Range("B1").Value = Range("B1").Value

Copy a number starting with 0 in vba and paste is as such

say cell A1, A2, A3 contains value "00V", cell An contains value "029"
I'm doing a comparison for consecutive cells in column A like:
If A1 not equal to A2 then ill copy the cell value of A2 and paste it into a new worksheet in Column A first consecutively.
When I compare 00V and 029 (i.e. both are unequal) ill copy 029 and paste into new sheet
But "029" gets pasted as "29"
How do I fix this in Excel vba?
You could use Format(val, "000") where val is the value of whatever you're copying.
Or you could use rng.NumberFormat = "000" where rng is the destination range of where you're copying the values to.
Convert the target column format to text and then use paste special values. You'll be able to preserve the zeros.
The easiest way is to add a apostrophe (') in front of the values. When copied, the 0 will be maintained.

Call a cell by using a value in another cell

I am trying to call a simple cell (Eg: =a1) in Excel. However this "a1" is available in another cell, let's say in cell 'e1'.
In another way, what I mean is e1 cell has the value "a1" in it and for that reason alone I want to call a1. Finally will in turn populate the value that is actually in a1.
I need to do this is because the cell no. that needs to be populated is retrieved from a formula in e1. That is how the cell a1 comes into the picture.
Using the function: =Indirect([cell]) will give you the value in the [cell]. For example, if cell Z1 has the function =Indirect(E1), and E1 has the value A1, will give you the value of A1 in Z1. Then if E1 changes to, say, A2, then cell Z1 will contain the value from A2.

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!!

Change sheet name inside formula by changing cell value

I have this formula:
=COUNTIF('5-5-2014'!H:H;"NEW")
and I have cell K9 have a value of "5-5-2014".
I want to modify this formula so that I could change sheet name inside formula by changing cell K9 Value. So I want something like this:
=COUNTIF('K9'!H:H;"NEW")
I know this formula is totally wrong! Is there any way doing that?!
=COUNTIF(INDIRECT("'"&K9&"'!H:H"),"NEW")
Where K9 contains the sheet name