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
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.
I am trying to write a Macro that updates 4 Cell if the User select "Mailing" From Cell A1. If the User selects "Mailing" in A1, then Automatically update A2,A3,A4, and A5 to Value in B1. If the User selects something other than "Mailing", Then all four cells should be blank and the user should be able to type in any value. Any help is appreciated. Thanks
I have gotten this far, but VBA is not my thing:
Sub test()
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Target.Value = "Mailing" Then
Range("A2:A4").Value = "B1"
End If
End Sub
As the others have mentioned, you just need to put it to Sub Worksheet_Change. Note that if you are "typing" the word into cell A1, you will actually be in A2 after the "Enter".
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Mailing" Then
Range("A2:A4").Value = "B1"
End If
End Sub
The problem is you are trying to change the value of some of the cells in your code, so the code should run itself. You need to turn off events before changing the cell values and then turn it back on:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Target.Value = "Mailing" Then
Application.EnableEvents = False
Range("A2:A4").Value = "B1"
Application.EnableEvents = True
End If
End Sub
I have these sheets Sheet1 and another which is Sheet2.
Sheet1 gets its values (including color of the cell) from Sheet2.
I have this block of code to check for the active cell color in Sheet2 and then change the color of the same cell in Sheet1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Interior.Color = 5296274 Then
Worksheets("ALL BRANDS").Range(Target.Address(False, False)).Interior.Color = 5296274
Else
Worksheets("ALL BRANDS").Range(Target.Address(False, False)).Interior.Color = ActiveSheet.Range(Target.Address(False, False)).Interior.Color
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Interior.Color = 5296274 Then
Worksheets("ALL BRANDS").Range(Target.Address(False, False)).Interior.Color = 5296274
Else
Worksheets("ALL BRANDS").Range(Target.Address(False, False)).Interior.Color = ActiveSheet.Range(Target.Address(False, False)).Interior.Color
End If
End Sub
The problem is when I select multiple cells at a time in Sheet2
it colors the referenced cell in Sheet1 to
You need to collect the Interior.Color property of each cell individually.
When a Range object consists of multiple cells, a few properties (like Value and Formula) will return an array of values. Many properties, including Interior.Color, will not. In the case of Interior.Color, if ALL the cells in the range have the same background color, you will get the correct value. If even one cell has a different color, the property cannot give you a single correct answer, and simply returns 0 (black).
As a side note, your If statement isn't doing anything useful as written. I'll assume you want to copy any occurring color for the sample below. If you only want to copy that certain shade of green, keep your If but drop the Else.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
With Worksheets("ALL BRANDS")
For Each c In Target
.Range(c.Address).Interior.Color = c.Interior.Color
Next c
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
With Worksheets("ALL BRANDS")
For Each c In Target
.Range(c.Address).Interior.Color = c.Interior.Color
Next c
End With
End Sub
Really, you should move that code into a function and call it from each event instead of rewriting and maintaining the code in multiple places.
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.