How to hide columns based on name in Excel 2010? - vba

I am trying to hide columns based on name using VBA inside Excel 2010. Each of my columns have a product version and some results below it. The product version does repeat throughout the spreadsheet since I have it categorized by OS. Thus, I'm hiding multiple columns based on selection, like a filter would do. If I could hide based on the name and not the column letter (A,B,C,...), then adding columns in between in the future would prevent more code changes on the location of those columns.
What I'm currently doing right now is fixed to the column letter. This limits me in the sense that I cannot add columns in between without having to change the code (column letter). Example:
`If productver_2dot5.Value = True Then
Columns("E").Hidden = False
Columns("M").Hidden = False
Columns("AC").Hidden = False
Columns("AT").Hidden = False
Columns("BD").Hidden = False
Columns("BR").Hidden = False
Else
Columns("E").Hidden = True
Columns("M").Hidden = True
Columns("AC").Hidden = True
Columns("AT").Hidden = True
Columns("BD").Hidden = True
Columns("BR").Hidden = True
End If`
What I would like to do is to hide any columns that contains the name 'Product Ver 2" (example) in one of its cells.

Sub HideBlahs()
Dim col As Range
For Each col In ActiveSheet.UsedRange.Columns
If Application.CountIf(col, "blah") > 0 Then
col.EntireColumn.Hidden = True
End If
Next col
End Sub
FYI your posted code reduces to:
Range("E1,M1,AC1,AT1,BD1,BR1").EntireColumn.Hidden = Not productver_2dot5.Value

Related

Can I use IsEmpty to refer to a different sheet and hide a column?

Is it possible to use IsEmpty to refer to a cell on a different sheet from where the macro is being fired from? Also, is it possible to hide the queried column if the result of that query is True?
Here's what I've built so far:
My first version looked like this:
If IsEmpty(L1) Then
Columns("L").EntireColumn.Hidden = True
Else
Columns("L").EntireColumn.Hidden = False
End If
Straightforward enough. But, that only works if it's fired from the worksheet where I want the query/hide to occur. When I launch the macro from the different sheet, it hides the column in that sheet (of course, duh).
So, after several iterations and errors, I got to this:
If IsEmpty(Sheets("Results").Cells(10, 1).Value) Then
Worksheets("Results").Columns(10).EntireColumn.Hidden = True
Else
Worksheets("Results").Columns(10).EntireColumn.Hidden = False
End If
Which at least doesn't throw any errors from the VBA. It also does a grand total of squat. :$ I'm starting to wonder if it's even possible to use IsEmpty on a different sheet? Or the EntireColumn.Hidden command? Also, given that I need to run this check on 9 columns, maybe there's a better way than 9 If/Then statements?
To get away from a loop through 9 columns' row 1, use SpecialCells(xlCellTypeBlanks).
dim blnks as range
with workSheets("Results")
with .range(.cells(1, "B"), .cells(1, "K"))
.entirecolumn.hidden = false
set blnks = .specialcells(xlCellTypeBlanks)
if not blnks is nothing then blnks.entirecolumn.hidden = true
end with
end with
Essentially this unhides all 9 columns then hides the columns with blank cells in the first row. Note that a zero-length string (e.g. "") returned by a formula is not the same thing as a truly blank cell.
I think you're very close, just you have the cells inputs the wrong way around:
If IsEmpty(Sheets("Results").Cells(1, 10).Value) Then
Worksheets("Results").Columns(10).EntireColumn.Hidden = True
Else
Worksheets("Results").Columns(10).EntireColumn.Hidden = False
End If
Additionally as mentioned in the comments you can create a loop to check many columns:
Dim i As Integer
Dim maxi As Integer
i = 1
maxi = 20
While i < maxi
If IsEmpty(ThisWorkbook.Worksheets("Results").Cells(1, i)) Then
Worksheets("Results").Columns(i).EntireColumn.Hidden = True
Else
Worksheets("Results").Columns(i).EntireColumn.Hidden = False
End If
i = i + 1
Wend

VBA single select on a Pivot Table field filter

I am working on my VBA code for a PivotTable field.
What I want to achieve is to only select Acc Payable in field Group. The following code can help me get what I want, but I'm considering whether there is a way to delete those False lines and make the code shorter?
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Group")
.PivotItems("Acc Services").Visible = False
.PivotItems("FRG").Visible = False
.PivotItems("Non FinOps").Visible = False
.PivotItems("Semi Auto").Visible = False
.PivotItems("Acc Payable").Visible = True
End With
End With
You can use a For loop, to iterate through the PivotField named "Group" PivotItems, and if the PivotItem.Name = "Acc Payable" then make it visible.
Code
Dim PvtItm As PivotItem
For Each PvtItm In ActiveSheet.PivotTables("PivotTable1").PivotFields("Group").PivotItems
If PvtItm.Name = "Acc Payable" Then
PvtItm.Visible = True
Else
PvtItm.Visible = False
End If
Next PvtItm

VBA Excel Double If

I have various dropdown menus in the document and need to create a double conditional statement.
So I am trying to do the following:
If Cell B14 = Option 1 Then unhide Cells B16:B17
If Cells B17 = Yes Then hide Cells B19:B53
If Cells B17 = No Then show Cells B19:B34
So if someone picks Option 1 from the dropdown menu then there is another drop down menu that appear and if they select Yes from the second on it hide the required cells and if they pick No it unhides the required cells.
Options 2 & 3 in the first dropdown menu do not need to show the second drop down box.
I have it all working apart from the double if.
Current code is:
If Target.Address = "$B$14" Then
If Range("B14") = "Option 1: Travel Home" Then
ActiveSheet.Rows("16:35").EntireRow.Hidden = False
ActiveSheet.Rows("36:55").EntireRow.Hidden = True
ElseIf Range("B14") = "Option 2: Travel to next city" Then
ActiveSheet.Rows("15").EntireRow.Hidden = False
ActiveSheet.Rows("16:17").EntireRow.Hidden = True
ActiveSheet.Rows("19:35").EntireRow.Hidden = True
ActiveSheet.Rows("36").EntireRow.Hidden = False
ActiveSheet.Rows("37:55").EntireRow.Hidden = True
ElseIf Range("B14") = "Option 3: Make own arrangements" Then
ActiveSheet.Rows("15:36").EntireRow.Hidden = True
ActiveSheet.Rows("39:55").EntireRow.Hidden = False
End If
End If
Option 2 and 3 are working OK just option 1 isn't working to unhide/hide whats necessary and also then need to do the second dropdown option which the alters what is shown.
I tested the below code and it appears to be behaving as you desire. I did make some seemingly significant changes to the structure, but I did so in hopes of making it both easier to read and maintain going forward. I think my edits are self-explanatory.
I also took the liberty to assume this is a Worksheet_Change event even though you did not explicity state as such.
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case Is = "$B$14"
Select Case Right(Left(Target.Value, 8), 1)
Case Is = 1 'Option 1
Me.Rows("16:17").EntireRow.Hidden = False
Case Is = 2 'Option 2
Me.Rows("15").EntireRow.Hidden = False
Me.Rows("16:17").EntireRow.Hidden = True
Me.Rows("19:35").EntireRow.Hidden = True
Me.Rows("36").EntireRow.Hidden = False
Me.Rows("37:55").EntireRow.Hidden = True
Case Is = 3 'Option 3
Me.Rows("15:36").EntireRow.Hidden = True
Me.Rows("39:55").EntireRow.Hidden = False
Case Else
Me.Rows("15:55").EntireRow.Hidden = True
End Select
Case Is = "$B$17"
Me.Range("B19:B35").EntireRow.Hidden = Not (Target.Value = "No")
End Select
End Sub
Here's the code to make sure all rows are hidden when the workbook is opened.
Private Sub Workbook_Open()
With Me.Worksheets("SheetName") ' change as needed
.Rows("1:3").EntireRow.Hidden = True
.Rows("15:55").EntireRow.Hidden = True
End With
End Sub

excel macro vba Filtering using wildcards

I need to filter in pivot.
I need to deselect all items starting with "A." and "H."
and keep everything else selected.
the items ranges from:
A.(3-13 characters)
B.(3-13 characters)
all the way to
Z.(3-13 characters)
the raw data also changes from 50-500 rows (I can have a data with only 50 rows today - then tomorrow I may have over 500)
my current code works: (by entering all possible items which appears on that column - roughly over 300 items) its long but it works.
lately I've been getting more items adding to that list, and I know we can use wild cards.
[MY CURRENT CODE]
ActiveSheet.PivotTables("PivotTable1").PivotFields("column").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("column"). _
EnableMultiplePageItems = False
With ActiveSheet.PivotTables("PivotTable1").PivotFields("column")
.PivotItems(" PLACE ITEM HERE ").Visible = False
.PivotItems
.PivotItems
.PivotItems [repeat over 300 times - changing the " PLACE ITEM HERE " with the items on the column]
End With
I can cut down the 300+ lines to about 5-10 lines only, I was thinking about something like (below) it would also fix my problem of having new items that are not yet on my list:
ActiveSheet.PivotTables("PivotTable1").PivotFields("column").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("column"). _
EnableMultiplePageItems = False
With ActiveSheet.PivotTables("PivotTable1").PivotFields("column")
.PivotItems("A.*").Visible = False
.PivotItems("H.*").Visible = False
End With
but this is not working
Try this:
With ActiveCell.PivotTable.PivotFields("Column")
For i = 1 To .PivotItems.Count
If .PivotItems(i).Name like "A.*" or .PivotItems(i).Name like "H.*" Then
.PivotItems(i).Visible = True
else
.PivotItems(i).Visible = False
End If
Next i
End With

Hide Controls in MS Access Based on Value Field

I am trying to hide some controls in a form in MS Access. The idea is that a linked table has a type of question such as OpenResponse or OptionBox. Given this input I want to switch the type of input the user can input. Here is a sample of what I have:
Private Sub QuestionType_AfterUpdate()
Dim QType As String
Set QType = Me.QuestionType.Value
Select Case QType
Case OpenResponse
Forms("Survey").Controls(AnswerField).Visible = True
Forms("Survey").Controls(OptionTitle).Visible = False
Forms("Survey").Controls(OptionFrame).Visible = False
Forms("Survey").Controls(Option69).Visible = False
Forms("Survey").Controls(Option70).Visible = False
Forms("Survey").Controls(Option71).Visible = False
Forms("Survey").Controls(Option72).Visible = False
Forms("Survey").Controls(Option73).Visible = False
Forms("Survey").Controls(Option74).Visible = False
Forms("Survey").Controls(Option75).Visible = False
Forms("Survey").Controls(Option76).Visible = False
Forms("Survey").Controls(Option77).Visible = False
Forms("Survey").Controls(Option78).Visible = False
Case OptionBox
Forms("Survey").Controls(AnswerField).Visible = False
Forms("Survey").Controls(OptionTitle).Visible = True
Forms("Survey").Controls(OptionFrame).Visible = True
Forms("Survey").Controls(Option69).Visible = True
Forms("Survey").Controls(Option70).Visible = True
Forms("Survey").Controls(Option71).Visible = True
Forms("Survey").Controls(Option72).Visible = True
Forms("Survey").Controls(Option73).Visible = True
Forms("Survey").Controls(Option74).Visible = True
Forms("Survey").Controls(Option75).Visible = True
Forms("Survey").Controls(Option76).Visible = True
Forms("Survey").Controls(Option77).Visible = True
Forms("Survey").Controls(Option78).Visible = True
End Select
End Sub
You're not thinking "out of the box", here. In this type of environment, thinking "inside the box" can ruin your day.
This is a perfect example of when to use a table-driven approach. The primary question here is; Can/Will the questions ever change? The probable answer is, Yes. In this case, I would do the something like this:
Create a table called Questions. In that table, list the questions (possibly in a Memo field, but a Text field may work), the question type, the control name and the value (True or False). Then, you can query the Questions table based on question type, and loop through the resulting dataset to set the Visible property of your controls. This way, you can always add more questions later if necessary. In fact, you might also add a Primary Key called QuestionID, and a True/False field called Active so you can go back and remove some of the questions without creating orphaned records, and query only questions marked as Active.
If I'm misunderstanding your purpose, let me know and I'll try to edit my answer accordingly.