Display the result in the active cell in another cell

rvan07

New Member
Joined
Sep 25, 2024
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello and thank you for any help.
The task basically involves two cells. C2 with value 6. C4 with value 9.
Task: When C2 is active, it's value should appear in C4 and C4 will also turn yellow.
When C2 is not active, the value in C4 will be 9 and will not be yellow.
The following code partially works.
It does not allow C4 to have a value when C2 is not active. May need to re-write line 3.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
Range("C4").ClearContents
If Not Intersect(Target, Range("C2")) Is Nothing Then
For Each c In Intersect(Target, Range("C2"))
If c.Address = ActiveCell.Address Then
Range("C4").Value = c.Value
Exit For
End If
Next c
End If
End Sub

Thank you,
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Welcome to the Board!

You have some discrepancies in your explanation (not sure if you want the VBA code to set it to hard-coded values, or the values of other cells, or a combination of both.
In any event, I have documented the code so you should be able to easily follow along and make changes where necessary:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'   If cell C2 is the activecell
    If ActiveCell.Address = "$C$2" Then
'       Set C4 equal to C2
        Range("C4").Value = Range("C2").Value
'       Turn cell yellow
        Range("C4").Interior.Color = 65535
'   If cell C2 is not the activecell
    Else
'       Set C4 equal to 9
        Range("C4").Value = 9
'       Remove color from cell C4
        Range("C4").Interior.Pattern = xlNone
    End If
       
End Sub

I also removed your email address from your initial post.
You should NEVER post your email address in public user forums, as Spambots routinely patrol forums such as these looking for email address to gather and Spam!
 
Upvote 0
Solution
Welcome to the Board!

You have some discrepancies in your explanation (not sure if you want the VBA code to set it to hard-coded values, or the values of other cells, or a combination of both.
In any event, I have documented the code so you should be able to easily follow along and make changes where necessary:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'   If cell C2 is the activecell
    If ActiveCell.Address = "$C$2" Then
'       Set C4 equal to C2
        Range("C4").Value = Range("C2").Value
'       Turn cell yellow
        Range("C4").Interior.Color = 65535
'   If cell C2 is not the activecell
    Else
'       Set C4 equal to 9
        Range("C4").Value = 9
'       Remove color from cell C4
        Range("C4").Interior.Pattern = xlNone
    End If
      
End Sub

I also removed your email address from your initial post.
You should NEVER post your email address in public user forums, as Spambots routinely patrol forums such as these looking for email address to gather and Spam!
Wow! Thankyou Joe4, it works perfectly. Thats totally awesome. And thank you also for removing my e-address
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0
Wow! Thankyou Joe4, it works perfectly. Thats totally awesome. And thank you also for removing my e-address
My intent was to extend the code in my application to incorporate other pairs of cells in the same manner but alas, the solution is not obvious.
Task: On the same worksheet, D3 value is 6. D5 value is 9.
When D3 is active, it's value should appear in D5 and D5 interior should turn yellow.
When D3 is not active, D5 value is 9 and not yellow.
The task would repeat yet again: E4 value is 6. E6 value is 9.
The code allowing this event to work should get me on my way.
In total, my application will require 108 repeats.
Thank you again for any help.
rvan
 
Upvote 0
In total, my application will require 108 repeats.
A good rule of thumb here. NEVER oversimplify your question when posting here. If you do, you may get the correct answer to the question you posted, but it really doesn't solve your full, underlying problem. Unless you feel 100% confident that you can take the answer and extend it out on your own, it is best to post the full nature/extent of your issue.

Is there any "pattern" to this 108 cells you want to apply this to? If not, it is going to be VERY cumbersome, as you are going to need to explicitly have 108 cell references in your code!
 
Upvote 0
Thank you for your support and sound guidance in approaching a problem of this nature.
I am rethinking how best to pose the problem from the perspective of repetitive patterns.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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