+ Reply to Thread
Results 1 to 3 of 3

Mandatory cells if another is filled

Hybrid View

  1. #1
    Registered User
    Join Date
    01-04-2014
    Location
    Florence, KY
    MS-Off Ver
    Excel 2010
    Posts
    1

    Smile Mandatory cells if another is filled

    Hello. I'm new to the site (and to VBA code) and I'm hoping someone can help.

    I found the a code on this site and modified it to match my sheet name and range. It works for the sheet named JAN but I want to include all the other sheets (FEB, MAR, etc). I've tried variations but I can only get it to work for one sheet at a time. Any help is appreciated.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     set rng = Worksheets("JAN").Range("A2:A10")
     for each cell in rng
     if not isempty(cell) and isempty(cell.offset(0,7)) then
     Application.Goto cell.offset(0,7)
     Cancel = True
     msgbox "Reason required!"
     
    End if
     Next
     End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Mandatory cells if another is filled

    Try this...

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim ws As Worksheet, cell As Range
        For Each ws In Sheets(Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"))
            For Each cell In ws.Range("A2:A10")
                If Not IsEmpty(cell) And IsEmpty(cell.Offset(0, 7)) Then
                    Application.Goto cell.Offset(0, 7)
                    Cancel = True
                    MsgBox "Reason required!"
                    Exit Sub
                End If
            Next cell
        Next ws
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Mandatory cells if another is filled

    maybe something like this

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim rng As Range
    Dim sh As Worksheet
    Dim arrMonth As Variant
    Dim i As Long
    
    ' Loop throu all the sheets in the workbook that have the name
    ' in the array
    ' Modify the array to add more months...
    arrMonth = Array("JAN", "FEB", "MAR", "APR")
    
    For i = 0 To UBound(arrMonth)
        Set rng = Worksheets(arrMonth(n)).Range("A2:A10")
        
        ' Loop through everty range in the sheets to see if there is a match.
        For Each cell In rng
            If Not IsEmpty(cell) And IsEmpty(cell.Offset(0, 7)) Then
                cell.Parent.Activate
                cell.Offset(0, 7).Select
                Cancel = True
                MsgBox "Reason required!"
            End If
        Next
    Next i
    
    End Sub
    Thanks
    Last edited by fredlo2010; 01-04-2014 at 10:37 PM.

+ 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] How to make it IMPOSSIBLE TO CLOSE Workbook until mandatory cells r nt filled?
    By Scofield24 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2013, 05:31 AM
  2. Make several cells mandatory if a cell in a range is filled out
    By steeveho in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-18-2013, 06:49 AM
  3. Need Help | VBA (can't close before all mandatory cell are filled up)
    By trajab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2009, 05:52 AM
  4. Replies: 1
    Last Post: 10-11-2009, 04:59 AM
  5. mandatory cells only if another cell is filled
    By RA in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2006, 12:45 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