+ Reply to Thread
Results 1 to 14 of 14

mandatory cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    mandatory cells

    Hello to all.
    This macro:
    Option Explicit
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim cella As Range
    Application.EnableEvents = False
    
    
    Set cella = Cells(Target.Row, 1) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    'MsgBox "Devi inserire una data nella cella  A" & Target.Row & " ", vbCritical, "Errore!"
    MsgBox "You must enter a date in cell A" & Target.Row & " ", vbCritical, "Errore!"
    
    End If
    
    End With
    
    
    Application.EnableEvents = True
    End Sub
    you must enter a date in cell A mandatory
    Now I added mandatory too column B / C / D but the warning appears 3 times, but it should only appear in the selected cell.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim cella As Range
    Application.EnableEvents = False
    
    'Set cella = Range("A3") '<<< cella singola
    'Set cella = Cells(Target.Row, Target.Column) '<<< colonna
    
    Set cella = Cells(Target.Row, 1) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    'MsgBox "Devi inserire una data nella cella  A" & Target.Row & " ", vbCritical, "Errore!"
    MsgBox "You must enter a date in cell A" & Target.Row & " ", vbCritical, "Errore!"
    
    End If
    
    End With
    
    
    Set cella = Cells(Target.Row, 2) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire il reparto nella cella  B" & Target.Row & " ", vbCritical, "Errore!"
    
    End If
    
    End With
    
    
    
    Set cella = Cells(Target.Row, 3) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire l'operatore nella cella  C" & Target.Row & " ", vbCritical, "Errore!"
    
    End If
    
    End With
    
    
    
    Set cella = Cells(Target.Row, 4) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire il tempo nella cella  D" & Target.Row & " ", vbCritical, "Errore!"
    
    End If
    
    End With
    
    
    
    Application.EnableEvents = True
    End Sub
    you can edit?
    xam
    Attached Files Attached Files
    Last edited by xam99; 01-12-2020 at 04:47 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: mandatory cells

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim cella As Range
    Application.EnableEvents = False

    'Set cella = Range("A3") '<<< cella singola
    'Set cella = Cells(Target.Row, Target.Column) '<<< colonna

    Set cella = Cells(Target.Row, 1) '<<< cella singola

    With cella
    If .Value = "" Then

    'MsgBox "Devi inserire una data nella cella A" & Target.Row & " ", vbCritical, "Errore!"
    MsgBox "You must enter a date in cell A" & Target.Row & " ", vbCritical, "Errore!"

    End If

    End With
    ' ...... etc

    End Sub
    Why not just exit the sub after the first failed test?

    e.g.
    With cella
    If .Value = "" Then
    
    'MsgBox "Devi inserire una data nella cella  A" & Target.Row & " ", vbCritical, "Errore!"
    MsgBox "You must enter a date in cell A" & Target.Row & " ", vbCritical, "Errore!"
    cella.select
    exit sub
    
    End If
    HTH
    Tim


    EDIT
    Remember to re-enable events!

     
    Cella.Select
    Application.EnableEvents = TRUE
    Exit Sub
    Last edited by harrisonland; 01-12-2020 at 05:50 AM.

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,266

    Re: mandatory cells

    For starters. This one will only give 1 messagebox when data is missing in one of the columns.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Application.CountA(Cells(Target.Row, 1).Resize(, 4)) <> 4 Then MsgBox "Missing data on Row " & Target.Row
    
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Thanks bakerman2 but the message must be referred to the cell to be inserted

  5. #5
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Hi harrisonland your modified:
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim cella As Range
    Application.EnableEvents = False
    
    'Set cella = Range("A3") '<<< cella singola
    'Set cella = Cells(Target.Row, Target.Column) '<<< colonna
    
    Set cella = Cells(Target.Row, 1) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    'MsgBox "Devi inserire una data nella cella  A" & Target.Row & " ", vbCritical, "Errore!"
    MsgBox "You must enter a date in cell A" & Target.Row & " ", vbCritical, "Errore!"
    cella.Select
    Application.EnableEvents = True
    Exit Sub
    End If
    
    End With
    
    
    Set cella = Cells(Target.Row, 2) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire il reparto nella cella  B" & Target.Row & " ", vbCritical, "Errore!"
    cella.Select
    Application.EnableEvents = True
    Exit Sub
    End If
    
    End With
    
    
    
    Set cella = Cells(Target.Row, 3) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire l'operatore nella cella  C" & Target.Row & " ", vbCritical, "Errore!"
    cella.Select
    Application.EnableEvents = True
    Exit Sub
    End If
    
    End With
    
    
    
    Set cella = Cells(Target.Row, 4) '<<< cella singola
    
    With cella
    If .Value = "" Then
    
    MsgBox "Devi inserire il tempo nella cella  D" & Target.Row & " ", vbCritical, "Errore!"
    cella.Select
    Application.EnableEvents = True
    Exit Sub
    End If
    
    End With
    
    
    
    Application.EnableEvents = True
    End Sub
    work well.
    Thans you
    xam

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,266

    Re: mandatory cells

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim i As Long
        For i = 1 To 4
            If Cells(Target.Row, i) = vbNullString Then
                Application.EnableEvents = False
                MsgBox "Devi inserire il reparto nella cella " & Chr(i + 64) & Target.Row & " ", vbCritical, "Errore!"
                Application.Goto Cells(Target.Row, i)
                Application.EnableEvents = True
                Exit Sub
            End If
        Next
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: mandatory cells

    Haha! Bakerman2's solution is far more elegant (as usual), why not tweak it a bit to make it flexible:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim i As Long
        For i = 1 To Target.Cells.count
            If Cells(Target.Row, i) = vbNullString Then
                Application.EnableEvents = False
                MsgBox "Devi inserire il reparto nella cella " & Chr(i + 64) & Target.Row & " ", vbCritical, "Errore!"
                Application.Goto Cells(Target.Row, i)
                Application.EnableEvents = True
                Exit Sub
            End If
        Next
    End Sub
    Tim
    Never stop learning!
    <--- please consider *-ing !

  8. #8
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Thanks bakerman2 and harrisonland
    the warning must be referred to the cell, see my first thread
    data / reparto / operatore / tempo

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,266

    Re: mandatory cells

    Didn't notice there was always a different message.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim i As Long, myarr
        myarr = Array(" una data ", " il reparto ", " l'operatore ", " il tempo ")
        For i = 1 To 4
            If Cells(Target.Row, i) = vbNullString Then
                Application.EnableEvents = False
                MsgBox "Devi inserire" & myarr(i - 1) & "nella cella " & Chr(i + 64) & Target.Row & " ", vbCritical, "Errore!"
                Application.Goto Cells(Target.Row, i)
                Application.EnableEvents = True
                Exit Sub
            End If
        Next
    End Sub

  10. #10
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Hi bakerman2 the new modified work well.
    i tried moving the required cells now are:
    A / D and E / F but as I did it doesn't work
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim i1 As Long, myarr1
        Dim i2 As Long, myarr2
        
        'myarr1 = Array(" una data ", " il reparto ", " l'operatore ", " il tempo ")
        myarr1 = Array(" una data ", " il reparto ")
        
        For i1 = 1 To 2 '<<< column A/B
            If Cells(Target.Row, i1) = vbNullString Then
                Application.EnableEvents = False
                MsgBox "Devi inserire" & myarr1(i1 - 1) & "nella cella " & Chr(i1 + 64) & Target.Row & " ", vbCritical, "Errore!"
                Application.Goto Cells(Target.Row, i1)
                Application.EnableEvents = True
                Exit Sub
            End If
        Next
        
        
       myarr2 = Array(" l'operatore ", " il tempo ")
       
        For i2 = 4 To 6 '<<< column E/F
            If Cells(Target.Row, i2) = vbNullString Then
                Application.EnableEvents = False
                MsgBox "Devi inserire" & myarr2(i2 - 1) & "nella cella " & Chr(i2 + 64) & Target.Row & " ", vbCritical, "Errore!"
                Application.Goto Cells(Target.Row, i2)
                Application.EnableEvents = True
                Exit Sub
            End If
        Next
        
    End Sub

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,266

    Re: mandatory cells

    Post an example file with the new layout.

  12. #12
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Here is an example.
    Another thing must start from row 3

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,266

    Re: mandatory cells

    Try this then.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim i As Long, myarr, x
        
        If Target.Row < 3 Then Exit Sub
        
        If Target.Column <> 3 And Target.Column <> 4 Then
        
            myarr = Array(" una data ", " il reparto ", " l'operatore ", " il tempo ")
            
            x = Split("1,2,5,6", ",")
            
            For i = 0 To 3
                If Cells(Target.Row, CInt(x(i))) = vbNullString Then
                    Application.EnableEvents = False
                    MsgBox "Devi inserire" & myarr(i) & "nella cella " & Chr(IIf(i < 2, i, i + 2) + 65) & Target.Row & " ", vbCritical, "Errore!"
                    Application.Goto Cells(Target.Row, IIf(i < 2, i + 1, i + 3))
                    Application.EnableEvents = True
                    Exit For
                End If
            Next
        End If
    End Sub

  14. #14
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    546

    Re: mandatory cells

    Thanks bakerman2 I think it is correct now.
    To be sure I have to try the macro in the office where I work.
    A greeting and thanks again.
    xam

+ 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. Mandatory Cells
    By Rich789 in forum Excel General
    Replies: 1
    Last Post: 08-24-2017, 08:33 AM
  2. VBA - mandatory cells in excel
    By bees2cute in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2016, 09:02 AM
  3. How can I make multiple cells mandatory if new rows are used with some cells left blank?
    By markbarnett in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2015, 07:06 AM
  4. Mandatory field required message when user skips mandatory fields
    By Bharathi27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2013, 05:12 AM
  5. Mandatory Cells
    By MYQ in forum Excel General
    Replies: 7
    Last Post: 10-12-2012, 02:16 PM
  6. Mandatory cells
    By john.mk in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-25-2010, 11:03 PM
  7. Mandatory Cells
    By Mel1221 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-05-2006, 05:10 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