Macro to Filter Pivot Table - vba

I have a dataset where i want to click on a cell in column A and it filters a pivot table on a different worksheet. I have this code so far:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A2")) Is Nothing Then
Sheets("Labor Detail").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("WBS1").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("WBS1").CurrentPage = Range("A2").Value
End If
End If
End Sub
It does what i want but only for one cell. I want to be able to click on ANY cell in column A and have the cell value filter the pivot table. How do i specify a range instead of just one cell?

Try this instead:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 And Target.Value <> "" Then
Sheets("Labor Detail").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("WBS1").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("WBS1").CurrentPage = Target.Value
End If
End Sub
I got rid of Selection.Count = 1 as I don't think that's really doing anything. Instead I'm testing against Target.Value to make sure you have something to filter your pivot table by.

Related

Select specific cell after Inputting values In previous cells

so I have an input sheet and want to move the activated cell to another cell after data has been typed in/chosen.
So far I've got this:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Range("F11").Value <> "" Then
Range("F13").Select
End If
End Sub
But the problem is this does not seem to work with more cells and ranges for some reason. So for example if then F13 is selected I want to move to F16. If F16 is selected I want to move to F17. So no rule here like always 2 rows down. How can I solve this?
Best
This is a simply type of a boilerplate, which can be increased further, following a specific business logic.
If you want to Add Range("D5") with 3 rows, then add it to the Union in this line: If Intersect(Target, Union(Range("F16"), Range("F13"))) and then make a Case Range("D5").
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row > Rows.Count - 2 Then Exit Sub
If Target = vbNullString Then Exit Sub
If Intersect(Target, Union(Range("F16"), Range("F13"))) Is Nothing Then Exit Sub
Select Case Target
Case Range("F16")
Target.Offset(1).Select
Case Range("F13")
Target.Offset(2).Select
End Select
End Sub

multiple if statement conditions vba

I'm very new to VBA so need a little help. I have a macro (BEM) that is dependent on the value of two cells. I want to be able to run the macro if either of those values is changed. If either of them is blank, I need the code to do nothing until a value is inputted in both cells.
Here's what I have so far but it doesn't seem to work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Or Target.Address = "$B$4" And (IsEmpty(Range("B3").Value) Or IsEmpty(Range("B4").Value)) Then
Exit Sub
Else
BEM
End If
End Sub
The and operator has higher precedence than the or operator therefore your if condition in its current format is interpreted as if:
If Target.Address = "$B$3" Or (Target.Address = "$B$4" And (IsEmpty(Range("B3").Value)) Or IsEmpty(Range("B4").Value)) Then
But you want to goup the or conditions:
If (Target.Address = "$B$3" Or Target.Address = "$B$4") And ((IsEmpty(Range("B3").Value) Or IsEmpty(Range("B4").Value))) Then
When testing for multiple Ranges, you can use If Not Intersect(Target, Range("B3:B4")) Is Nothing.
And instead of checking each Cell if IsEmpty or Not, you can use the WorksheetFunction.CountA for an entire range, in your case your range consists of 2 cells, so you want to check that WorksheetFunction.CountA("YourRange") = 2.
Code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:B4")) Is Nothing And WorksheetFunction.CountA(Range("B3:B4")) = 2 Then
BEM
End If
End Sub

Excel VBA: Hiding columns between two given dates

I'm working on a sheet listing days throughout the year. The rows will be populated manually by different users and the purpose is to log these values throughout a whole year. Columns are specific to each day of the year, and therefore a lot of scrolling is required to find a specific range.
What i want to achieve is to have two cells that can be filled with two dates, a start and end date, and when these are entered all the other columns are hidden from view.
I've found a method to hide columns before a given date in one cell, but id like some help to incorporate a way to also hide columns after a given date in another cell. In this case cell E35
The VBA code for the method so far is:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160725
Dim xCell As Range
If Target.Address <> Range("E34").Address Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In Range("H1:NG1")
xCell.EntireColumn.Hidden = (xCell.Value < Target.Value)
Next
Application.ScreenUpdating = True
End Sub
A reference picture here:
Example
Thank you in advance // R
Following-up on #dgorti answer, in future cases, when you want to monitor the worksheet for multiple ranges, you can use:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E34:E35")) Is Nothing Then
' perform your code hewe
Else
' you can put your Exit Sub here
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160725
Dim xCell As Range
If Target.Address <> Range("E34").Address AND Target.Address <> Range("E35").Address Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In Range("G1:NG1")
xCell.EntireColumn.Hidden = (xCell.Value < Range("E34").Value or xCell.Value > Range("E35").Value )
Next
Application.ScreenUpdating = True
End Sub

VBA Pivot Table One Cell Reference Different Sheet

I'm using this current code to update a pivot table filter based on a cell value (E1) within the same sheet. What i would like to do is to update a filter based on a cell in a sheet named summary. If I set the filed in the current filed equal to the cell in the summary I need to press f2 and enter otherwise it won't work. I'm sure a little bit of tweaking and my code could work for it.
Any tips?
Private Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("E1")
If Target Is Nothing Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Sheets("Tech Pivot Table").PivotTables("PivotTable2").PivotCache.Refresh
With Me.PivotTables("PivotTable2")
.PivotCache.Refresh
.PivotFields("Name").CurrentPage = Target.Value
End With
Application.EnableEvents = True
End Sub
I think the problem is you're changing the value the Target variable up front when you should be checking to see if Target = "E1". Try the code below and let me know if it works.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("E1") Then
If Target Is Nothing Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Sheets("Tech Pivot Table").PivotTables("PivotTable2").PivotCache.Refresh
With Me.PivotTables("PivotTable2")
.PivotCache.Refresh
.PivotFields("Name").CurrentPage = Target.Value
End With
Application.EnableEvents = True
End If
End Sub

VBA - Open a UserForm by clicking anywhere in a specific column

I would like to build a makro in VBA which opens a UserForm when I click in a cell in a specific column, for more details look here.
With this code (from Mr.Burns):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A1")) Is Nothing Then
'name of userform .Show
End If
End If
End Sub
I was able to open the UserForm by clicking in the cell A1, but not by clicking in any cell inside the column A.
I tried to solve this problem with this code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
Dim check As Boolean
check = True
If check Then
Dim i As Long
For i = 1 To 100000
If Not Intersect(Target, Range("A" & i)) Is Nothing Then
UserForm1.Show
check = False
End If
Next
End If
End If
End Sub
It actually works fine, but it is very slow, is there any better possibility to solve this?
To display the form when a cell is selected in column A:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' if target is one cell and in column A
If Target.Columns.count = 1 And Target.Rows.count = 1 And Target.Column = 1 Then
UserForm1.Show
End If
End Sub
You can use .count and .column property together with AND and it will become so much simple and fast. Following code triggers pop-up if u click in column A on active-sheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo errorhandler
If Target.Count = 1 And Target.Column = 1 Then '.count to check if only one cell is selected and .column to check if it is a first column
'UserForm1.Show
'Do whatever you want to do here like opening User form
MsgBox "You clicked in column A"
End If
errorhandler:
End Sub