Macro Question/Support

scott_86_

New Member
Joined
Sep 27, 2018
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi,

On a worksheet I have 3 seperate ranges (J9:J28, U9:U28, J36:J55 = total 60 cells) where users can select from a drop down box either Y or N to poulate an indivudual cell.

I would like to have a limit of 30 Y's within the 60 cells. If someone would try and enter another Y after 30, this would not be permitted and a MsgBox would also come up with text.

Needs to be aplied to all worksheets which are all identical.

This workbook already runs some macros and if someone could help me out with this that'd be great!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

Try this, it will need to be added to the sheets (not in a module)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    Dim KeyCells As Range
    Dim myrange As Range
    Dim myrange2 As Range
    Dim myrange3 As Range
    
    Set myrange = Range("J9:J28")
    Set myrange2 = Range("U9:U28")
    Set myrange3 = Range("J36:J55")
    Set KeyCells = Range("J9:U55")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

    If Application.CountIf(myrange, "Y") + Application.CountIf(myrange2, "Y") + Application.CountIf(myrange3, "Y") > 30 Then
    With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
    End With
    MsgBox "Limit Exceeded"
    End If

    End If
    
End Sub
 
Last edited:
Upvote 0
Hi,

Try this, it will need to be added to the sheets (not in a module)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    Dim KeyCells As Range
    Dim myrange As Range
    Dim myrange2 As Range
    Dim myrange3 As Range
    
    Set myrange = Range("J9:J28")
    Set myrange2 = Range("U9:U28")
    Set myrange3 = Range("J36:J55")
    Set KeyCells = Range("J9:U55")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

    If Application.CountIf(myrange, "Y") + Application.CountIf(myrange2, "Y") + Application.CountIf(myrange3, "Y") > 30 Then
    With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
    End With
    MsgBox "Limit Exceeded"
    End If

    End If
    
End Sub

Hi mlrshl,

Thank you.

That worked fine when I applied in to an individual worksheet.

Is there any chance you could tweak it so I can put the code into 'ThisWorkbook' so it applies to all worksheets?

Thanks
 
Upvote 0
Simply copy the code & pout it into a
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
event, in the ThisWorkbook module
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

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