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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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