trying to add 2 events that happen in the same column in a worksheet - excel

Good day
I am trying to make both of the macros work in the same range since I want the drop down it that is created there to be able to check if there is a value in the column next to it get that rows values and also still be able to run the first macro. see picture attached since I don't think I am explaining properly what I want.
So in the picture in column A is set number. Column B has the dropdown where the first macro was implemented which enables it to be able to get values from column A and also be able to add a value to show 2+3 , I need it then to be able to get the values of stream C and actually add them in column C
attached is my current code and a picture of a example that doesn't necessarily work with the code just an example show what I mean.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Dim nommer As Integer
Dim finder As Range
On Error GoTo Exitsub
If Target.Column = 3 Then <------- here is macro 1
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
If Not Intersect(Target, Range("J9")) Is Nothing Then
Select Case Range("J9")
Case "A": toets_my_ws
End Select
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
If Target.Column = "3" Then <------- here is macro 2
nommer = ActiveCell.Value
Else: If Target.Value = "" Then GoTo Exitsub Else
Set finder = Range("B9:B40").Find(what:=ActiveCell.Value,LookIn:=xlValues, lookat:=xlWhole)
ActiveCell.Offset(0, 3).Value = finder.Offset(0, 4).Value
ActiveCell.Offset(0, 5).Value = finder.Offset(0, 6).Value
End If
End Sub
]1

You can only have one event of this type as it works for the entire worksheet. So if you have multiple criteria for what happens when the worksheet changes you need to include all of that logic in the event. I have combined your logic in to one big thing but there is no way for me to tell if this was done correctly. You never use the variable nommer so that does nothing. It's also not clear what toets_my_ws is.
I would advise against using Worksheet change events as is slows down the worksheet considerably.
The better way to tackle this would be to use User Defined Functions (UDF). This way you can embed VBA in to a single cell without bogging down the whole sheet with logic every time you press a key.
HERE IS THE COMBINED LOGIC:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Dim nommer As Integer
Dim finder As Range
If Target.Value = vbNullString Then GoTo Exitsub
On Error GoTo Exitsub
If Target.Column = 3 Then
nommer = ActiveCell.Value
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing 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
Else
Set finder = Range("B9:B40").Find(what:=ActiveCell.Value, LookIn:=xlValues, lookat:=xlWhole)
ActiveCell.Offset(0, 3).Value = finder.Offset(0, 4).Value
ActiveCell.Offset(0, 5).Value = finder.Offset(0, 6).Value
End If
If Not Intersect(Target, Range("J9")) Is Nothing Then
Select Case Range("J9")
Case "A"
toets_my_ws
End Select
End If
Exitsub:
Application.EnableEvents = True
End Sub
HERE IS INFORMATION ABOUT UDFS:
https://excelchamps.com/excel-user-defined-function/

Related

Allow Select all that Apply in Excel in Multiple Columns on the same sheet

I am creating an activity tracker in Excel. I'd like to be able to "Select all that apply" from drop down lists in two separate columns on the same sheet.
I am using this VBA code for one column:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To make mutliple selections in a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
If Target.Column = 6 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
I found this code online, and altered it to apply to all of Column 6 instead of one cell.
I would like this to work for three columns on the sheet, Columns 1, 6, and 9. I imagine this is achieved by adding an Else statement somewhere below my If Target.Column = 6 statement.
Trying to answer your question, you may change the folowing If statement:
If Target.Column = 6 Then
into
If Target.Column = 1 Or Target.Column = 6 Or Target.Column = 9 Then

How to have apply Worksheet_Change to more than one cell?

I implemented code to timestamp a cell whenever a condition in another cell in the same row is manually met:
Private Sub Worksheet_Change(ByVal target As Range)
Dim A As Range: Set A = Range("A2:A2800")
Dim v As String
If Intersect(target, A) Is Nothing Then Exit Sub
Application.EnableEvents = False
v = target.Value
If v = "" Then target.Offset(0, 6) = ""
If v = "Solicitud enviada" Then target.Offset(0, 6) = Date
Application.EnableEvents = True
End Sub
I need to timestamp another cell by a different criteria. I know I can't have two Worksheet_Change subs at the same time, but from what I've investigated trying to have two events at the same time goes beyond me.
Private Sub LeadTimeStamp(ByVal target As Range)
Dim D As Range: Set D = Range("D2:D2800")
Dim b As String
If Intersect(target, D) Is Nothing Then Exit Sub
Application.EnableEvents = False
b = target.Value
If b = "" Then target.Offset(0, 8) = ""
If b = "lead" Then target.Offset(0, 8) = Date
Application.EnableEvents = True
End Sub
b needs to be compared as a string array with the cell, something like b.length <= 10 if this was JavaScript.
I know that VBA uses LEN(), but I do not know how to use it here. For now I have a placeholder condition similar to the one on the original code, to make sure that the code works before I tackle the array condition part.
Your situation is that you need to check for one of several possible changes, so that means an If statement at the Target level. So in outline form it would look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
'--- only deal with single-cell changes. multi-cell
' edits are skipped
If Target.CountLarge > 1 Then Exit Sub
Dim solicitudArea As Range
Dim leadArea As Range
Set solicitudArea = Range("A2:A2800")
Set leadArea = Range("D2:D2800")
Application.EnableEvents = False
If Not Intersect(target, solicitudArea) Is Nothing Then
'--- a request has changed
If Target.Value = vbNullString Then
Target.Offset(0, 6).Value = vbNullString
ElseIf Target.Value = "Solicitud enviada" Then
Target.Offset(0, 6).Value = Date()
End If
ElseIf Not Intersect(target, leadArea) Is Nothing Then
'--- a request has changed
If Target.Value = vbNullString Then
Target.Offset(0, 8).Value = vbNullString
ElseIf Target.Value = "lead" Then
Target.Offset(0, 8).Value = Date()
End If
End If
Application.EnableEvents = True
End Sub
A good practice is to use variable names that match up with what those values represent. It makes the code easier to read and maintain later on.

How to copy only the last value of a split cell into a new worksheet

I am trying to copy a string of values into a single column on a new sheet. My code works when there is only one value in the active cell, but will copy every value in the cell once there are multiple values. I want it to copy only the most recent addition to the column on the new sheet. The input is selections from a drop-down menu that allows for multiple selections. I then have these selections being split and offset to a new cell 9 columns over (I also have other drop-downs so that is why there is so much space, but the larger loop should be able to handle the other drop-downs).
This is an image of the input:
This is what I am currently getting as an output:
This is my desired output:
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & "; " & Newvalue
Dim txt As String
Dim i As Integer
Dim FullName As Variant
txt = ActiveCell.Value
FullName = Split(txt, ";")
For i = 1 To UBound(FullName)
ActiveCell.Offset(i, 9).Value = FullName(i)
ActiveCell.Offset(i, 9).Copy
Worksheets("Links").Range("A3").End(xlUp).Offset(2, 0).Insert
Next i
I have included only the loop of code that is problematic in order to simplify finding a solution.
My best guess is that on a detected change, you want to update a distinct list of values in 9 cells over?
Right now you are already managing a single distinct list. All that you would need to do is clear the values in the column 9 cells over then print the values in your drop down.
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$A$1" 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
Dim txt As String
Dim i As Integer
Dim FullName As Variant
txt = ActiveCell.Value
FullName = Split(txt, ";")
ActiveCell.Offset(, 9).EntireColumn.Clear
For i = 0 To UBound(FullName)
ActiveCell.Offset(i, 9) = Trim(FullName(i))
Next i
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
But what if I want a distinct list from more than one drop down or ; delimited array? The best way to manage a distinct list in is a Collection or a Dictionary object.
If that is what your looking for, I will update this answer with a way to use those objects.
Based on your feed back I have updated the code to below to use a collection object to manage your distinct list from more than one drop down.
Option Explicit
Private col As Collection
' ^ we are defining this to the module level. That means it will retain values
' and be able to be referenced from any other place in the project.
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("$A$1:$B$1")) Is Nothing Then
' ^ this will make the area your looking more specific than just .row = 11
' you could also replace the address with a namedRange
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else:
If Target.Value = "" Then
'' My guess is that here you would want to make a call to a function that
'' removes values from the function. You should be able to loop over the collection
'' to find the value to remove.
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
ManageList Newvalue
' ^ you already have the newest value. You just need a easy way to check if it
' is in the list. To do this I made a sub that receives text, and checks
' if it is in the publicly scoped collection.
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
Private Sub ManageList(txt As String)
' This Sub will receive a text value and try to put it in a collection.
If col Is Nothing Then Set col = New Collection
On Error Resume Next
col.Add Item:=txt, Key:=txt
' ^ this method will throw an error if the Key is already in the collection.
' all we need to do then is watch for errors, and handle if we found a new one.
' I have found that collections and dictionary objects can handle .5M keys without any issues.
' using a dictionary, would allow you to test for a key without defining an error handler.
' with the trade off being that you have to add an additional reference to your project.
If Err.Number = 0 Then
' we had a new value
PrintList col
End If
End Sub
Private Sub PrintList(col As Collection)
Dim printTo As Range
Dim i As Long
Set printTo = Range("e1")
' ^ change e1 to a fully qualified address of where you
' want you list to be printed.
printTo.EntireColumn.Clear
On Error GoTo eos:
For i = 0 To col.Count - 1
printTo.Offset(i) = col(i + 1)
Next
eos:
End Sub

Ambiguous name detected: Worksheet_change

I'm attempting to add a second code to a single worksheet and keep getting the "Ambiguous name detected" error. Realise that I need to combine the two codes but having trouble doing so. here are the two codes, one below the other:
Private Sub Worksheet_Change(ByVal Target As Range)
'are changes made within answer range?
Set isect = Application.Intersect(Target, Range("Answers"))
If Not (isect Is Nothing) Then
For Each chng In Target.Cells
'Get row number
startY = Impact.Range("Answers").Row
targetY = chng.Row
row_offset = (targetY - startY) + 1
rating_type = Impact.Range("Impacts").Cells(row_offset, 1)
If rating_type = "Major / V.High" Then cols = 16711884
If rating_type = "Significant / High" Then cols = 255
If rating_type = "Important / Moderate" Then cols = 49407
If rating_type = "Minor / Low" Then cols = 5287936
If rating_type = "" Then cols = 16777215
Impact.Range("Ratings").Cells(row_offset, 1).Interior.Color = cols
Impact.Range("Impacts").Cells(row_offset, 1).Interior.Color = cols
Next chng
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$C$2" 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
Was hoping someone knows how to combine the two in order to circumvent this error.
Thanks in advance!
Based on my comment, you can track changes in more than one range as shown in the below sample code.
Private Sub Worksheet_Change(ByVal Target As Range)
'Exit the sub if more than one cells are changed at the same time
If Target.CountLarge > 1 Then Exit Sub
'Disable the event so that if the code changes the cell content of any cell, the code is not triggered again
Application.EnableEvents = False
'Error handling to skip the code if an error occurs during the code execution and enable the events again
On Error GoTo ErrorHandling
'Change event code will be triggered if any cell in column A is changed
If Not Intersect(Target, Range("A:A")) Is Nothing Then
MsgBox "The content of a cell in colunm A has been changed."
'Change event code will be triggered if any cell in column C is changed
ElseIf Not Intersect(Target, Range("C:C")) Is Nothing Then
MsgBox "The content of a cell in colunm C has been changed."
'Change event code will be triggered if any cell in column E is changed
ElseIf Not Intersect(Target, Range("E:E")) Is Nothing Then
MsgBox "The content of a cell in colunm E has been changed."
End If
ErrorHandling:
Application.EnableEvents = True
End Sub

Target.address for multiple rows in Excel

I need to reference an entire column of Excel spreadsheet, with a drop-down list using VBA. The code i got online works only for a single cell which is "$M$2". How can i define a range for the entire column?
Private Sub Worksheet_Change(ByVal Target As Range)
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
If Target.Address = "$M$2" 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
Firstly, Target may be a single cell or multiple cells, depending on what the user changed
To test if any cell in (and only in) column M changed, use
If Target.EntireColumn.Address = "$M:$M" Then
To test if any cell in Target is in column M use
Dim rng As Range
Set rng = Application.Intersect(Target, Me.Columns("M"))
If Not rng Is Nothing Then
Note: the rest of your code will need to be modified to allow for Target being more than one cell

Resources