Target.address for multiple rows in Excel - 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

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

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

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/

How to work with more Target.Address (range)

In a cell, I have made a list to choose. However, one choice is not enough. I found some vba code that enables me to make more choices. But this code points to a specific cell. I want to use it more general in the sheet, probably a range.
I tried to put in a range as target.address but that won't work. It won't work with a range of one cell (J3) either. I found out that the formula below does not work after removing the dollar signs from $j$3.
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
TheCell = ActiveCell
If Target.Address = "$J$13" 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 look for a solution to choose from all the list of more than one item. The formula should be changed in a way that is usable in a range of cells. These cells have al the same choice list.
Instead of
If Target.Address = "$J$13" Then
You can do an Intersect of your Target range with the range you like to check:
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Me.Range("J13:J20")) 'put your range here
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange 'loop through all affected cells
'here use Cell instead of Target
Next Cell
End If

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

Drop values saved comma separated in a cell in excel

I have a metadatasheet from which I am setting some parameters from which I am generating pivots. I am selecting these parameters through dropdown lists.
Here is what my metadata sheet looks like.
I am saving the values of dropdown in a comma separated manner in the corresponding cell. For that I have a macro on that sheet which is :
Private Sub Worksheet_Change(ByVal Target As Range)
'Set automatic formula calculation ON
Application.Calculation = xlAutomatic
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$C$5" Or Target.Address = "$C$6" Or Target.Address = "$C$7" Or Target.Address = "$D$5" Or Target.Address = "$D$6" Or Target.Address = "$D$7" Or Target.Address = "$E$5" Or Target.Address = "$E$6" Or Target.Address = "$E$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
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
My issues:
1) Rather than defining individual cells in the 10th line of macro, is there a way define a range? Basically I am defining all the cells of the range C5:E7 as individual cells using "Or"
2) I am not able to delete individual comma separated values, because while doing so it give me the following error.A user has restricted values that can be entered into the cell
I have to entire cell and then select the values again. Is there a way I can delete only single value?

Resources