+ Reply to Thread
Results 1 to 6 of 6

Restrict OpenEvent code to one column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-23-2015
    Location
    UK
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    120

    Restrict OpenEvent code to one column

    Can I amend the following code such that ONLY when a cell in column B is selected,
    formatting that cell is permitted ?


    Private Sub Workbook_Open()
        Sheets("Sheet1").Protect Password:="123", _
            DrawingObjects:=True, Contents:=True, _
            Scenarios:=True, AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, AllowFormattingRows:=True, _
            AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True, UserInterfaceOnly:=True
    End Sub
    I particularly wish to allow changes to:
    font colour, fill colour, italic, bold and underline.

    Thanks.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Restrict OpenEvent code to one column

    Hi there,

    Try inserting the following code into the VBA CodeModule of the worksheet you wish to edit, and see if it does what you want:

    
    
    Option Explicit
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Const iENABLED_COLUMN_NO    As Integer = 2
        Const sTYPE_RANGE           As String = "Range"
        Const sPASSWORD             As String = "123"
    
        If TypeName(Selection) = sTYPE_RANGE Then
    
            If Selection.Columns.Count = 1 Then
    
                If Selection.Column = iENABLED_COLUMN_NO Then
                      Me.Unprotect Password:=sPASSWORD
                Else: Me.Protect Password:=sPASSWORD
                End If
    
            End If
    
        End If
    
    End Sub
    The highlighted value corresponds to Column B - this can be altered to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    04-23-2015
    Location
    UK
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    120

    Re: Restrict OpenEvent code to one column

    Hi Greg, thanks.

    I had thought of something like that, but worried about handing control back to the sheet with protection removed.

    I guess now they'll be able to do stuff as long as they don't select another cell ?

    Is there a way (after your code runs) to limit their freedom with the selected cell
    to just:
    font colour, fill colour, italic, bold and underline ?

    Many thanks.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Restrict OpenEvent code to one column

    Hi again,

    Take a look at the attached workbook - it allows only formatting of the font characteristics of cells in Column B.

    The following code is inserted in a standard VBA CodeModule:

    
    
    Option Explicit
    
    
    Sub EditCells()
    
        Const iENABLED_COLUMN_NO    As Integer = 2
        Const sSHEET_NAME           As String = "Sheet1"
        Const sTYPE_RANGE           As String = "Range"
        Const sPASSWORD             As String = "123"
    
        Dim bOkToProceed            As Boolean
        Dim wks                     As Worksheet
    
        Set wks = ThisWorkbook.Worksheets(sSHEET_NAME)
    
        If TypeName(Selection) = sTYPE_RANGE Then
    
            If Selection.Columns.Count = 1 Then
    
                If Selection.Column = iENABLED_COLUMN_NO Then
    
                          bOkToProceed = True
    
                          wks.Unprotect Password:=sPASSWORD
                              Application.Dialogs(xlDialogActiveCellFont).Show
                          wks.Protect Password:=sPASSWORD
    
                    Else: bOkToProceed = False
    
                    End If
    
              Else: bOkToProceed = False
    
              End If
    
        Else: bOkToProceed = False
    
        End If
    
        If bOkToProceed = False Then
            MsgBox "This facility is available only for cells in Column " & _
                    iENABLED_COLUMN_NO, vbExclamation
        End If
    
    End Sub

    It's might be possible to allow the cell background colour to be changed also, but that would definitely make life more complicated.

    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    04-23-2015
    Location
    UK
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    120

    Re: Restrict OpenEvent code to one column

    Greg,

    Many thanks for the help, and the workbook - at my level, never even knew about application.dialogs !

    I think I'll keep the workbook on one side for future reference, and work with the code example you gave me.
    See where that leads.

    Many thanks again.

    Regards.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Restrict OpenEvent code to one column

    Hi again,

    Many thanks for your feedback - glad I was able to help (even if not 100% )

    Best regards,

    Greg M

+ 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 restrict code to current workbook
    By troygeri in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-03-2014, 11:17 PM
  2. [SOLVED] how can I restrict the users not to enter the balance again for the same item code?
    By heihei in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-22-2013, 09:33 PM
  3. VBA Code Excel restrict user emailing a workbook
    By Ronsco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2011, 05:03 AM
  4. Restrict View of VBA Code
    By NoiCe in forum Excel General
    Replies: 2
    Last Post: 10-24-2009, 09:36 AM
  5. 1004 Error:VB code to restrict the use of a combobox
    By bernie_bolt_sa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2008, 09:55 AM
  6. Code help appreciated:following code to restrict character input
    By peter.thompson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-22-2006, 12:20 AM
  7. [SOLVED] Opening workbook with OpenEvent
    By Nigel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-20-2005, 04:06 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