Combing two VBA Codes

admahen

New Member
Joined
Feb 14, 2018
Messages
3
Hi,

I have the following two codes and would really appreciate if someone could help me combine them.

Thank you in advance,

Code 1:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With Sheet2.DTPicker1
.Height = 20
.Width = 20
If Not Intersect(Target, Range("I:J")) 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

Code 2:

Private Sub Worksheet_Change(ByVal Target As Range)


Dim Oldvalue As String
Dim Newvalue As String


On Error GoTo Exitsub
If Target.Column = 7 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
Target.Value = Oldvalue & ", " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The posted code uses 2 different events, SelectionChange and Change, and work with different ranges.

How exactly would you want to combine them?

Are you sure you need to combine them?
 
Last edited:
Upvote 0
The posted code uses 2 different events, SelectionChange and Change, and work with different ranges.

How exactly would you want to combine them?

Are you sure you need to combine them?

Hi Norie,

Ideally I would love for it to work on its own but unfortunately they both do not seem to run at the same time. I am very new to VBA and tried everything to make both codes work (Codes sourced online and modified) on the same sheet but only the top one would run.

One code is suppose to let me pick multiple items from a drop down list and the other lets me pick dates from a calendar.
 
Upvote 0
I don't see why they shouldn't work as they are.

How aren't they working?

Is the second code definitely not running when you make a change in column G?
 
Upvote 0
It lets me make changes but column G wont let me pick multiple items from my drop down list as it should do when I have both codes on. The second code works well by it self.

this is how i got it set up now,

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With Sheet2.DTPicker1
.Height = 20
.Width = 20
If Not Intersect(Target, Range("I:J")) 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


Private Sub Worksheet_Change(ByVal Target As Range)


Dim Oldvalue As String
Dim Newvalue As String


On Error GoTo Exitsub
If Target.Column = 7 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
Target.Value = Oldvalue & ", " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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