Results 1 to 5 of 5

Combine two pieces of code together

Threaded View

  1. #1
    Registered User
    Join Date
    05-27-2015
    Location
    Milwaukee, WI
    MS-Off Ver
    MS Office 2010
    Posts
    6

    Combine two pieces of code together

    I found some code to help with the text being too small to read in the drop down list. It is a double click to open a combo box. I have now created code to open a comments box when a certain condition is not reached. When I try to combine the two codes my comments box code doesn’t work.
    How can I combine the two codes to make them both work and most importantly, why is breaking?
    Below is the code combined.
    Note*** if I do not activate the combo box the other code works and the comment box code works like it is supposed to.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Static sName As String
        Dim iLen    As Long
        Dim str As String
        Set ws = ActiveSheet
    
        If Len(sName) = 0 Then sName = Application.UserName & ":"
     
        With Target(1)
            If Intersect(.Cells, Range("U17:EZ74")) Is Nothing Then Exit Sub
            If .HasFormula Then Exit Sub
    
            If .Value = Cells(.Row, "B") Or .Value = Cells(.Row, "C").Value Then
            If bHasComment(.Cells) Then .Comment.Delete
            
            Else
                .Select
    
                If Not bHasComment(.Cells) Then
                    .AddComment
                Else
                    iLen = Len(.Comment.Shape.TextFrame.Characters.Text)
                End If
    
                With .Comment.Shape.TextFrame
                    .AutoSize = True
                    .Characters(Start:=iLen + 1).Insert IIf(iLen, vbLf, "") & sName & vbLf
                    .Characters(Start:=iLen + 1, Length:=Len(sName)).Font.Bold = True
                End With
                   
                With .Comment
                    .Visible = True
                    Application.SendKeys "+{F2}"
                    .Visible = False
                End With
               
            End If
        End With
    End Sub
    
    Function bHasComment(cell As Range) As Boolean
        On Error Resume Next
        bHasComment = cell.Comment.Parent.Address = cell.Address
        
    End Function
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
    Cancel As Boolean)
    Dim str As String
    Dim cboTemp As OLEObject
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Set cboTemp = ws.OLEObjects("TempCombo")
      On Error Resume Next
      With cboTemp
      'clear and hide the combo box
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
      End With
    On Error GoTo errHandler
      If Target.Validation.Type = 3 Then
        'if the cell contains a data validation list
        Cancel = True
        Application.EnableEvents = False
        'get the data validation formula
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        With cboTemp
          'show the combobox with the list
          .Visible = True
          .Left = Target.Left
          .Top = Target.Top
          .Width = Target.Width + 5
          .Height = Target.Height + 5
          .ListFillRange = str
          .LinkedCell = Target.Address
        End With
        cboTemp.Activate
        'open the drop down list automatically
        Me.TempCombo.DropDown
    
      End If
      
    errHandler:
      Application.EnableEvents = True
      Exit Sub
    
    End Sub
    Thank you,
    Ted
    Last edited by ttontis; 06-29-2015 at 12:57 PM. Reason: Becuse I didnt read the forum rules

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 combine two pieces of code and make them efficient
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2015, 02:01 PM
  2. Please explain the pieces of this code
    By freshfruit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2014, 09:22 PM
  3. [SOLVED] Please explain the pieces of this code
    By freshfruit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2014, 06:00 PM
  4. [SOLVED] Combine three pieces of information into one cell.
    By gherzberg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-25-2012, 12:55 PM
  5. Integrate two pieces of code
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-24-2009, 09:47 AM

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