Filter Table VBA - vba

I am doing my project and I am encountering some problems. Moreover, my scope was to use VBA as a primary software to do my project.
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!!
[1]http://imgur.com/GNGyh91 [2]http://imgur.com/uuh2Y1u
Update: I have tried recording Macros
Sub Filter()
'
' Filter Macro
Range("B9:BR38").Select
ActiveWindow.ScrollRow = 32
ActiveWindow.ScrollRow = 5
ActiveWindow.ScrollRow = 1
Selection.AutoFilter
ActiveSheet.Range("$B$9:$BR$38").AutoFilter Field:=1, Criteria1:="e"
End Sub
Update 2: Instead of recording Macros, i decided to use a Textbox to filter my table data. However, i realised that my table is not filtering according to what i type in the Text Box.
Private Sub TextBox1_Change()
Dim Text
Text = TextBox1.Value
If Text <> "" Then
Sheet2.Range("A5:AV26").AutoFilter
Field = 1
criteria = "text,_"
visibledropdown = False
Else
Sheet2.AutoFilterMode = False
End If
End Sub

You need to keep the parameters on the same line separated by commas and it is Criteria1, not criteria. If you want to hide the drop-down arrows, you need to do each individually. The code below only hides the dropdown for the field being filtered. If course, if you are only filtering one field then you could just use Sheet2.Range("A5:A26") since filtering goes across all columns.
If True Then
Sheet2.Range("A5:AV26").AutoFilter Field:=1, Criteria1:="text,_", VisibleDropDown:=False
Else
If Sheet2.AutoFilterMode Then Sheet2.AutoFilterMode = False
End If
See Range.AutoFilter Method for more information.

Related

Macro to auto fill text in a column down to the last cell in one table (while there are additional tables below)

example of tables in the spreadsheet
Hi I am new to VBA and wanted to create a macro that can auto fill column "Y/N" (as shown in the attached pic) with the text "Yes" by clicking a button.
Right now I have 3 rows in the first table, which is fine and I used the following code:
Sub autofill1()
Range("E12").Value = "Yes"
Range("E12").Select
Selection.AutoFill Destination:=Range("E12:E14")
End Sub
However, the rows in the first table will keep changing, say it becomes 20, 50, 100 depending on different cases. I wonder if there's a way for me to define the auto fill range to be cell E12 to the last cell in this table, not to extend to the rest of the tables below.
I checked the codes provided for other similar questions - looks like it will auto fill all the way down to the last cell in my worksheet, which is the last cell in this column of the 5th table. And I don't want that. Is there a way to only auto fill down to the last cell of the 1st table? Thanks so much in advance!
In my opinion, adding "yes" until found a bottom border.
Sub autofill1()
'Range("E12").Value = "Yes"
'Range("E12").Select
'Selection.AutoFill Destination:=Range("E12:E14")
Dim i As Long
i = 11
Do While Range("E" & i).Borders(xlEdgeBottom).LineStyle _
= xlNone
i = i + 1
Cells(i, 5) = "Yes"
Loop
End Sub

I want to hide a column in excel using VBA macro in run time but while executing other columns also get selected.

I want to hide column C based on value in cell AB7.
If value in cell AB7 is 117 then entire column C needs to hide.
Else I want column C to be there.
If Range("AB7").Value = "117" Then
Columns("C:C").Select
Selection.EntireColumn.Hidden = True
Else
Columns("C:C").EntireColumn.Hidden = False
End If
When I run the code columns A:G also get selected. Please let me know if there are any limitations while using the above code. I use excel 2010. Thanks in advance.
I would recommend not using select. Columns("C:C").Hidden = False and Columns("C:C").Hidden = True

Excel 2010: how to auto-refresh auto-filter based on checkbox cell link

In Excel2010 I have created a table of X items on rows and features of these items on Y columns. One of the features is the Application Area and I want to filter the items based on the application area type. The difficulty is the items on the table have multiple application areas, so I cannot use the simple filter function of excel.
I came up with an idea to have n check-boxes (3 for this example) and assign the output of each check-box to a different cell, e.g. A10, B10, C10. I assign relevant values to OR functions for each item in an extra column, e.g. column F. So, for example, when I check the application area A the items 1 and 3 have TRUE in the column F and I can use this column to filter the table by selecting TRUE in the auto filter function. See the picture for better understanding.
The problem is that the auto filter function does not refresh automatically after I change the status of a check box. Every time I have to reapply the filter in data ribbon. Online I found this piece of VBA code to have an auto refresh feature in the active sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 8 Then
ActiveSheet.AutoFilter.ApplyFilter
End If
End Sub
Now, this code does work and the filter is reapplied, if I actively go to one of the cells in the F column and edit the cell to TRUE or FALSE manually. If I change a cell value in the F using the check box, the auto refresh does not work.
What is the difference between changing a cell value by typing the value and pressing ENTER and using a check box?
BTW, the check box is a FORM CONTROLS type, not ACTIVEX.
Any help would highly be appreciated.
Thanks!
the following will have Worksheet_Change react as if you manually changed any sheet cell
assuming:
your Form checkboxes names are "A", "B" and "C"
they are assigned macros named, respectively, "A_Click", "B_Click", "C_Click"
they are in the same sheet as the table one
the table name is "Features"
then place the following code in any module:
Option Explicit
Sub A_Click()
SetOrs
End Sub
Sub B_Click()
SetOrs
End Sub
Sub C_Click()
SetOrs
End Sub
Sub SetOrs()
Dim i As Long
Dim A As Boolean, B As Boolean, C As Boolean
Dim appAreaStrng As String
With ActiveSheet
A = .Shapes("A").ControlFormat.Value = xlOn '<~~ get CheckBox "A" value
B = .Shapes("B").ControlFormat.Value = xlOn '<~~ get CheckBox "B" value
C = .Shapes("C").ControlFormat.Value = xlOn '<~~ get CheckBox "C" value
With .ListObjects("Features")
For i = 1 To .ListRows.Count
appAreaStrng = .DataBodyRange.Cells(i, .ListColumns("App Area").Index) '<~~ get current row "App Area" value
.DataBodyRange.Cells(i, .ListColumns("OR").Index) = (A And InStr(appAreaStrng, "A") > 0) Or (B And InStr(appAreaStrng, "B") > 0) Or (C And InStr(appAreaStrng, "C") > 0)
Next i
End With
End With
End Sub

dynamic table references with Excel VBA

I have an Excel table called "groups" where the headers are group names. In the column below the group name are the members of that specific group.
In another table I have all member names in the first column and the group names in the first row. And I want to check for each member name whether it is in the group above.
To check whether Tim is in the group "guys" I could use the formula:
=if( countif( groups[guys];"Tim")>0;"yes";"no")
But I have a command button "add group" which opens a userform where I can enter a new group + members. The name of the new group is entered into textbox1. The group and member are added to the groups table.
I want the 2nd table to update as well. What I would like to do is something like this:
With Worksheets("Overview").ListObjects("PersonIsInGroupTab")
.ListColumns.Add Position:=3
.HeaderRowRange(3).Value = TextBox1.Value
.DataBodyRange(1, 3).formula= "=if( countif( groups[textbox1.value];""Tim"")>0;""yes"",""no"")"
End With
Unfortunately, this doesn't work.
textbox1.value is the name of the new group (also added to the groups tab).
Is there a way I can make this work?
EDIT:
I tried another approach that didn't work either but might help answering the question.
In the table that checks whether a certain person is a member of a group I start by default with one group. And manually enter the formula
=if(countif(groups[guys],"Tim")>0,"yes","no")
into the cell (in my case J4). When I add a group "gals" using the userform I get a second column "gals" in my table. When I manually drag the formula from the [guys] cell to the [gals] cell (K4) it works just as I want it to. And I get the formula
=if(countif(groups[gals],"Tim)>0,"yes","no")
in the gals cell (K4)
So I figured I could just do this autofill in VBA as part of the "add group" routine. This is the code I used:
Range("groups[guys]").Select
Selection.AutoFill Destination:=Range("J4:K4"), Type:=xlFillDefault
I got this code from recording the autofill as a macro. But when I run it the formula in K4 is just as in J4, with groups[guys] instead of groups[gals].
I cant do Destination:=Range("groups[[guys]:[gals]]") as I want it to be working with any groupname not only gals.
I was able to fix the problem by writing a VBA function instead of using countif.
The function is:
Function IsInGroup(group, member As String) As Boolean
For Each Item In group
If Item = member Then
IsInGroup = True
Exit Function
End If
Next Item
IsInGroup = False
End Function
And I used it like this:
temparray = ActiveSheet.ListObjects("groups").ListColumns(2).Range
With Worksheets("Overview").ListObjects("PersonIsInGroupTab")
tempmember = .DataBodyRange(1, 1).Value
.ListColumns.Add Position:=3
.HeaderRowRange(3).Value = TextBox1.Value
.DataBodyRange(1, 3).Value = IsInGroup(temparray, tempmember)
End With

Advanced Filter with multiple criteria selected for 1 row

Sheet1 contains my data which also becomes my filtered data as i have set filter in place rather than copying the range.
The criteria range on the spreadsheet is populated by a UserForm command button, which also applies the advanced filter.
The criteria range of 2 of the columns within this filter are populated from a list box with the MultiSelectMulti function enabled.
I want to be able to select multiple items from these 2 lists boxes to filter for. I have tried the following and it populated the cells i assigned as it should. Although the filter i believe is trying to find all the values i have assigned in one row, not for each individually so there-fore not displaying anything.
I am pretty new at using VBA and have read some posts about using Unique:=True after the code for the criteria range. I don't know how to use this so if someone could explain that would be great.
'SEARCH CRITERIA - JOB STATUS
If ListBox1.Selected(0) = True Then Range("BK2") = "WON"
If ListBox1.Selected(1) = True Then Range("BL2") = "PENDING"
If ListBox1.Selected(2) = True Then Range("BM2") = "LOST"
'SEARCH CRITERIA - WIN PERCENTAGE
If ListBox2.Selected(0) = True Then Range("BN2").Value = "100%"
If ListBox2.Selected(1) = True Then Range("BO2").Value = "90%"
If ListBox2.Selected(2) = True Then Range("BP2").Value = "80%"
If ListBox2.Selected(3) = True Then Range("BQ2").Value = "70%"
If ListBox2.Selected(4) = True Then Range("BR2").Value = "60% OR LESS"
'APPLY ADVANCED FILTER USING SELECTED CRITERIA
Range("A6:BD99999").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("BH1:BR2")
'BH1:BJ2 CONTAINS MY OTHER CRITERIA
I already have the sheet set to unprotect prior the code and after the code (as well as my other selections on the user form which work fine). I have tried using 'OR' and 'Else:' to no avail.
Any suggestions would be greatly appreciated on how i can solve my issue to filter the above when selecting multiple items without me having to create extra columns for each criteria in the data as i will have to move loads of conditional formatting manually and it will create too much clutter on my already large sheet.
in short, the AdvancedFilter filtering criteria requires to:
spread filter values between rows to achieve some "OR" condition
keep filter values in the same row to achieve some "AND" condition
Not so sure about your actual filtering needs, but my first guess is that you need something like follows (explanations in comments, and I'm assuming that filtering criteria are contained in listboxes items themselves):
Option Explicit
Private Sub CommandButton1_Click()
Dim iSel As Long, iRow As Long
Intersect(Range("BH1:BR1").EntireColumn, ActiveSheet.UsedRange).Offset(1).ClearContents ' clear any existing filtering criteria
With ListBox1 'reference ListBox1
For iSel = 0 To 2 'loop through its items from first to third (note you can use 'For iSel = 0 To .Count-1' to loop through all its items)
If .Selected(iSel) Then ' if current item is selected
iRow = iRow + 1 'update filter range row to achieve "OR" condition
Cells(1 + iRow, "BK") = .List(iSel) 'write current referenced listbox value in criteria range cell in a row by its own
End If
Next
End With
With ListBox2
For iSel = 0 To 4
If .Selected(iSel) Then
iRow = iRow + 1
Cells(1 + iRow, "BN") = .List(iSel)
End If
Next
End With
Range("A6:BD99999").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("BH1:BR1").Resize(iRow + 1) ' size criteria range to fit the filtering values read from listboxes
End Sub
in such a way you filter A:BD rows that match any filter value between those selected in the two listboxes, i.e. filtered record will have:
any selected value from ListBox1 in column referenced by "BH1" cell value
or
any selected value from ListBox2 in column referenced by "BN1" cell value
should that be what you need, than you can remove columns BL to BM and BO to BR from criteria range (and therefore adjust all BN references to BL)
finally I'd recommend you to use explicit worksheet reference instead of implicitly relying (as your code currently does) on the ActiveSheet