I want to highlight an individual cell based upon it being changed. The code below gives me a debug error. The range I am using is A7:AH500 if any of this individual cells changes, then highlight that cell that changed. And also can I add this to a module, or does this have to go on the sheet directly?
If Not Intersect(Target, Range("A7:AH500")) Is Nothing Or _
Not Intersect(Target, Range("A7:AH500")) Is Nothing Then
Target.Interior.ColorIndex = 3
End If
From other thread to complete this off;
Okay try this - Delete what you've just added to the ThisWorkbook object. Then with the drop-downs select 'Workbook' from the left hand one, and 'SheetChange' from the right hand one. It should insert some code. Inside the Sub (before the End Sub code) - Add the code
Target.Interior.ColorIndex = 3
The code to detect a change in a cell must be in the sheet. The code that executes whatever changes you want can be in a module.
So you could have in each sheet (if more than one):
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Range("A7:AH500")) Is Nothing Or _
Not Intersect(Target, Range("A7:AH500")) Is Nothing Then
Call UpdateFormat(Target)
End If
End Sub
And in a module
Sub UpdateFormat(p_rngCell As Range)
p_rngCell.Interior.ColorIndex = 3
End Sub
This will allow you to make any changes to actual logic only once across multiple sheets.
Related
Hi I have a spreadsheet similar to below
Where when I click on a cell (red cell), I want to return the row and column number to another cell for use in an indirect lookup (blue cell)
Ideally I want to only update the cell value if it's within a set range or at least limit it only to that worksheet for error handling.
Hope that's clear... not an easy thing to google. My experiments with
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
MsgBox ActiveCell.Row
End Sub
Have returned nothing, not even a message box even though macros run fine. Any ideas?
Based on your example. Make sure your code is in the appropriate sheet module, not a standard module and make sure Application.EnableEvents=True (your existing code should have done something).
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Intersect(Target(1), Range("C4:H9")) Is Nothing Then Exit Sub
Range("J3").Value = Cells(Target(1).Row, 2) & "," & Cells(3, Target(1).Column)
End Sub
Use this in the worksheet's private code sheet.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target.Cells(1), Range("C4:H9")) Is Nothing Then
Range("C4:H9").Interior.Pattern = xlNone
Cells(3, "J") = Join(Array(Cells(Target.Cells(1).Row, "B"), _
Cells(3, Target.Cells(1).Column)), Chr(44))
Target.Cells(1).Interior.ColorIndex = 3
End If
End Sub
In the worksheet(1), I have multiple plan-to-change cells, A1 and other cells, and others cells value depends on the the value of A1 as I set up the function(eg.,B1=A1+1). In this way, if I change the value of A1, other cells would change value as well, and the total changed cells would be the A1 plus others changed cells. However, as the quantity of "other cells" is quite large, when I run the following function in VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
Cells(Target.Cells.Count, 21) = "ok"
End Sub
The column 21 would appear 3 "ok"s as I suppose because the large cells change happen in order, which actives three times the Worksheet_Change function. (the first ok for I change A1, the second for the time delay of updated function, the third for the final cell) However, I only want the Worksheet_Change to capture the final cells change after I change the value of A1, what should I do to avoid the previous capturing of Worksheet_Change function?
Modify your code to run only when A1 is changed.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
Cells(1, 21).Value = "ok"
End If
End Sub
Target is often more than a single cell. Determine if one of the changed cells is A1 with Intersect and then only deal with A1.
If you plan to change a value on the same worksheet, shut down event handling temporarily so the Worksheet_Change does not try to run on top of itself.
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, range("a1")) is nothing then
on error goto safe_exit
application.enableevents = false
dim trgt as range
for each trgt in intersect(target, range("a1"))
Cells(trgt.row, 21) = "ok"
next trgt
end if
safe_exit:
application.enableevents = true
End Sub
Since I don't believe that a1 is the actual target of your Target in your real world, I've made the code a little more verbose than it probably needs to be.
How can I set up vba code to allow user to type in several selections and then the accompanying list filters based on that input, reference cell? I tried this but its only filtering the first selection.
I set it up as a worksheet script, so it only runs on that one sheet and set it up to only run when the user input cells (A1, A2) are updated.
I then tried getting the lists in columns C:D to filter, based on the values in A1 and A2.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell is touched
If Intersect(Target, Range("A1:B5")) Is Nothing Then Exit Sub
With Sheets("TestTab")
.Range("C1:D100").AutoFilter Field:=1, Criteria1:=.Range("A1").Value, Field:=2, Criteria1:=.Range("A2").Value
End With
End Sub
If you are trying to filter a single column with what is in either cell A1 or A2:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell is touched
If Intersect(Target, Range("A1:A2")) Is Nothing Then Exit Sub
With Sheets("TestTab")
.Range("C3:D100").AutoFilter Field:=1, Criteria1:=.Range("A1").Value, Operator:=xlOr, Criteria2:=.Range("A2").Value
End With
End Sub
If you are trying to filter column C on A1 and column D on A2:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell is touched
If Intersect(Target, Range("A1:A2")) Is Nothing Then Exit Sub
With Sheets("Sheet1")
.Range("C1:D100").AutoFilter Field:=1, Criteria1:=.Range("A1").Value
.Range("C1:D100").AutoFilter Field:=2, Criteria1:=.Range("A2").Value
End With
End Sub
I have looked around for an answer and in fact the code that I am using is from this site. I have the VBA for changing PivotTable filters by inputing a value into a seperate cell like so:
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Sheets("Dashboard").Range("Lane1")) Is Nothing Then
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG"). _
ClearAllFilters
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG").CurrentPage _
= Sheets("Dashboard").Range("Lane1").Value
End If
End SUb
The code works fine. It lets me enter the value and filters accordingly, but when I delete the value in the cell it does not filter "all". Instead, the problem I run into is that a runtime error '1004' is thrown when I delete the value in the cell. I hit end and it gives me the answer. However the error keeps popping up.
I am pretty much a newborn when it comes VBA, so it might be so that I have missed something glaringly obvious.
Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng=Me.Range("Lane1")
If Not Application.Intersect(Target, rng) Is Nothing Then
With Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG")
.ClearAllFilters
If Len(rng.Value)>0 Then .CurrentPage = rng.Value
End With
End If
End SUb
I have two columns but the codition I would like is to be evaluated from one cell to another.
The first column has cells which have a drop down validation with names, and the second will activate only if a certain name from the adjacent cell is selected.
so far i only found this code but it does not seem to work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Else
Range("B1").Locked = True
End If
End Sub
I would need this code go from (for example) A1:A10 and B1:B10.
I hope I am making sense. If there is a way to do it without VBA, that would be great.
Thanks for the help.
The Target parameter tells you the range that is being changed.
You need to do something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Range("A1:A10"), Target)
If rng Is Nothing Then
' Not updating the range we care about
Exit Sub
Else
rng.Offset(0, 1).Locked = ... whatever ...
End If
End Sub
Note that your target range can be more than one cell (e.g. when using copy/paste), so you need to handle and test this case.
Calling Intersect returns you the intersection of the target range and the range you are interested in testing (A1:A10 in this sample).
You can then access the corresponding adjacent cell(s) using .Offset(0,1)
That code snippet works perfectly for me.
Did you place that code in the proper WorkSheet object? It won't work if you just put it into a VBA module. When you are in the Visual Basic Editor, look for a directory on the left side of the screen labeled "Microsoft Excel Objects". In that directory should be a WorkSheet object for every sheet in your file. Double-click on one of these to edit the code for that WorkSheet. This is where your code snippet should go.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Me.Unprotect ("password")
Else
Range("B1").Locked = True
Me.Protect ("password")
End If
End Sub
Use Me.Protect so the .Locked method does something. You should probably unlock every other cell though.