+ Reply to Thread
Results 1 to 8 of 8

How to prevent compilation of a field on condition based

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2017
    Location
    Rome
    MS-Off Ver
    OFFICE 2006
    Posts
    4

    Unhappy How to prevent compilation of a field on condition based

    Hi everybody,

    I try to write the following VBA code in order to prevent the compilation of the fields "Imponibile" and "IVA" when "TipoDocumentazione"= Ricevuta (see attached image). Immagine.png

    I have two types of problem:
    1) the fields "Imponibile" and "IVA" can still be compiled
    2) doing modification on other fields on excel file, the "non corresponding types - runtime error" appears

    Where I am wrong?

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim WSpese As Worksheet
        Dim Row As Long
        Dim strRicevuta As String
        Dim strFattura As String
        
        
        
        Set WSpese = Target.Worksheet
        strRicevuta = "Ricevuta"
        strFattura = "Fattura"
        
        Row = Target.Row
        
        
           
        If (Target.Column = 7 And Target.Value = strRicevuta) Then
            
            WSpese.Cells(Row, 8).Locked = True
            WSpese.Cells(Row, 9).Locked = True
        
        ElseIf (Target.Column = 7 And Target.Value = strFattura) Then
               
            Row = Target.Row
            WSpese.Cells(Row, 8).Locked = False
            WSpese.Cells(Row, 9).Locked = False
        
        Else
            
            
        End If
        
    End Sub

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

    Re: How to prevent compilation of a field on condition based

    Try this. If your sheet is protected, uncomment the two green lines and change the password to suit.

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Target.Count = 1 Then
            If Target.Column = 7 Then
                'Me.Unprotect Password:="Secret"
                
                If Target.Value = "Ricevuta" Then
                    Me.Cells(Target.Row, 8).Locked = True
                    Me.Cells(Target.Row, 9).Locked = True
                
                ElseIf Target.Value = "Fattura" Then
                    Me.Cells(Target.Row, 8).Locked = False
                    Me.Cells(Target.Row, 9).Locked = False
                    
                End If
                
                'Me.Protect Password:="Secret"
            End If
        End If
    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
    Registered User
    Join Date
    05-26-2017
    Location
    Rome
    MS-Off Ver
    OFFICE 2006
    Posts
    4

    Re: How to prevent compilation of a field on condition based

    Same behaviour...

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to prevent compilation of a field on condition based

    Locking a cell has no effect unless and until a worksheet is Protected. The Lock property determines what happens to that cell when the sheet is protected. It does nothing until the sheet is protected.

    This approach might work for you.
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Target
            If .Cells.Count = 1 And .Column = 7 Then
                If LCase(.Value) = "ricevuta" Then
                    With .EntireRow.Range("H1:I1")
                        Application.EnableEvents = False
                        .ClearContents
                        With .Validation
                            .Delete
                            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
                            xlBetween, Formula1:="=false"
                            .IgnoreBlank = True
                            .InCellDropdown = True
                            .ErrorMessage = "'Ricevuta' forbids data entry to this cell"
                            .ShowInput = False
                            .ShowError = True
                        End With
                        Application.EnableEvents = True
                    End With
                End If
            End If
        End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    05-26-2017
    Location
    Rome
    MS-Off Ver
    OFFICE 2006
    Posts
    4

    Re: How to prevent compilation of a field on condition based

    It seems it works only in some situations.. (it make not sense, I know..).. see attached imageImmagine.png

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to prevent compilation of a field on condition based

    duplicate error

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to prevent compilation of a field on condition based

    I thought that if the cell in column G said "Ricevuta" you wanted the cells in uncompleted, i.e. empty.

  8. #8
    Registered User
    Join Date
    05-26-2017
    Location
    Rome
    MS-Off Ver
    OFFICE 2006
    Posts
    4

    Re: How to prevent compilation of a field on condition based

    Quote Originally Posted by mikerickson View Post
    I thought that if the cell in column G said "Ricevuta" you wanted the cells in uncompleted, i.e. empty.
    yes, I want that.

    It seems there is something wrong in the code because actually I can fill the "Imponibile" and "IVA" fields; I attach the code
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Target
            If .Cells.Count = 1 And .Column = 7 Then
                If LCase(.Value) = "Ricevuta" Then
                    With .EntireRow.Range("H1:I1")
                        Application.EnableEvents = False
                        .ClearContents
                        With .Validation
                            .Delete
                            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
                            xlBetween, Formula1:="=false"
                            .IgnoreBlank = True
                            .InCellDropdown = True
                            .ErrorMessage = "Il tipo'Ricevuta' impedisce l'introduzione di questo dato (si veda legenda)"
                            .ShowInput = False
                            .ShowError = True
                        End With
                        Application.EnableEvents = True
                    End With
                End If
            End If
        End With
    End Sub
    and the table screenshot
    Immagine.png

+ 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] Prevent user from closing file until condition is met
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2017, 02:28 AM
  2. Replies: 0
    Last Post: 06-26-2012, 09:06 PM
  3. Condition format prevent no is repeat
    By choy96 in forum Excel General
    Replies: 2
    Last Post: 12-25-2011, 12:19 PM
  4. Select worksheet based on condition & populate cell based on condition
    By beth1069 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2011, 01:27 PM
  5. Defining a Text field based on its condition
    By C.Hirsch in forum Excel General
    Replies: 3
    Last Post: 05-30-2006, 02:55 PM
  6. [SOLVED] Conditional Compilation Based On Excel Version
    By Josh Sale in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2005, 05:05 PM
  7. prevent event procedure under condition
    By short_n_curly in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2005, 04:05 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