Posting 2 VBA Codes on One Sheet

Lilmacks3

New Member
Joined
Mar 14, 2019
Messages
3
Good morning. I am new to this site and brand new to VBA. Basically I am trying to add 2 codes to the same sheet but I am not doing it correctly as only one code is working. Could someone please help me? These are the 2 codes I am trying to get on the same sheet:

#1

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  
  With Sheet1.DTPicker1
    .Height = 20
    .Width = 20
    If Not Intersect(Target, Range("D4:D200, E4:E200")) Is Nothing Then
      .Visible = True
      .Top = Target.Top
      .Left = Target.Offset(0, 1).Left
      .LinkedCell = Target.Address
    Else
      .Visible = False
    End If
  End With
      
End Sub


AND #2

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$I$4" Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

That's it! Please let me know if you can help. Thank yo so much!
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
.
First, I did not review both macros to see if they could be reduced in size or edited for efficiency.

The quick and dirty method of combining both would be :

Code:
Option Explicit


'#1


Private Sub Worksheet_SelectionChange(ByVal Target As Range)


With Sheet1.DTPicker1
.Height = 20
.Width = 20
    If Not Intersect(Target, Range("D4:D200, E4:E200")) Is Nothing Then
        .Visible = True
        .Top = Target.Top
        .Left = Target.Offset(0, 1).Left
        .LinkedCell = Target.Address
    Else
        .Visible = False
    End If
End With


'AND [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2]#2[/URL] 


Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
    If Target.Address = "$I$4" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else: If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
            Else
                If InStr(1, Oldvalue, Newvalue) = 0 Then
                    Target.Value = Oldvalue & ", " & Newvalue
                Else:
                    Target.Value = Oldvalue
                End If
            End If
        End If
    End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

Note: It is a requirement of this FORUM and other FORUMS that all code be placed with the hash marks which is provided on the REPLY MENU. These symbols allow
for the code to be displayed as seen here which makes it a lot easier to read and understand. You should also get into the habit of indenting your code as shown.

Best wishes.
 
Upvote 0
Thank you so much! So for some reason that doesn't work for me and I am not sure why. I tried both of these codes separately and they work but they don't both work with the suggestion above. I sincerely appreciate you taking the time. Any more thoughts? Thanks!
 
Upvote 0
What error message are you receiving ?

What line of code is highlighted ?
 
Upvote 0
Hey! So I am not getting an error message at all. The first code is working but the second code just isn't working. No error message or anything like that. But it's like the code just isn't there, is the best way to describe it.
 
Upvote 0
Ok.

Please create a sample workbook with the second macro only.

Provide a BEFORE and AFTER example of what you expect the macro to do.
Understand the macro doesn't actually have to work at this point ... just provide an example of what
it should do on the workbook sheet.

You will need to post the example on a cloud site like: DropBox.com or Google Cloud, etc.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top