Why is my code not working

dkopp3

New Member
Joined
Feb 22, 2019
Messages
5
This is the first time I've really tried to use VBA in excel.

I'm trying to get a cell's value (K6) to change to another cell's value (B2) whenever P6 is equal to 1. As far as I can tell I have formatted things correctly but whenever P6 changes to 1 in my worksheet, K6 does not change at all. What am I doing wrong? My code is below...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)   
    If Target = Range("P6") Then
        If Sheets("Sheet1").Range("P6").Value = 1 Then
            Sheets("Sheet1").Range("K6").Value = Sheets("Data").Range("B2").Value
        End If
    End If
End Sub

Thank you!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How is the value changing by manual change, code or formula?
 
Upvote 0
What is in P6 ?
Range P6 will only be a target of a change when you directly change the contents of P6.
If P6 contains a formula and you expect that a change of its value will trigger a change event - you are wrong.

Apart from this - you will have to work a bit on understanding referencing to objects consistently and correctly.
e.g. when you use Range("P6") it is good practice to use workbook and sheet reference - not always strictly necessary but helps avoiding strange and confusing results. And after the first if you can use Target instead of Sheets("Sheet1").Range("P6")
 
Upvote 0
It is changing via a formula. How would I get it to work with it changing via formula? Is it possible?
 
Upvote 0
I figured it out! Here's the code that I used...

Code:
Private Sub Worksheet_Calculate()        If Sheets("Sheet1").Range("O6").Value = 1 Then
            Sheets("Sheet1").Range("K6").Value = Sheets("Data").Range("B2").Value
        ElseIf Sheets("Sheet1").Range("O6").Value = 2 Then
            Sheets("Sheet1").Range("K6").Value = Sheets("Data").Range("J2").Value
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

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