Set range.value as range.address

Limone

Board Regular
Joined
Dec 20, 2018
Messages
57
Hi.
I've got a formula that returns a cell address. I set up a worksheet_calculate event that's supposed to extract it (as a value) and give me the address of the cell next to the one originally returned by the formula among the other things. Sometimes it works but eventually it always crashes.

This is one of the various ways I've tried to make it work...

Code:
Private Sub worksheet_calculate()
Dim i As String
Set Target = ActiveSheet.Range("d15")
If Target Is Nothing Then Exit Sub
If Range("c15").Value = "" Then Exit Sub


i = Range(Range("d15").Value).Offset(0, 1).Address(False, False)


    Range("e15").Value = "'=SE(E(" & i & "<>"""";" & i & "<>""F"");VERO;FALSO)"


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
D15 does contain an address but I still get the same error.

Seriously?

On the MREXCEL sheet, cell D15 is empty, so you have an error.
The cell containing a range is the D5, you have to update the macro:

the code goes on the mrexcel sheet

Code:
Private Sub worksheet_calculate()


    If Range("D5").Value = "" Then Exit Sub
    If Range("C5").Value = "" Then Exit Sub
    '
    'Gets the data that is inside the cell
    valor = WorksheetFunction.Trim(Range("D5").Value)
    'Get the row according to the data obtained from the cell
    i = Range(valor).Row
    
    '
    'Disable the events, since you are creating a formula and this would activate the Calculate event again causing an endless loop
    Application.EnableEvents = False
    'Create the formula
    Range("E5").Formula = "=IF(AND(C" & i & "<>"""",C" & i & "<>""F""),TRUE,FALSE)"
    'Enable events again
    Application.EnableEvents = True
End Sub

See the example below
https://www.dropbox.com/s/mvtotq45lidioh0/Gennaio ASA (1) dam.xlsm?dl=0
 
Upvote 0
I accidentally placed the table in the wrong row when creating a new sheet for you, sorry about that. Anyway, your code works wonderfully, thanks a lot.
Last question: since I ended up inserting the resulting formula as a value and a formula in the end, should I still de-activate and then re-activate the application events or can I delete that part of the code?
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,094
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top