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

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.

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.

Google sheet - how to set conditional formatting on a cell block using a cell value outside of block

I'm a Google sheet newbie. I'm trying to work out how to set a conditional formatting 'greater than' rule for a cell block where the criteria value is outside the block. I've seen elsewhere that the solution requires a custom rule but I can't understand how to build the equation.
The cell block is: H10: M64.
The criteria value is at cell 'I6'
What equation do I set to force the conditional formatting to apply at a cell level within the H10:M64 block, so that if the cell value is greater than the value at 'I6' the conditional formatting will apply?
The key to this is absolute vs. relative cell references.
A primer on absolute vs. relative cell references
If you write a formula in cell A3 that says =A1*2, that reference is relative. If you copy your formula into cell B3, it now says =B1*2.
You can keep this from happening and always refer to the exact same cell, no matter where you copy the same formula, with absolute cell references. Add $ before the row and column references to lock the formula in on the same cell reference. If your original formula says =$A$1*2, then when you copy it over to cell B3 then the formula stays exactly the same: =$A$1*2.
You can also just freeze either the row or the column, referencing $A1 (So the row will shift relatively, but will always refer to column A) or A$1 (the columns will shift relatively, but will always refer to row 1).
How this relates
In your conditional formatting rule, you want every cell in the range to be compared to the exact same cell: $I$6. But how do you get every separate cell in the range H10:M64 to compare itself to $I$6? Relative cell references.
Write your custom formula like so:
=H10>$I$6
By comparing the relative cell reference for the top-left cell(H10) in the range to the absolute cell reference for $I$6, The formula will compare every cell relative to H10 in the range to the same comparison cell and format accordingly:
H10>$I$6,
H11>$I$6,
...,
M64>$I$6
Screenshot:

Macro to find and delete column based on cell value

I have two sheets in an Excel workbook. I would like a VBA code to search for the content of cell J19 in Sheet2 and delete the column with the matching cell in Sheet1. I have been searching all around Google to look for some VBA codes, but I haven't found anything yet that works.
Does anybody here know how to do this? I have zero experience in VBA.
You could try this code to perform such a task.
Sub FindMatchAndThenDeleteColumn()
FindContent = Worksheets("Sheet2").Range("J19")
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
If Cell.Value = FindContent Then Columns(Cell.Column).Delete
Next
End Sub
Put the code above in your workbook's module. Note that FindContent and Cell are randomly chosen here, you can use any names. I use
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
to loop through every cell that is in use in Sheet1. It will check everything in the range from cell A1 to the last cell with data (the bottom right-most cell). If the content of cell J19 is a text, you can declare the variable FindContent as a String type, i.e. Dim FindContent As String. If it's a number, you can declare it as a Long type or a Single type or any number type that fits the content. Here I don't declare it which means it defaulting to a Variant type. And since you're a beginner in VBA, you may learn it from Excel Easy. Hope this helps.

Working with merge and center cells in Excel using VBA

If I take a range over merge and centered cells in excel, are each of them addressed as single cells or the group of cells constituting them? For example, if the cells A1 to A10 are merged and I do a
Worksheets(1).Range("A5")
Would this return the range of a single cell among those constituting the merged cell or a range consisting the merged cell itself? And what value would it contain if I get its value?
In other words, how would you represent range over merged cells in excel VBA?
Also, how can I get the range of the non merged cells adjacent to the length of this merged cell?
In other words, how would you represent range over merged cells in excel VBA?
By addressing it with the top left cell. Ex: "A1" in this case.
When in doubt, check it yourself first. If still in doubt, search google or whatever search engine you use. Still if something is unclear, ask :)
Would this return the range of a single cell among those constituting the merged cell or a range consisting the merged cell itself? And what value would it contain if I get its value?
It would return a single cell A5 which doesn't have anything in it because when you merge cells the data from the top left cell is kept and rest discarded. The reason why I say discarded is that if you now unmerge the cells, you will not get your values back.
Best way to check:
Let's say A1 to A10 had 1,2,3..10. Merge them. The cell will now have only 1
Try this in Immediate window
?Range("A5").Value
You will get nothing. Similarly if you want to write to it, you cannot use Range("A5").Value = "Blah". You have to address it with the top left cell. For example
Range("A1").Value = "Blah"

I have created a macro using the if function but it doesn't seem to work. What am I misisng?

I created a macro to show the following:
If (I3<>0,I3*G3,H3*G3) and this repeats itself for cell N3, R3, V3, Z3 etc.
Option Explicit
Sub Eg()
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = "=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
End Sub
However this doesn't seem to work.
Let me explain a bit more how this should work:
This report needs to be downloaded from an application.
The macro needs to be attached to this report so that when I download the report the macro automatically runs this formula in the appropriate columns.
Also I'll have to populate the spreadsheet for all the rows with this formula.
The columns where the formula should sit are not blank but this needs to be catered for in the report automatically once the macro is run.
What am I missing here?
When you use Range.Formula = formulaAsString, and the range refers to multiple cells, you specify the exact formula string as required by (only) the first cell in the range, while appropriately using relative vs. absolute cell references because the assignment to multiple (succeeding) cells will occur as if you were pasting the first cell in the range into the others, exactly as if you'd done a copy & paste without VBA -- you use absolute addressing A1 vs. A$1 vs. $A$1 vs. $A1 etc... as desired to achieve the right alteration of the formula for the succeeding cells.
For example,
Range ("A1, C1, E1").Formula = "=A2+$A2"
will have the same result as
Range ( "A1" ).Formula = "=A2+$A2"
Range ( "C1" ).Formula = "=C2+$A2"
Range ( "E1" ).Formula = "=E2+$A2"
You are mixing up .Formula with .FormulaR1C1! Your string is R1C1 style, but you assign it to the A1 style formula.
Therefore, simply, change it to:
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").FormulaR1C1 = _
"=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
or
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = _
"=IF(I3<>0,I3*G3,H3*G3)"
As Erik points out in his answer, also the later will work and adjust the formula for each cell in the same way (which is not necessary in R1C1 as the formula stays the same anyway...)