Lets try to automate it then. Try this in a copy of your workbook (or perhaps in a new workbook to test).
- Put in a few rows of data set up like in rows 2 down below. List the location names from cell D2 across. As you enter rows, mark any locations that store that chemical with an x in their columns.
- Select from A2 to the bottom right of the data you have entered so far then Insert ribbon tab -> Table -> My table has headers -> OK
- Formulas ribbon tab -> Name Manager -> Select the table (probably Table1) -> Edit -> change Name: to LocationData -> OK
- Name cell C1: Select cell C1 -> In the Name box (at the very left of the formula bar row and showing C1) place your cursor and type in Location -> Enter
- Set up Data Validation in C1: Select C1 -> Data ribbon tab -> Data Validation -> Data Validation... -> Allow: List -> Source: =OFFSET(Location,1,1,1,COUNTA(2:2)-3) -> OK (Test by now selecting one of the locations from the drop-down in C1)
- Enter the formula shown in B1. Where I have used column AZ in the formula, it needs to be further across than your table will ever go. If 37 locations, AZ should do.
- Enter the formula shown in C3. It should auto-populate to the other rows in the table.
.....................................................
- Right click the sheet name tab and choose "View Code".
- Copy and Paste the code below into the main right hand pane that opens at the previous step.
- Close the Visual Basic window & test by selecting another Location in C1. To dispaly all rows in the table, just delete any value out of C1
- Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).
If you add more locations and/or chemicals, the table should automatically expand to accommodate them.
If you don't want to have a macro-enabled work book, you could stop at the dotted line and then manually filter/unfilter the location column using the drop-down in cell C2.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Location")) Is Nothing Then
With ActiveSheet.ListObjects("LocationData").Range
If IsEmpty(Range("Location").Value) Then
.AutoFilter Field:=3
Else
.AutoFilter Field:=3, Criteria1:="x"
End If
End With
End If
End Sub
Excel Workbook |
---|
|
---|
| A | B | C | D | E | F | G | H | I |
---|
1 | | 2 | Houma | | | | | | |
---|
2 | Name | Type | Location | Harvey | Houma | Loc 3 | Loc 4 | Loc 5 | |
---|
3 | Chem 1 | | x | x | x | | x | | |
---|
4 | Chem 2 | | x | | x | | | | |
---|
5 | Chem 3 | | | x | | x | | x | |
---|
6 | Chem 4 | | | x | | | | | |
---|
7 | Chem 5 | | x | | x | | | | |
---|
8 | Chem 6 | | | | | | | x | |
---|
9 | | | | | | | | | |
---|
|
---|
</td></tr></table></td></tr></table> <table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#0000ff;background-color:#ffFCF9; color:#000000; "><tr><td ><b>Data Validation in Spreadsheet</b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Allow</td><td >Datas</td><td >Input 1</td><td >Input 2</td></tr><tr><td >C1</td><td >List</td><td > </td><td >=OFFSET(Location,1,1,1,COUNTA(2:2)-3)</td><td > </td></tr></table></td></tr><tr><td ><table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#0000ff;background-color:#fffcf9; color:#000000; "><tr><td ><b>Names in Formulas </b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Name</td><td >Applies to</td></tr><tr><td >C1</td><td >Location</td><td >='Chemical Locations'!$C$1</td></tr></table></td></tr></table></td></tr></table>