Formula and Manual user input in same cells of the Column

chintanbtech

New Member
Joined
Jan 16, 2012
Messages
6
Hi,

Please find below the Sample spreadsheet.

ACDAVALAVALCDAVCDREF
AAppleFruit
BBananaFruit
CCatC for CatAnimal
DDogD for DogAnimal
EElephantE for ElephantAnimal
FFoxF for FoxAnimal
GGrapeFruit
............................

<tbody>
</tbody>




Is it possible to create a macro which fulfill below conditions?
If in Column D (AVCDREF) the value is Animal then in Column C (AVALCD) formula should appear as =IF(D2="Animal",A2&" for "&B2,"").
If in Column D (AVCDREF) the value is Fruit then in Column C (AVALCD) Formula should not be appeared and user should allow to enter any value in the corresponding cell of Column C. In case in Column D (AVCDREF) the value is updated to Animal then again formula should appear.


Thank you for your help!

Regards,
CP
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
There's no need to clear the formula in column C if column D does not contain "Animal" :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cel As Range
Set rng = Intersect(Target, [D:D])
If rng Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each cel In rng
    If LCase(cel) = "animal" Then _
    cel(1, 0).FormulaR1C1 = "=IF(RC[1]=""Animal"",RC[-2] & "" for "" & RC[-1],"""")"
Next
Application.EnableEvents = True
End Sub

If you wish, you could prevent column C being changed if column D contains "Animal":
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range, cel As Range
Set rng = Intersect(Target, [C:C])
If rng Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each cel In rng
    If LCase(cel(1, 2)) = "animal" Then cel(1, 2).Select
Next
Application.EnableEvents = True
End Sub
Note : This second procedure does not prevent changes by dragging into column C, or by "pasting through" column C.
 
Upvote 0
Hi Footoo,

Thank you for the prompt response.

The Code is working perfectly fine when I enter the values in Column D (AVCDREF) however in my sheet the values of Column D are populating using formula and the code is not working if Column D has formula :(.

Regards,
CP
 
Last edited:
Upvote 0
Try:
Code:
Private Sub Worksheet_Calculate()
Dim rng As Range
Set rng = Range([C2], Cells(Rows.Count, "D").End(xlUp)(1, 0))
Application.EnableEvents = False
rng.Formula = "=IF(D2=""Animal"",A2 & "" for "" & B2,"""")"
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Or perhaps :
Code:
Private Sub Worksheet_Calculate()
Dim rng As Range, cel as Range
Set rng = Range([D2], Cells(Rows.Count, "D").End(xlUp))
Application.EnableEvents = False
For Each cel In rng
    If LCase(cel) = "animal" Then _
    cel(1, 0).FormulaR1C1 = "=IF(RC[1]=""Animal"",RC[-2] & "" for "" & RC[-1],"""")"
Next
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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