Filling shape/cell with text dependent on which cell is highlighted

Cave_Johnson

New Member
Joined
Jan 20, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

What I'm trying to do is fill a shape or cell (whichever is easiest to code!) with specific text depending on which cell is selected. The text is located on a different sheet in the same workbook. I'd also like a default text to be in there if any other cell is selected.

It'll end up being a lot more detailed than this (I think there will be 20+ options), but if I can get the grammar right then I should be able to fill out the rest.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Range("B4:K4"), Target) Is Nothing Then
Sheets("Definitions").Range("B2").Copy
Sheets("Target").Activate
Range("M10"). Select
ActiveSheet.Paste
Application.CutCopyMode = False

ElseIf Not Intersect(Range("B5:K5"), Target) Is Nothing Then
Sheets("Definitions").Range("C2").Copy
Sheets("Target").Activate
Range("M10"). Select
ActiveSheet.Paste
Application.CutCopyMode = False

Else 
Sheets("Definitions").Range("A2").Copy
Sheets("Target").Activate
Range("M10"). Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub

What's wrong with that!?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
What sheet is the code in?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   If Not Intersect(Range("B4:K4"), Target) Is Nothing Then
      Range("M10").Value = Sheets("Definitions").Range("B2").Value
   ElseIf Not Intersect(Range("B5:K5"), Target) Is Nothing Then
      Range("M10").Value = Sheets("Definitions").Range("C2").Value
   Else
      Range("M10").Value = Sheets("Definitions").Range("A2").Value
   End If
End Sub
 
Upvote 0
Solution
I think that is great :D Thanks! Will fill in all the details now.

One last question - is there a simple way I can get it to copy the formatting as well?
 
Upvote 0
Yup, just use it like
VBA Code:
      Sheets("Definitions").Range("B2").Copy Range("M10")
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

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