Please can you advise on how to simplify this code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,251
Office Version
  1. 2007
Platform
  1. Windows
Hi,

As per title.
Thanks

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim cel As Range
Dim ns As Variant
Set ws = Sheets("MC LIST")
For Each cel In ws.Range("B8", ws.Range("B" & ws.Rows.Count).End(xlUp))
If Len(cel) > 9 Then cel.Characters(Start:=10, Length:=1).Font.ColorIndex = 3
Next cel

    If Target.Cells.Count > 1 Then Exit Sub
    
    Application.ScreenUpdating = False

Application.EnableEvents = False
Select Case Mid(Range("B8").Value, 10, 1)
Case Is = "X"
        Range("I8").Value = "1999"
Case Is = "Y"
        Range("I8").Value = "2000"
Case Is = "1"
        Range("I8").Value = "2001"
Case Is = "2"
        Range("I8").Value = "2002"
Case Is = "3"
        Range("I8").Value = "2003"
Case Is = "4"
        Range("I8").Value = "2004"
Case Is = "5"
        Range("I8").Value = "2005"
Case Is = "6"
        Range("I8").Value = "2006"
Case Is = "7"
        Range("I8").Value = "2007"
Case Is = "8"
        Range("I8").Value = "2008"
Case Is = "9"
        Range("I8").Value = "2009"
Case Is = "A"
        Range("I8").Value = "2010"
Case Is = "B"
        Range("I8").Value = "2011"
Case Is = "C"
        Range("I8").Value = "2012"
Case Is = "D"
        Range("I8").Value = "2013"
Case Is = "E"
        Range("I8").Value = "2014"
Case Is = "F"
        Range("I8").Value = "2015"
Case Is = "G"
        Range("I8").Value = "2016"
Case Is = "H"
        Range("I8").Value = "2017"
Case Is = "J"
        Range("I8").Value = "2018"
Case Is = "K"
        Range("I8").Value = "2019"
End Select

    Application.EnableEvents = True


End Sub
 
Thanks to you all,
I will look at these and see which to use.

Have a nice day
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I will look at these and see which to use.
Ultimately that is your choice, we might be able to offer better advice if you told us what you were trying to do, you asked us how you could simplify the code, but it was already simple.

@Yongle and @GWteB have used some alternative methods to make the code more compact.
In my first reply, I kept your original (simple) code, doing nothing more than removing a few lines that appeared to be unnecessary.
In post 9, I've shortened the code from my first reply by using @GWteB's idea to replace the case selection, it is a great way to reduce the amount of code, but it is only simplifying the code if you understand it so that you can change it when you need to.
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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