Consulting

Results 1 to 5 of 5

Thread: One VBA disables another, how to correct

  1. #1
    VBAX Regular
    Joined
    Dec 2007
    Posts
    23
    Location

    Question One VBA disables another, need help correcting

    I have run the macros (copied from the kbase):

    1) Comments - to enter a comment into any cell by double-clicking
    2) Protect entire worksheet at once with a password

    The problem that I am having is that since having run the workbook protection macro, the comment macro has been disabled. Upon double-clicking on any cell, a dialogue box comes up that will allow for a comment to be entered, but once typed into the dialogue box and enter is pressed, the comment does not display in the cell.

    Is there an easy way to correct this problem?
    Last edited by cmm0812; 01-21-2008 at 10:51 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Show us your code and we might be able to tell.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Dec 2007
    Posts
    23
    Location
    Comments

    'Code to be placed in the worksheet(s) you want to use this in
    Option Explicit
    Public oldRange As Range

    Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    On Error Resume Next
    Dim rng As Range
    Set rng = Target(1, 1)

    oldRange.Comment.Visible = False

    With rng
    If Not .Comment Is Nothing Then
    If .Comment.Visible = False Then
    .Comment.Visible = True
    Else
    .Comment.Visible = False
    End If
    End If
    End With

    Set oldRange = Target(1, 1)
    End Sub

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    On Error Resume Next
    Dim cmtText As String
    Dim inputText As String

    If Target.Comment Is Nothing Then
    cmtText = InputBox("Enter info:", "Comment Info")
    If cmtText = "" Then Exit Sub
    Target.AddComment Text:=cmtText
    Target.Comment.Visible = True
    Target.Comment.Shape.TextFrame.AutoSize = True 'Remove if you want to size it yourself
    Else
    If Target.Comment.Text <> "" Then
    inputText = InputBox("Enter info:", "Comment Info")
    If inputText = "" Then Exit Sub
    cmtText = Target.Comment.Text & Chr(10) & inputText
    Else
    cmtText = InputBox("Enter info:", "Comment Info")
    End If
    Target.ClearComments
    Target.AddComment Text:=cmtText
    Target.Comment.Visible = True
    Target.Comment.Shape.TextFrame.AutoSize = True 'Remove if you want to size it yourself
    End If

    Cancel = True 'Remove this if you want to enter text in the cell after you add the comment
    End Sub


    Password protect

    Option Explicit

    Sub ProtectAll()

    Dim wSheet As Worksheet
    Dim Pwd As String

    Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
    For Each wSheet In Worksheets
    wSheet.Protect Password:=Pwd
    Next wSheet

    End Sub

    Sub UnProtectAll()

    Dim wSheet As Worksheet
    Dim Pwd As String

    Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
    On Error Resume Next
    For Each wSheet In Worksheets
    wSheet.Unprotect Password:=Pwd
    Next wSheet
    If Err <> 0 Then
    MsgBox "You have entered an incorect password. All worksheets could not " & _
    "be unprotected.", vbCritical, "Incorect Password"
    End If
    On Error Goto 0

    End Sub

  4. #4
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    This sounds like my KB entry. You have to modify the Comment Code to check for worksheet protection BEFORE it runs, then have it behave accordingly.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    VBAX Regular
    Joined
    Dec 2007
    Posts
    23
    Location
    I have tried correcting the code but need some help, as I still cannot get it to show comments once they have been typed (I am new at writing VBAs, as you can tell). I also think I have the user interface bit incorrect. I do not want to have the protection disabled while imputting a comment in. I want to have the protection running continuously through the comment adding process, and just allowing for comments as an option, if possible.


    [VBA] Option Explicit
    Public oldRange As Range

    Private Function wbookSheetProtection(WorkbookTarget As Workbook) As Boolean
    Dim wsheet As Worksheet
    For Each ws In wbookTarget.Worksheets
    If wsheet.ProtectContents = True Then
    .Comment.Visible = True
    .Protect Password:="YourPassword", UserInterFaceOnly:=True
    Else
    .Comment.Visible = True
    Exit Function
    End If
    Next ws

    End Function
    Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    On Error Resume Next
    Dim rng As Range
    Set rng = Target(1, 1)
    'I wasn't sure if I should add a command in here to have this code recognize the previous function
    oldRange.Comment.Visible = False
    With rng
    If Not .Comment Is Nothing Then
    If .Comment.Visible = False Then
    .Comment.Visible = True
    Else
    .Comment.Visible = False
    End If
    End If
    End With
    Set oldRange = Target(1, 1)
    End Sub
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    On Error Resume Next
    Dim cmtText As String
    Dim inputText As String
    If Target.Comment Is Nothing Then
    cmtText = InputBox("Enter info:", "Comment Info")
    If cmtText = "" Then Exit Sub
    Target.AddComment Text:=cmtText
    Target.Comment.Visible = True
    Target.Comment.Shape.TextFrame.AutoSize = True
    Else
    If Target.Comment.Text <> "" Then
    inputText = InputBox("Enter info:", "Comment Info")
    If inputText = "" Then Exit Sub
    cmtText = Target.Comment.Text & Chr(10) & inputText
    Else
    cmtText = InputBox("Enter info:", "Comment Info")
    End If
    Target.ClearComments
    Target.AddComment Text:=cmtText
    Target.Comment.Visible = True
    Target.Comment.Shape.TextFrame.AutoSize = True
    End If
    Cancel = True
    End Sub[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •