VBA Code to change cell texts based on dropdown list selection

Brendon Donald

New Member
Joined
Apr 27, 2022
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Looking for a code that will change a number of individual cells based on a dropdown list selection.

Example,
Cell A1 dropdown selection is either "miles/chains" or "miles/yard" and I need cells C3, C9, C16 & C30 to change to "ch" if "miles/chains" is selected or to "yds" if "miles/yards" is selected?

Thanks in advance.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Help us to help you. What kind of pulldown is it? A validation list or an actual pulldown control? Are you storing the current selection in a cell? It is best to give that cell a name for easier reference. You should highlight all those cells you want to change and give those a name also. Do you want to trigger the macro when you make a change to the dropdown, or a button?

I named the cell A1 "DistanceChoice" and the 4 cells you want to change "DistanceCells". Add this code to the SHEET module. This code works only if the cell A1 or "DistanceChoice" has changed. The picture below shows the code in Sheet3 because that's where the pulldown is.

1651769484627.png


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
  Dim i As Range
  
  Set i = Intersect(Range("DistanceChoice"), Target)
  If Not i Is Nothing Then
    If i.Value = "" Then Exit Sub
    Application.EnableEvents = False
    
    Select Case i.Value
      Case "Miles/Chains"
        Range("DistanceCells").Value = "ch"
      Case "Miles/Yards"
        Range("DistanceCells").Value = "yds"
    End Select
    
    Application.EnableEvents = True
  End If
  
    
End Sub
 
Upvote 0
Thanks Jeffrey, that works, it was a validation list. Also thanks for pointing me in the direction of naming cells, that will help me out in the future. Thanks,
 
Upvote 0
to make rangechoice dynamic (could be A1, A2 or other cell), and result range could be anywhere in worksheet
in C3, type UDF (User Defined Formula)
=unit($A$1)
Copy C3 to any other result cells.
with source as following:
VBA Code:
Option Explicit
Function Unit(ByVal cell As Range)
    If InStr(6, cell, "ch") Then
        Unit = "ch"
    ElseIf InStr(6, cell, "yard") Then
        Unit = "yds"
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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