+ Reply to Thread
Results 1 to 5 of 5

Combine two pieces of code together

Hybrid View

ttontis Combine two pieces of code... 06-29-2015, 10:24 AM
ttontis Re: Combine two pieces of... 06-29-2015, 10:30 AM
6StringJazzer Re: Combine two pieces of... 06-29-2015, 11:07 AM
ttontis Re: Combine two pieces of... 06-29-2015, 12:33 PM
6StringJazzer Re: Combine two pieces of... 06-29-2015, 12:41 PM
  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

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

    Re: Combine two pieces of code together

    I just noticed that if I do not actavate the combo box the other code works and the comment box is actaved like it is supposed to. So I guess the new question is how do I get my comments box code to work with my combo box code?

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,784

    Re: Combine two pieces of code together

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

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

    Re: Combine two pieces of code together

    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

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,784

    Re: Combine two pieces of code together

    Please edit your first post to add code tags, instead of adding a new post.

+ 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 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