When the value in a cell changes change the filter on a pivot table - vba

I have been reading around for a bit and reached the ask SO point. I have a drop down selector in excel that lets me change names and when the name is changed, the value in "C3" is changed (c3 references another cell on another sheet if that matters) and when the value in "C3" changes I want a pivot table on another sheet (sheet6 for now) to update its filter on territory id to reflect the new value of "C3".
It sounds like an event to me, so I went digging around and found out VBA has events (yay!), but I have been unable to get my event to actually do anything. The code I have there works when I run it as a macro, but I would really like it to automatically run everytime the value in cell "C3" changes.
Here is what I am trying so far:
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Sheets("Current Status").Range("C3")) Is Nothing Then
Sheets("Sheet6").PivotTables("PivotTable5").PivotFields("territory_id"). _
ClearAllFilters
Sheets("Sheet6").PivotTables("PivotTable5").PivotFields("territory_id").CurrentPage _
= Sheets("Current Status").Range("C3").Value
End If
End Sub
Update: I have found that if I put the above code in the sheet section (sheet 2) instead of in a new module I can get it to run if I physically enter the code and then hit enter. Now I am wondering if there is a way to make it do it without me having to manually enter the value and hit enter. I just want to be able to use my drop down menu to select a new name and when the value in c3 changes due to the drop down selector update the pivot table.
Thank you as always SO.

The problem is that C3 is not actually changing, because it's just formula reference that is updating. Is the "drop down selector" on a form or based on data validation and in a cell?
If it's based on a cell, set your target to be the target cell, not C3 -> because C3 is just a
formula reference, and your drop down cell is the one actually changing.
If it's based on a form, but code in the on_change event of the form control.

Related

Refering to Table cell in Macro

So, I am relatively new to VBA and Programming as a whole,
I know in Excel Formulae, Rather than refer directly to a Cell ($B$27) you can refer to the Column name instead, (#[Condition]).
Now I am aware that this only works on "linear" tables, the formulae will only work on and affect the row its in, but for the table i am working on this is fine.
My question is, is there a way to refer to this inside VBA?
I am trying to create a function that takes the value of a "Condition" Cell and if this is "Other" it will open a custom User form with 3 buttons that allow the user to select the reason why the Condition is "other"
Depending on the button click, the Value of another Cell in the same table (Works Completed) will be changed to what ever the reason they click.
(E.G, user clicks "To be Repaired", the value of "Works Complete" will be changed to "For Repairs"
This currently only works when referred to a Cell directly ($B$27)
Whenever you have a question, concerning the translation of a manual action in Excel to VBA the best answer is:
Start the macro recorder, do the manual action and check yourself.
This is a sample of a recorded macro, selecting Table1, Column3:
Sub Macro1()
Range("Table1[[#Headers],[Column3]]").Select
ActiveCell.FormulaR1C1 = "Testing"
Range("C2").Select
End Sub

How to use "trigger" in Excel 2010 to transcribe value into inputted row name and column name?

I am trying to use some sort of trigger to allow the user of this sheet to take an outputted value (through an algorithm I created) and transcribe it into another sheet.
The cell that it should transcribe the value to will be determined by taking the row name and column name from the user.
In this example, the user would insert X and Y as their row name and column name respectively. Once they click the trigger button (does not have to be a button, I am willing to use any type of trigger, but I assume there must be one in order for this to work), the value in the "Value (Rounded)" labeled cell will be transcribed to the corresponding cell:
This cell is where I want to insert the value.
The goal of this is automation. Ideally the user only needs to input the row name and column name and press a button (or any other trigger), which will transcribe the value into the other sheet.
Does this have to be VBA? This can be done with a pretty simple formula.
Assumptions:
The input sheet is named Input (change all references to Input to be the actual name of that sheet)
The row name is in cell B1 (change Input!$B$1 to be the actual row name entry cell)
The column name is in cell B2 (change Input!$B$2 to be the actual column name entry cell)
The Value (Rounded) cell is in B4 (change Input!$B$4 to be the actual Value (Rounded) cell)
In cell B2 of our Output sheet, put in this formula and then copy over and down:
=IF(AND(Input!$B$1=$A2,Input!$B$2=B$1),Input!$B$4,"")
Adding a button can be done by creating a shape (rectangle or any other), right click it and select "Assign Macro". There you can select a macro which should be carried out after clicking that shape. One note though, the macro needs to be a "public sub".
You need to use event macro within the SHEET code (not module code). Here are two examples of how to get if a cell is changed or if a double click happens, both on specific cells:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("E3")) Is Nothing Then
'Do something of Range("E3") has been double clicked
'(Basic Example)
'Range("G3").Value = "Range("B3").value
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'If a cell has chanced...
If Not Intersect(Target, Range("B3")) Is Nothing Then
'Do something
End If
End Sub

Excel, VB form with Text boxes. On button click. Create new row in worksheet with data from text boxes

Ive been looking around EVERYWHERE! And nothing works. ever.
I would like to be able to have a form that you fill in ( Lets say with two textboxes2 )
When you click the button it should then add a new row below all other populated rows and put the data of the text boxes into the cells of that row.
Nothing I do works :(
Does anyone have any ideas?
My form has:
Textbox1
Textbox2
Worksheet is called Sheet1
( Ive left everything default while testing )
Any help would be epic..
Once I have this bit sorted, i can then modify to do what i want to achieve in the long run. but this basic " Add a row with some data" is just killing me.. It cant be that hard right?
Thanks
G
I believe you could follow a logic similar to that : (After the click)
Find the last populated row
Insert an empty row under it
Grab the data from the textboxes and copy them to their proper column
Does that help? If not, what part are you having trouble? I'll be happy to provide more help.
Something like this could get you started. This assumes your first text box will fill in column A and second text box fills in column B.
Sub InsertText()
Range("A65000").End(xlUp).Offset(1, 0).Select
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Value = TextBox2.Value
End Sub
I don't think you need to add another row if you're just filling in below the last populated cell. If so
ActiveCell.EntireRow.Insert

How to highlight a cell when formula result from another sheet changes?

This is one that's been killing me and I've tried almost every solution on the Internet.
Here's background. I have an HR model that has each department broken out on separate tabs. I want to run an extract from our payroll system each payroll run and send highlight any updates individually. If someone's title or salary or status changes, I want to have that called out by highlighting the cell.
Each tab uses an INDEX/MATCH lookup to the extract tab to pull in the current information. What I want is if any value changes or is new(new hire, for example), highlight the cells.
I've played with Worksheet_Calculate and Worksheet_Change to no avail. Worksheet_Change doesn't fire because I'm not making the change directly on the sheet and Worksheet_Calculate doesn't have the Target object for to reference. I've tried the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range(Target.Dependents.Address)
If Not Intersect(updatedCell, Range("A:A")) Is Nothing Then
updatedCell.Interior.ColorIndex = 3
End If
End Sub
The range I actually need evaluated is A7:R104 but I've been trying to get anything to work when linked to another sheet.
This works fine if formula of target cell is pointing to another cell on same sheet. The moment you point to one on another sheet it doesn't work. I've tried most of the solutions on here with no success. I've even tried putting the Worksheet_Change on the extract sheet and see if I can trigger it that way with no luck.
Is there a recommended solution to triggering a change to a cell for a formula linked to another sheet?
so I just saw this post, I don't know if you've found the solution or are still looking, but:
if you select a cell in sheet 3, you can then go to the home tab, go to "conditional formatting" -highlight cell rules - more rules (at the bottom) - and "use formulas to determine which cells to format" and then put your cursor in the formula box. now, select a cell in sheet 1 (click the sheet1 tab, and click a cell) and you'll notice it should populate the address for sheet1, and the cell u selected. now type <> after that cells address, then select sheet2 and a cell. then click format, and choose a fill color. then ok. if you go to conditional formatting and manage rules it will show there the rule / formula and which cells it applies to.
doing this i was able to select cell D10 in sheet 3, and make it an ugly green if cells in sheet1 and 2 didnt match (I picked which cells) you can also select a range of cells.
thusly, you can apply this rule to whatever dells you want, and if you record a macro of you setting this conditional formatting, you can manitpulate that macro to apply it to a bunch of different cells, and change the ranges. (using loops / variables)

Copy data from one part of a userform to another automatically

I am trying to work with an Excel form using VBA that I have customized (I found the original on the web).
I have two tabs: one is called Planning-Deleted, the other is called Planning-Deleted Data. All the data that I enter in the first tab is copied to the second tab when I press a command button called Add to Database.
Is it possible to skip the command button altogether and record the data entered in cell D5 and D6 directly in the second tab and clear these cells for the next records? I have no knowledge of programming and I would appreciate as much clarity and specificity as possible.
If more details are needed, please let me know.
You'll need some trigger to cause the copy operation to happen, for example:
Using the 'Change' event, you can trigger some action any time some value is entered into a sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet2.Range(Target.Address).Value = Target.Value
End Sub
This code will copy any data entered into the first sheet into the same cell on the second sheet. 'Target' in this case is the cell where data was entered.
I'm not sure if this what you want though, since you also want to clear the cells after the copy. Can you describe when the copy-and-erase should occur, if not from clicking a button?
Also, your question doesn't seem to match your question's title. Can you clarify a bit what you are trying to accomplish?