Source Formatting from a Data Validation

CantExcel123

New Member
Joined
Mar 28, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I've been messing around with this for a couple days and cant figure it out. Please help.

I have a spreadsheet where I have a dropdown list (data validation) that (on sheet1) references a list of names on sheet2. I would like to keep the source formatting (i.e. yellow fill, or red text, etc) from the list of names on sheet2 when I select a name on sheet1. I've tried a couple flavors of VBA that weren't exactly my setup, mor complicated if anything, (by changing the range of cells, etc) with no luck.

Please help.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
@CantExcel123 Welcome.
Try the below in the input sheet.
It is not overly sophistocated in as much as the dv sheet name, dv list range and input range need to be hard coded, (to be edited by you).
However, if you only need this functionality for the names it may suffice.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vws As Worksheet
Dim vrng As Range
Dim r As Long

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A2:A2000")) Is Nothing Then Exit Sub   '<<<<< edit range to be your input range of interest
''Otherwise proceed
Application.EnableEvents = False

Set vws = Sheets("Sheet2")  '****  validation list sheet - edit to suit
Set vrng = vws.Range("$A$2:$A$20")  '<<<<< Validation List address  - edit to suit

r = Application.WorksheetFunction.Match(Target, vrng, 0)   'row in dv list
'set same interior and font colours
Target.Interior.Color = vrng.Cells(r, 1).Interior.Color
Target.Font.Color = vrng.Cells(r, 1).Font.Color

  Application.EnableEvents = True
End Sub

Hope that helps.
 
Upvote 0
@CantExcel123 Welcome.
Try the below in the input sheet.
It is not overly sophistocated in as much as the dv sheet name, dv list range and input range need to be hard coded, (to be edited by you).
However, if you only need this functionality for the names it may suffice.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vws As Worksheet
Dim vrng As Range
Dim r As Long

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A2:A2000")) Is Nothing Then Exit Sub   '<<<<< edit range to be your input range of interest
''Otherwise proceed
Application.EnableEvents = False

Set vws = Sheets("Sheet2")  '****  validation list sheet - edit to suit
Set vrng = vws.Range("$A$2:$A$20")  '<<<<< Validation List address  - edit to suit

r = Application.WorksheetFunction.Match(Target, vrng, 0)   'row in dv list
'set same interior and font colours
Target.Interior.Color = vrng.Cells(r, 1).Interior.Color
Target.Font.Color = vrng.Cells(r, 1).Font.Color

  Application.EnableEvents = True
End Sub

Hope that helps.
Trying this now. Thank you...
 
Upvote 0
Seems to be working. I was even able to add Target.Font.Bold = vrng.Cells(r, 1).Font.Bold so it would automatically format that also.

Thank you very much sir.
 
Upvote 0
One Question - when I try and clear data from a (dropdown list) cell, be it 'clear contents' or backspace it pops an error. Is there a way to stop that ?

Runtime error 1004
Unable to match property of the Worksheetfucntion class
 
Upvote 0
Hi, does the below sort it?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vws As Worksheet
Dim vrng As Range
Dim r As Long

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A2:A20")) Is Nothing Then Exit Sub   '<<<<< edit range to be your input range of interest
''Otherwise proceed
Application.EnableEvents = False

Set vws = Sheets("Sheet2")  '****  validation list sheet - edit to suit
Set vrng = vws.Range("$A$2:$A$20")  '<<<<< Validation List address  - edit to suit

On Error Resume Next  'allows for clearing a single cell
r = Application.WorksheetFunction.Match(Target, vrng, 0)   'row in dv list
'set same interior and font colours
Target.Interior.Color = vrng.Cells(r, 1).Interior.Color
Target.Font.Color = vrng.Cells(r, 1).Font.Color
On Error GoTo 0
  Application.EnableEvents = True
End Sub
 
Upvote 0
Hi, does the below sort it?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vws As Worksheet
Dim vrng As Range
Dim r As Long

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A2:A20")) Is Nothing Then Exit Sub   '<<<<< edit range to be your input range of interest
''Otherwise proceed
Application.EnableEvents = False

Set vws = Sheets("Sheet2")  '****  validation list sheet - edit to suit
Set vrng = vws.Range("$A$2:$A$20")  '<<<<< Validation List address  - edit to suit

On Error Resume Next  'allows for clearing a single cell
r = Application.WorksheetFunction.Match(Target, vrng, 0)   'row in dv list
'set same interior and font colours
Target.Interior.Color = vrng.Cells(r, 1).Interior.Color
Target.Font.Color = vrng.Cells(r, 1).Font.Color
On Error GoTo 0
  Application.EnableEvents = True
End Sub
No error when using this code, but does not pull formatting from list...
 
Upvote 0
@CantExcel123

This is probably a bit better as it will reestablish cleared cells with gridline.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vws As Worksheet
Dim vrng As Range
Dim r As Long

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A2:A20")) Is Nothing Then Exit Sub   '<<<<< edit range to be your input range of interest
''Otherwise proceed
Application.EnableEvents = False

Set vws = Sheets("Sheet2")  '****  validation list sheet - edit to suit
Set vrng = vws.Range("$A$2:$A$20")  '<<<<< Validation List address  - edit to suit

On Error Resume Next  'alows single cell clearing by ignoring any error setting r
r = Application.WorksheetFunction.Match(Target, vrng, 0)   'row in dv list

If Not Err.Number = 0 Then 'there is an error setting r due to clearing
    ' so set target back to default
    Target.Interior.Pattern = xlNone
Else
    'otherwise set as per the dv pick
    Target.Interior.Color = vrng.Cells(r, 1).Interior.Color
    Target.Font.Color = vrng.Cells(r, 1).Font.Color
    Target.Font.Bold = vrng.Cells(r, 1).Font.Bold
End If

On Error GoTo 0    'reset error handling to default
Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Brilliant! If you would mark the post as answered that would be great.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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