Vba/formulas

manmah

Board Regular
Joined
May 11, 2009
Messages
70
Hello, This is really getting me down....
Is there any way at all for VBA to use the returned value in a formula and treat the actual formula as blank.
I want the VBA to look at a cell and when the cell contains "Y" do what the macro says, but all it sees is a formula???

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe you could post your code and explain in context?
 
Last edited:
Upvote 0
Hi please see below:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range("D18")) Is Nothing) And Target.Cells.Count = 1 Then
ActiveSheet.Unprotect
If Target.Value = "Y" Then
Target.Offset(0, 3).Locked = False
Else
Target.Offset(0, 3).Locked = True
End If
ActiveSheet.Protect
End If

If Not (Intersect(Target, Range("D22")) Is Nothing) And Target.Cells.Count = 1 Then
ActiveSheet.Unprotect
If Target.Value = "Y" Then
Target.Offset(0, 3).Locked = False
Else
Target.Offset(0, 3).Locked = True
End If
ActiveSheet.Protect
End If

If Not (Intersect(Target, Range("A24")) Is Nothing) And Target.Cells.Count = 0 Then
ActiveSheet.Unprotect
If Target.Value = "Y" Then
Target.Offset(0, 3).Locked = False
Else
Target.Offset(0, 3).Locked = True
End If
ActiveSheet.Protect
End If


End Sub

The first two parts work fine because the cells they refer to are entered by the user, however, the third part does not work as it refers to a cell (A24) containing A formula because to unlock the cell (D24) it requires multiple arguments.
I hope you can help as its driving me mad..

Thanks
 
Upvote 0
This might work, if the precedents of A24 are on the same worksheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        If Not Intersect(Target, Union(Range("D18, D22"), _
                                       Range("A24").Precedents)) Is Nothing Then
            ActiveSheet.Unprotect
            Target.Offset(0, 3).Locked = Target.Value <> "Y"
            ActiveSheet.Protect
        End If
    End If
End Sub
 
Upvote 0
No it didn't like that.. I tried putting .precedents into my code but still did not like it..
What I don't understand is that a conditional format will read the cell as Y but it seems like the VBA just sees the formula?????????
 
Upvote 0
1. If you select the sheet of interest, open the VBE Immediate window and enter

? Range("A24").Precedents.Address

... what does it return?

2. What's the formula in A24?
 
Upvote 0
It returned the following:
$J$4:$J$5,$D$22,$G$22,$D$14

The Formula is:
=IF(J5<>"",IF(J5="DNATYY","Y",IF(J5="DNATYN","Y",IF(J5="SLSYN","Y",IF(J5="SLSYY","N")))),"")
 
Upvote 0
Then try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D14,D18,D22,G22,J4,J5")) Is Nothing Then
        ActiveSheet.Unprotect
        Target.Offset(0, 3).Locked = Target.Value <> "Y"
        ActiveSheet.Protect
    End If
End Sub
 
Upvote 0
That has locked the cells which refer to a manual input but not the one which has a formula in. However that does work better for me for the other cells I wish to lock, but i'm still stumped with the formula thing because to lock that cell I need multiple arguments...Thanks for trying tho....
 
Upvote 0
What I don't understand is that a conditional format will read the cell as Y but it seems like the VBA just sees the formula?????????
Just to clarify a little. It is not vba as such that 'sees' a formula or a value but the Worksheet_Change event. From the vba Help, with my emphasis
Worksheet.Change Event
Occurs when cells on the worksheet are changed by the user or by an external link..
.
.
Remarks

This event doesn't occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
So, what shg is trying to do for you is to track back and find precedent cells that are changed manually and use those with the Worksheet_Change event to then check the value in the formula cell(s).
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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