+ Reply to Thread
Results 1 to 12 of 12

Shorten the Code - Use CASE ?

  1. #1
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Shorten the Code - Use CASE ?

    Hello

    What would be a smarter way to shorten the below code please ?

    Kind regards
    Vivek

    Please Login or Register  to view this content.
    Last edited by captvsharma; 03-25-2022 at 04:28 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Shorten the Code - Use CASE ?

    Do you mean something like this (not tested)?

    Please Login or Register  to view this content.
    <<< If you have valued anyone's contributions in this thread, please click * to thank them for their efforts

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Shorten the Code - Use CASE ?

    A little shorter

    Please Login or Register  to view this content.
    Only check in D4:D400 once, then check for text.
    The code is not case sensitive "on site" and "On Site" will both cause the change.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Shorten the Code - Use CASE ?

    good spot, hadn't picked up the different .TintAndShade parameters

  5. #5
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Thanks and gives the idea

  6. #6
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Andy , very clear and extremely succinct.

    How can I build in multiple criteria in the same code ? e.g. If "ON SITE" is on Saturday or Sunday ( Date is Column C) than remove the TintandShade ?

  7. #7
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Shorten the Code - Use CASE ?

    you could nest the case, or put simple IF...THEN statements on individual lines (depending on how complex they are)

  8. #8
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Hello

    Please help with building a nested CASE statement. If Column C (date) is a weekend i.e. Saturday or Sunday then the Tint for "On Site Work" should be removed. How can I achieve that in the code please ?
    [CODE]
    Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target(1), [D4:D400]) Is Nothing Then
    Select Case UCase(Target(1).Text)
    Case "ON SITE WORK", "TRAVEL TO/FROM", "NO AUDIT WORK"
    Application.EnableEvents = False
    With Rows(Target(1).Row).Columns("E:AB").Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.499984740745262
    .PatternTintAndShade = 0
    End With
    Application.EnableEvents = True
    Case "TRAVEL WITHIN", "VIRTUAL WORK"
    Application.EnableEvents = False
    With Rows(Target(1).Row).Columns("E:AB").Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    Application.EnableEvents = True
    Case Else
    Application.EnableEvents = False
    With Rows(Target(1).Row).Columns("E:AB").Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    Application.EnableEvents = True
    End Select
    End If

    End Sub
    Last edited by captvsharma; 03-24-2022 at 08:09 AM.

  9. #9
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Attaching the Excel sheet for understanding.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Thanks Sir. Please help with building a nested CASE statement. If Column C (date) is a weekend i.e. Saturday or Sunday then the Tint for "On Site Work" should be removed. How can I achieve that in the code, please ? Excel spreadsheet attached if it helps.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Shorten the Code - Use CASE ?

    add a check of weekday

    Please Login or Register  to view this content.
    In the weekday function I used 2 so the values of Saturday and Sunday were 6 and 7. This makes the conditional test easier.

  12. #12
    Registered User
    Join Date
    08-19-2020
    Location
    London
    MS-Off Ver
    365
    Posts
    22

    Re: Shorten the Code - Use CASE ?

    Andy - wow ! What a great code and thanks a lot. You are awesome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Shorten VBA code
    By DiCaver in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2018, 05:40 AM
  2. shorten code vba
    By wildonln in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2016, 03:50 PM
  3. [SOLVED] Represent (define?) a section of code with a variable (shorten long code lines)?
    By Gene@action in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2016, 03:59 PM
  4. Hi all, can you please help me to shorten this code as much as possible
    By boddulus in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-31-2014, 07:31 AM
  5. [SOLVED] Very inefficient code because of different sizes of arrays, how do i shorten my code?
    By Brammer88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 04:49 PM
  6. [SOLVED] Disregard case in VBA code. (UCase, LCase, Select Case)
    By Orestees in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-07-2012, 12:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1