VBA Variable in range - vba

Hey is it possible to use a variable within a range object?
My goal is to keep the formula that is in the excel cell, but the value of the cell itself is to be deleted.
The procedure for a better understanding:
1. search for a value, where "i" is indicating the row, where the value is found
2. delete this value, BUT: save the formula
3. another value is entered in this cell and the formula is still active and is checking some condition
Searching is no problem, but I cant delete the value of the cell without deleting the formula aswell.
This is what i tried:
rangeA = "D" & i
Set rConstants = Sheet1.range(rangeA).SpecialCells(xlCellTypeConstants)
rConstants.ClearContents
Any ideas?

You should set the constant equal to rangeA.Formula. You can then use the constant wherever you would like within your module or VBA project.

Related

VBA Excel - Update Cell value based on other cell value

I am trying to write a formula in a Range of cells based on other range cells values from another worksheet. The formula is shown below:
ActiveSheet.Range("G14:G43").Formula = "=Worksheets("1ºperíodo").Range("V14:V43").Value"
But the code doesn't work and I get a syntax error. That must be related with the strings but I don't know how to fix it.
The value at V14 must be equal to the value at G14 until the last cell, i.e., the value at G14 equals the value at V43. Besides the syntax error is the formula correct based on what I expect to have?
"=Worksheets("1ºperíodo").Range("V14:V43").Value"
is not a formula. It is a value.
If you only want the static values then just assign the values:
ActiveSheet.Range("G14:G43").Value = Worksheets("1ºperíodo").Range("V14:V43").Value
If you want a live formula you need to pull vba from the string and use the .Address function:
ActiveSheet.Range("G14:G43").Formula = "=" & Worksheets("1ºperíodo").Range("V14").Address(0,0,,1)
But the above can be simplified to:
ActiveSheet.Range("G14:G43").Formula = "='1ºperíodo'!V14"
With the formula, we only need to refer to the first cell with a relative reference and vba will make the changes to each row.

How to make dynamic cell in formula based on pointed value in excel spreadsheet

I have an issue with the cell range in formula and I don't know how to change it based on a predefined value in the spreadsheet. For example, from figure I have cells B8:B12=0 (5 cells), however, if I want to change range to range B10:B12=0 (3 cells) I should delete them from formula. How can I do the reference to a specific cell in a spreadsheet where I can simply change value 5 to 3 and it will change automatically, without interfering formula each time? I'm new to VBA, any help is appreciated.
As it was mentioned before, you should try offset function and do something like:
AND(SUM(OFFSET(D13,-1,-2,-(G6),1))=0). Then the cells in the range B8:B12 would be possible to change inserting range in cell G6.
Use the =INDIRECT function to define your target cells, e.g.
=TEXT(INDIRECT(A1), "")
If I entered the text B3 into cell A1, then this formula would return the text value in cell B3.
Let me know if it works for you.

Set Value of Cell, relative to active cell, within single column named range

I have written the following code as part of a larger sub-routine to set the value of a cell, relative to the active cell, when a particular selection has been made within the active cell.
ActiveCell.Offset(0, 5).Value = "CLV 7"
While this works, I may have need in the future to add columns into my worksheet and this presents a problem, due to the change of location of the cell that requires its value to be set, and by association the need to rewrite the code each time a new column is added.
In considering this variable, I researched and as a result, defined a range name for each column that requires values to be set within it. I thought that I would then be able to determine the variable & relocatable intersect point between the active row and the named range column and define it as the cell that requires the value to be set.
After this I researched ways to define this variable intersection and attempted to set the following alternate code:
ActiveCell.Offset(0, 0).Range("BusinessStudies").Value = CLV 7
in the hope that it would do the trick, but unfortunately it does not. I have looked at other posts and cannot see how to adjust it with any success as I can't see any similar requests.
try the Intersect() function in VBA
Debug.Print Intersect(Rows(ActiveCell.Row), Range("MyRange")).Value
Edit: apply to your situation, assuming that you want the string "CLV 7" to go into the cell:
Intersect(Rows(ActiveCell.Row), Range("BusinessStudies")).Value = "CLV 7"

How to keep named range formula from changing cell values with VBA

I noticed an interesting problem while trying to debug a VBA routine that sorts the list of worksheets in a range and then redraws the border around that range.
The range containing is defined in Name Manager with the formula as shown below
=Tables!$L$2:$L$22
The problem that is occurring is that while doing the sheet name work, sometimes cells are deleted and sometimes cells are inserted. This CHANGES the cell address values in the named range. So if I deleted two cells and inserted one cell, the formula changes to
=Tables!$L$2:$L$21
And if I happen to insert into the first cell (L2), then the formula changes to
=Tables!$L$3:$L$22
I'm certain that problem can be solved using the header range name and offsetting by one, but I'm not sure how to do that in the formula for a named range as I've tried numerous ways and can't get it right. But I also need the ending range address to be static.
Any help appreciated.
One thing to try is to use:
=Indirect("Tables!$L$2:$L$22")
rather than
=Tables!$L$2:$L$22
It is an acceptable Name, but I am not sure you will get the same functionality.
Found a better way than the comment using the static header range. You simply define the whole range using the offset function from the header.
=OFFSET(AllSheetsHeader,1,0,21,1)
Where the 1,0,21,1 are 1 row offset from header, 0 column offset, 21 is row height, and 1 is just that one column

Excel IF formula to validate another formula

I want to run below formula on all cells, where current value is zero.
Formula on C3
=SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2)
I have tried to put this in another IF statement to validate the cell value first and if that matches condition execute above formula
=IF(C3=0,SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2))
But here I am getting circular reference warning, because the second formula is depends upon same cell value. Is there any other way I can do it ?. mean running the formula if the value is equal to zero on specific cell
You can't reference the cell you are working in without creating a circular reference. I usually just add an intermediate column to avoid this. If you want it all in one cell for some reason:
=IF(SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2)) = 0, "Sums are zero", SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2)))
Its probably takes twice as long to compute, is ugly, and you gotta keep both formulas the identical if you ever edit it, but it does fit in one cell.
Is this all one worksheet, or multiple? I think the below would work, but can't know without the workbook.
=SUMPRODUCT((C3=0) * (raw!$A:$A = A3) * (raw!$C:$C = C2) * (raw!$D:$D))