Table Filtering drop down list Excel VBA - vba

I am currently doing my final year project and it make use of excel VBA whereby I am bad at it. I am really hope someone could guide me through the problem that I am facing.
As for now, I am hoping that I can use a drop down list to filter my table whereby it only shows the particular department and weeks I want to show.
From the image below,
From the range("C7:L26"), whenever I filter cell(F2) or cell(J2), it will leave the data that I want from the dropdown list.
For example, if cell(F2) = 2 and cell J2 = e,
From range("C7,L26"), it will only show department with value "e" and have week 2 in it. As for cells that does not have the department value or week value, it will be cleared or blank.
I also hope that if it is possible to press a button to return the table back to default.
Do guide me and I really need your help!! Thank You
[1]http://imgur.com/GNGyh91
[2]http://imgur.com/uuh2Y1u
As for now I have this as my codes that I've learnt from a user #PeterT
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:AW28")) Is Nothing Then
Dim legendWS As Worksheet
Dim legendcell As Range
Set legendWS = ThisWorkbook.Sheets("Legend")
Set legendcell = legendWS.Range("A2:A18").Find(Target.Value)
If Not legendcell Is Nothing Then
Target.Interior.Color = legendcell.Interior.Color
End If
End If
End Sub
End Sub

Related

VBA - Select Top of Matrix

I'm using a "worksheet_selectionChange" event to fire a macro whenever a cells within certain ranges are selected.
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Cells.Offset(-???,0).Value
Case "LABEL_1"
Tenor = "2W"
Call MyLameMacro()
Case....
End Select
End Sub
Those ranges look like little matricies:
If the user selects any of the cells underneath label, I want VBA to lookup whatever the label is at the top. Using offset would work if I knew exactly how many rows up to Label, but its not constant....
Is there another way to go about that?
Thanks - KC
Barring further information about the layout ... you can use formatting to build your own search algorithm. This will slow down if it has to go through thousands of lines (find another way if your data set is that large).
You'll have to replace "labelColor" and "notLabel" with the background color of the label rows. This is assuming the above picture is accurate, and "Label" is highlighted. To find the value to replace them with, simply select the cell in question, then type "debug.print selection.interior.color" into the immediate window in VBA.
By placing the label value and the origin address in parentheses after your lame macro, you can preserve those values in the macro.
I did not test this. In order to do so I would have to guess at the setup of your workbook ... but an approximation of this code should work for you.
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
i = 0
searchLoop:
If i > 100 Then
MsgBox ("Infinite loop. Click OK to stop")
Exit Sub
End If
If Target.Offset([-i], 0).Interior.Color = labelColor Then Call MyLameMacro(Target.Offset([-i], 0), Target.address)
If Target.Offset([-i], 0).Interior.Color = notLabel Then
If Target.Offset([-i], 0).Value = "Value" Then Call MyLameMacro(Target.Offset([-i], [-1]).Value, Target.address)
i = i + 1
GoTo searchLoop
End If
End Sub

Trying to use VBA to filter my Pivot Table based on Year and Period values in cells D2 and E2

I am trying to filter my Pivot Table using VBA so it will filter based on a Period and a Year in cells D2 and E2 respectively. I have the below code which is to filter by the year and works without having the Fiscal Period in the Columns but when I put the Fiscal Period in the columns it debugs at the Field.ClearAllFilters and Field.CurrentPage = Year lines. Can someone please help me find a fix for this?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Worksheets("CY PT").Range("E2")) Is Nothing Then Exit Sub
Dim PT As PivotTable
Dim Field As PivotField
Dim Year As String
Set PT = Worksheets("CY PT").PivotTables("CY PT")
Set Field = PT.PivotFields("Fiscal Year")
Year = Worksheets("CY PT").Range("E2").Value
With PT
Field.ClearAllFilters
Field.CurrentPage = Year
PT.RefreshTable
End With
End Sub
The .CurrentPage method only applies to PageFields. Sounds like you are trying to filter RowFields. So your options are:
Change your fields to be PageFields; or
Use a Slicer; or
Use a PageField filter dropdown mascarading as a Data Validation
dropdown (like I outline at
http://dailydoseofexcel.com/archives/2014/08/16/sync-pivots-from-dropdown/
); or
Iterate through the PivotItems collection for each field, and set the
.Visible status of everything but the selected items to FALSE.
If you choose the last option, you need to be aware this can take a while. See the code I posted at Filtering a Pivot Table Based on Variable Range to get an understanding of how to do this efficiently. Also give my post at http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/ a read.

Conditional Highlighting VBA based on Date

I made this workbook that works sort of like a program. It takes orders, puts it in a masterlist on a separate sheet, then plots it on a calendar. Using VBA, I want my code to autohighlight the newly /modified input orders. Right now I have to double click the cell to highlight it because Excel doesn't recognize formula changes as a modification. I want to add a time range condition too - when the order is due within 14 days I want the highlight to be red, but 14 days or more is still yellow. Right now my code goes like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("B9:AE53")) Is Nothing Then
For Each c In Intersect(Target, Range("B9:AE53"))
Target.Interior.Color = vbYellow
Next c
End If
End Sub
Is it doable? How do I modify my code?
Same workbook, different issue
How to do this using formulas:
1) set the cells to be in DATE format
2) write a code to show current date
3) end date - current date = number of days.
4) conditional formatting. if the number of days is greater than 14 it would be yellow, else it would turn red.
All these can be done automatically.

How to have a macro search for a value in an offset cell on one sheet in another sheet

this is my first question so please bear with me if i'm doing anything wrong.
Quick back storey... on sheet "Purchase" in column C i want the item name of what has been purchased, in column D i want the quantity and in column G i want the location of where the item is going. For example i want 10 boxes of tiles delivered to the office. C2=Tiles, D2=10, G2=Office
When the location of an item is entered into column G(cell change) i want the macro to offset to the same row in column C and then search for the value in column A on sheet "Office", i currently have this for searching if a cell changes;
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
keycells = "g2:g9999"
If Application.Intersect(ActiveCell, Range(keycells)) = "Office" Then MsgBox "hello"
End Sub
This does work, but it also checks if all other cells change, which i dont want it to do this. In place of "msgbox "hello" " i want to put the script for it to offset to the same row in column C look at the value and then search for this value in column A on sheet "Office".
If it is found i need the macro to add the newly purchased quantity to the current quantity on sheet "Office" (this will be column B), if it is not found i need it add the information from columns C & D on sheet "Purchase" to columns A & B
Also i will need to add more locations to the script and will give these locations a sheet in the workbook over time
If this is possible i will be extremely gratefull for all help provided
The Worksheet_Change event fires on all cell changes.
To prevent your code running unless the cell is in column G, you could restrict it like this:
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
Dim iSect
If target.Column = 7 Then
keycells = "g2:g9999"
Set iSect = Application.Intersect(target, Range(keycells))
If Not iSect Is Nothing Then
If lcase(target) = "office" Then
MsgBox "hello"
' add code here to copy the data
End If
End If
End If
End Sub
However, a better solution might be to use formulas (such as an array formula or using INDEX and MATCH) in the target worksheets to extract the data - this will avoid using a macro entirely.

Excel Drop Down Box with Formula

Hi I have an excel with a drop down box whose list has 3 cells. One of these cells contain a formula. The problem is this formula is dependent on data in another cell and when this data changes the calculated value changes. The value is automatically update in the list where it was chosen from but I will manually have to go back to the drop down box and change it. How can I have the value be updated automatically. Willing to look at a VBA solution if need be
Put the following into the worksheet module. It assumes that the cell with validation applied is G9, and the second option of the list is the formula.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "G9" Then
If Target.HasFormula Then Exit Sub 'or else infinite loop
Dim ListRange As Range
Dim FoundIdx As Variant
Set ListRange = Me.Evaluate(Me.Range("G9").Validation.Formula1)
FoundIdx = Application.Match(Target.Value, ListRange, 0)
If Not IsError(FoundIdx) Then
If FoundIdx = 2 Then
Target.Formula = ListRange(2).Formula
End If
End If
End If
End Sub
Note that this will not work if the formula might have the same value as any of the other options!
I couldn't reproduce your issue. Here is what I did:
Populate 2 cells with a random value and a third cell with a formula (columns B3, B4, B5)
Define a Name Range with these 3 cells called it Options
Create a drop down using Insert/Form Controls/Combo Box
Set the input range of the drop down to Options
Change the cells value to get the formula give different results and the new values are reflected on the Options list and the Drop Down.
Is this what you are doing?