Change Event in VBA Code - Combine multiple Events in one WorkSheet / Workbook

Melissa_

New Member
Joined
Oct 25, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello!
It appears that I have a similar question and I'm getting Stuck! I would appreciate a little help on this!

I have a Workbook that has a number of Worksheets that once a particular number of cells are selected/Fill the user can then click a CSV button that generates a CSV file that they will upload into JIRA to automatically created a number of JIRA Issues. I have the code for the CSV and works great! In the cells that need to be selected/filled I have three Drop down lists. This makes sure my users are nothing adding random things and everything is uniformed for sorting purposes. I'm able to achieve this via a Change Event and I get it to work on one event. I would like to have all three Change Events. I attempted but I'm getting stuck! Help! Below are the three Change Events that I would like to use. Two are very simple but the third one is a big more involved and I think that's where I get stuck.

Change Event 1
VBA Code:
' DropDown_ProductID
ProductName = Target.Value
If Target.Column = 10 Then
ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("ProductID"), 2, False)
If Not IsError(ProductCode) Then
Target.Value = ProductCode
End If
End If

Change Event 2
VBA Code:
' DropDown_StaffName

StaffName = Target.Value
If Target.Column = 8 Then
StaffTest = Application.VLookup(StaffName, ActiveSheet.Range("StaffName"), 2, False)
If Not IsError(StaffTest) Then
Target.Value = StaffTest
End If
End If

End Sub

Change Event 3 -
VBA Code:
'Multple Section
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lOld As Long

If Target.Count > 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)

On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing

Else

Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal

If Target.Column = 9 Then
If oldVal = "" Then
'do nothing

Else
If newVal = "" Then
'do nothing

Else
lOld = Len(oldVal)

If Left(newVal, lOld) = oldVal Then
Target.Value = newVal

Else
Target.Value = oldVal _
& ", " & newVal

End If
End If
End If
End If
End If

If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)

If Left(newVal, lOld) = oldVal Then
Target.Value = newVal

Else
Target.Value = oldVal _
& ", " & newVal

End If
End If
exitHandler:

Application.EnableEvents = True
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello!
It appears that I have a similar question and I'm getting Stuck! I would appreciate a little help on this!

I have a Workbook that has a number of Worksheets that once a particular number of cells are selected/Fill the user can then click a CSV button that generates a CSV file that they will upload into JIRA to automatically created a number of JIRA Issues. I have the code for the CSV and works great! In the cells that need to be selected/filled I have three Drop down lists. This makes sure my users are nothing adding random things and everything is uniformed for sorting purposes. I'm able to achieve this via a Change Event and I get it to work on one event. I would like to have all three Change Events. I attempted but I'm getting stuck! Help! Below are the three Change Events that I would like to use. Two are very simple but the third one is a big more involved and I think that's where I get stuck. Change Event 3 should only work on the particular column (9) not that other ones

Change Event 1
VBA Code:
' DropDown_ProductID
ProductName = Target.Value
If Target.Column = 10 Then
ProductCode = Application.VLookup(ProductName, ActiveSheet.Range("ProductID"), 2, False)
If Not IsError(ProductCode) Then
Target.Value = ProductCode
End If
End If

Change Event 2
VBA Code:
' DropDown_StaffName

StaffName = Target.Value
If Target.Column = 8 Then
StaffTest = Application.VLookup(StaffName, ActiveSheet.Range("StaffName"), 2, False)
If Not IsError(StaffTest) Then
Target.Value = StaffTest
End If
End If

End Sub

Change Event 3 -
VBA Code:
'Multple Section
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lOld As Long

If Target.Count > 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)

On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing

Else

Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal

If Target.Column = 9 Then
If oldVal = "" Then
'do nothing

Else
If newVal = "" Then
'do nothing

Else
lOld = Len(oldVal)

If Left(newVal, lOld) = oldVal Then
Target.Value = newVal

Else
Target.Value = oldVal _
& ", " & newVal

End If
End If
End If
End If
End If

If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)

If Left(newVal, lOld) = oldVal Then
Target.Value = newVal

Else
Target.Value = oldVal _
& ", " & newVal

End If
End If
exitHandler:

Application.EnableEvents = True
End Sub

Sorry for got to include that that Last Change Event should only work on the particular column (9) not that other ones
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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