Using Len and Mid For condition Formatting - VBA

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi,


I have the code
Code:
If Len((Range("I10")) = 3 And Mid((Range("I10")), 3, 1) > 5 Then
(Range("I10").Interior.ColorIndex = 3

ElseIf Len(Range("I11")) = 4 And Mid((Range("I11")), 3, 2) > 5 Then
Range("I11").Interior.ColorIndex = 3

ElseIf Len(Range("I12")) = 5 And Mid((Range("I12")), 3, 3) > 5 Then
Range("I12").Interior.ColorIndex = 3

End If

which formats the cells based on the condition stated . However I want to use a Private Sub so it changes when the instrg is entered in a cell. I also want this for a set of range and not just one cell. I.E Range ("I10:AA1000").

Thanks in advanced for your help !
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Sounds like you want a Worksheet_Change event procedure code, which is VBA code that automatically runs as data is manually entered into a specific range.
Here is code that will do that for you, but it must be placed in the correct module.
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in the resulting window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Target, Range("I10:AA1000"))
    
    If Not rng Is Nothing Then
        For Each cell In rng
            Select Case Len(cell)
                Case 3, 4, 5
                    If Mid(cell, 3, Len(cell) - 2) > 5 Then cell.Interior.ColorIndex = 3
            End Select
        Next cell
    End If

End Sub
For more on event procedure code, see here: http://www.cpearson.com/excel/events.aspx
 
Upvote 0
Perfect !!
This does the job !

Much Appreciated and will look more into event procedure it does really come in handy as don't need to create a command button all the time and the code could run itself !
 
Upvote 0
You are welcome!

Yes, event procedures are very handy. And on "manual change" isn't the only one. You can trigger things to run on opening the file, saving file, selecting certain cells, as well as many others.
 
Upvote 0

Forum statistics

Threads
1,223,953
Messages
6,175,598
Members
452,658
Latest member
GStorm

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