Multi conditions in Case to trigger cells change

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
I use this code to make cells change but I feel the Case is too chunky. Can anyone help to make it more neatly. Basically the following conditions trigger the change to happen :
Conditions to trigger the event
firstcell value and secondcell value

Cells values
firstcell value = ("E") or ("N")
secondcell value =("D", "D1", "D2", "D3", "D4", "D5", "G", "K")

and

firstcell value =("N")
secondcell value =("E")

VBA Code:
Sub PairedCell3()

Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.ScreenUpdating = False

Dim C As Range, rng As Range, rng1 As Range
Set rng = Range("C3", Range("AL" & Rows.Count).End(xlUp))

With rng.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
End With

'Make Target and Adjacent Cells Change
For Each C In rng
               Case "ED", "ED1", "ED2", "ED3", "ED4", "ED5", "EG", "EK", _
                "ND", "ND1", "ND2", "ND3", "ND4", "ND5", "NE", "NG", "NK"
                    With C.Resize(, 2)
                        .Borders.LineStyle = xlContinuous
                        .Borders.Weight = xlThick
                        .Borders.Color = RGB(102, 0, 255)
                    End With
                End Select
Next C
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I use this code to make cells change but I feel the Case is too chunky.
Does it currently work?
If it works, and the code isnt slow, is it really worth it to try to re-do it differently?
I think to rewrite it would have different complexities of its own, so I am not sure what is to be gained if you already have a working solution.
 
Upvote 0
Quite frankly, if you could simplify it down to just a few "hard-and-fast" rules, you may be able to simplify the code a little (possibly).
But you have different situations, especially as it pertains to the second values (as in some cases, it really is second and third values).
So the fact that your entries are not all the same length make it more complicated (i.e. include "ED" and "ED1" but not "ED6").

So to get rid of that one long "CASE" statement will probably involve having to write a lot more lines of code. So I don't know if I would say if that is an improvement (to make your code even longer).

The "CASE" statement also gives you flexibility, in that it is easy to add or remove more options are any time.
If you tried to do it all on logic, and had some options you wanted to add or remove, it could involve rewriting a chunk of code each time.

So, in my opinion, if the code is working and is not too slow, I would probably opt for what you are doing now, instead of trying to create some longer code trying to incorporate logic to check a myriad of different situations, which need to be checked carefully to account for all exceptions.
 
Upvote 0
it is working.
You say that the code is working but it does not actually compile. For one thing, you have a "Case" statement without a "Select Case" statement.
Could you post your actual working code as I would like to know what that full Select Case statement is to consider if there are viable alternatives.
 
Upvote 0
Hi Peter_SSs,

AgentProposal_Roster0728_0830.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKAL
1MdateAttendance27-Oct28-Oct29-Oct30-Oct31-Oct1-Nov2-Nov3-Nov4-Nov5-Nov6-Nov7-Nov8-Nov9-Nov10-Nov11-Nov12-Nov13-Nov14-Nov15-Nov16-Nov17-Nov18-Nov19-Nov20-Nov21-Nov22-Nov23-Nov24-Nov25-Nov26-Nov27-Nov28-Nov29-Nov30-Nov1-Dec
2DateSummary(5)(4)(3)(2)(1)12345678910111213141516171819202122232425262728293031
3Cat GooT:22 L:0 D:0 E:0 N:0
4Mathew YiuT:22 L:0 D:0 E:0 N:0ED1
5Roy LaiT:22 L:0 D:1 E:0 N:1NG
6Jonathan LaiT:22 L:0 D:0 E:0 N:0
7Vitus LaiT:22 L:0 D:0 E:1 N:1EN
8Vincent SzetoT:22 L:0 D:0 E:0 N:0
9Jason LiT:22 L:0 D:0 E:0 N:0
202111
Cells with Data Validation
CellAllowCriteria
H3:AL9List=ShiftcodeNew
A3:A9List=HelpAgent

VBA Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.ScreenUpdating = False


Dim C As Range, rng As Range, rng1 As Range
Dim cArray As Variant

Set rng = Range("C3", Range("AL" & Rows.Count).End(xlUp))

With rng.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
End With

For Each C In rng
           Select Case UCase(C.Value) & UCase(C.Offset(0, 1).Value)
                Case "ED", "ED1", "ED2", "ED3", "ED4", "ED5", "EG", "EK", _
                "ND", "ND1", "ND2", "ND3", "ND4", "ND5", "NE", "NG", "NK"
                    With C.Resize(, 2)
                        .Borders.LineStyle = xlContinuous
                        .Borders.Weight = xlThick
                        .Borders.Color = RGB(102, 0, 255)
                    End With
            End Select
Next C
      

Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.ScreenUpdating = True



End Sub
 
Upvote 0
Thanks for the code and sample data.

I see this is actually a Sub Workbook_SheetCalculate(ByVal Sh As Object) event code yet nowhere in the code does it use the "Sh" object.

Why do you have this in Wokbook_SheetCalculate code and not simply Worksheet_Calculate code for the particular sheet? Do you have multiple sheets in this workbook for which you wish to run this same code?

Assuming that this is to operate on a single sheet, you could, in a copy of your workbook, remove the above code and try this code in the particular worksheet's module, rather than the ThisWorkbook module.

I don't see it as much less 'chunky' or as 'better' code than what you had. Further, I doubt most users would find it as easy to maintain/edit if changes were required. Still, the choice is yours, if this does the job for you.

VBA Code:
Private Sub Worksheet_Calculate()
  Application.DisplayStatusBar = False
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  
  Dim C As Range, rng As Range
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(^[EN]#((D[1-5]?)|G|K)$)|(^N#E$)"
  
  Set rng = Range("C3", Range("AL" & Rows.Count).End(xlUp))
  With rng.Borders
      .LineStyle = xlContinuous
      .Weight = xlThin
  End With

  For Each C In rng
    If RX.Test(UCase(C.Value) & "#" & UCase(C.Offset(0, 1).Value)) Then
      With C.Resize(, 2).Borders
        .Weight = xlThick
        .Color = RGB(102, 0, 255)
      End With
    End If
  Next C
  
  Application.DisplayStatusBar = True
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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