Excel Insert Rows - Using Named Ranges To Hide Rows - excel

I have hidden rows with checkbox macros
Every time I want to insert a new row, the vba code gets messed up and the code assigned to hide rows below will not work
is there a way to dynamically have the rows change the code in the vba
So I don't have to redo all the row values and check boxes that move in the process
[IMG]http://i39.tinypic.com/2akek5z.jpg[/IMG]
Sub CheckBox1_Click()
If Range("B3").Value = True Then
Rows("4:18").EntireRow.Hidden = False
Else
Rows("4:18").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B51").Value = True Then
Rows("52:66").EntireRow.Hidden = False
Else
Rows("52:66").EntireRow.Hidden = True
End If
End Sub
I have about 10 subcheckboxes in the code
If I insert or delete a row the code below will not work.
This is because the rows have changed
from the original code that I wrote

Edit
Use 'Named Ranges' instead of hard coding the range. The named range will automatically update when you insert a new row. For example:
I selected rows 9 thru 13 and named them 'RowsABC' as shown here:
I also named my checkbox cell as 'CheckABC' so that it is no longer dependent on what row it's in.
Your code now looks like this:
If Range("CheckABC").Value = True Then
Range("RowsABC").EntireRow.Hidden = False
Else
Range("RowsABC").EntireRow.Hidden = True
End If
Now you can select a row such as row 10 and right click -> insert, and your macro will hide rows 9:14 instead of 9:13 because the named range will update automatically when you insert.

Related

Ho do you hide / highlight rows based on the value cells

I am new to excel and want to programmatically do two things.
Hide entire row if Code = 0
Set background and border to all cells in rows if columns I - O = 0
Any help would be appreciated. Thanks in advance.
Place the following code in a new module (VBA Editor, Insert->Module).
Public Sub ToggleRows()
Dim Sheet As Worksheet
Dim Row As Long
Set Sheet = ThisWorkbook.Worksheets("Sheet1") ' Replace with the name of your worksheet
' Get Row Number of last Row
Row = Sheet.UsedRange.Rows.Count + Sheet.UsedRange.Row - 1
Application.ScreenUpdating = False
While Row > 1 ' (exclude header row)
' Hide/Unhide Row depending on value in Column A
Sheet.Rows(Row).EntireRow.Hidden = IIf(Sheet.Cells(Row, "A").Value = 0, True, False)
Row = Row - 1
Wend
Application.ScreenUpdating = True
End Sub
After copying it you should have a "ToggleRows" macro in the list when you press the Macros button in the Developer tab.
If you wanted the macro to run automatically, you need to add a few more lines of code.
Open the code module for your worksheet and put the following
Private Sub Worksheet_Calculate()
ToggleRows
End Sub
For the second part of your question, you should be able to use Conditional Formatting.
There are many ways you can do it, one way is:
Select Columns I->O and select New Rule from the conditional formatting menu, and then select Use a formula to determine which cells to format
Enter a formula (example below) and then specify the formatting.
=IF(SUM(INDIRECT("I"&ROW()&":O"&ROW()))=0,TRUE,FALSE)
If Sum of Cells I-O equals 0, apply the formatting, otherwise dont.

Automatically Update a Pivot Table Based Based on Cell Reference

I have a pivot table with the following columns:
item_number, job_number, description and qty.
I would like to have VBA code take the input of a cell, A5, to automatically sort through the job_number column of my pivot table and find the entries that have the job_number found within A5.
The data set is very large, so it is not a wise idea to sort through using PivotItems.
I have a file that does exactly that. I have a cell which isn't part of the pivot table but acts like a filter field for the pivot table.
I put a script into the Worksheet_Change event to watch that cell for changes and a sub which swaps out the filter on the pivot table to match the cell value.
The Worksheet_Change event is basically like
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5")) Is Nothing Then Call FilterChanger
End Sub
And the FilterChanger sub is basically like
Private Sub FilterChanger()
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim FilterText as String
FilterText = Me.Range("A5")
With Me.PivotTables("MyPivotTable")
.ManualUpdate = True
.PivotFields(3).ClearAllFilters
.PivotFields(3).PivotFilters.Add Type:=xlCaptionContains, Value1:="zzzzzzzzzzzz"
'Filter out all results to prevent table updating when clearing filters
.PivotFields(5).ClearAllFilters
'now that there is another filter, there isnt any lag while the table tries to load all the unfiltered data
.PivotFields(5).PivotFilters.Add Type:=xlCaptionEquals, Value1:=FilterText, Value2:=FilterText
.PivotFields(5).ShowDetail = True
.PivotFields(3).ClearAllFilters
.ManualUpdate = False
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
In this code PivotFields(3) is any pivot row field that is in the table other than the one you intend to filter. and PivotFields(5) is the field that I am targetting with my filter. You can put column names instead of a number when referring to your fields, but I just prefer numbers.
In my file, the source data for the pivot table is 20k rows and growing, so I found that whenever I did .ClearAllFilters the workbook would pause for ~10 seconds while the pivot table attempted to load all 20k rows. To fix that issue, I put in a temporary filter in another field which will prevent any rows from loading while I swap out the filter in the intended field. I chose zzzzzzzzzzzz in PivotField(3) because it will always return 0 rows.
Sub Macro1()
' in A1 there is the value for sorting
' if you want to show the whole table again run only the "select all" part (line 2):
' Field value == Column (right now column 2); Criterial == Filter Value (value of cell A1) ; change Table name to yours
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2, Criteria1:=Range("A1").Value
' "Select All -- to show whole table again de-comment following line
'ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2
End Sub
Hope this is what you are looking for.

Select Row in Excel with VBA if complete

I want to select each row that is complete. So each row where there is no empty cell.
The selected row should then be exported to a sharepoint list.
so far the code I tried works but exports the whole table. I want to add a line that selects and exports only the rows that have no blank cells.
I had something like the following in mind. But I don't know how to select the complete rows from the entire table instead of specifying the cells.
If Cells().Value not isnull then
Range().EntireColumn.Select
Sub ExportToSharePoint ()
ActiveSheet.ListObjects("Table1").Publish Array("https://sharepoint...", "Name of the List")_, True
ActiveCell.Select
End Sub
You can loop through your table and get the completed rows using the following
Option Explicit
Public Sub ExportToSharePoint()
Dim r
With ActiveSheet.ListObjects("Table1")
For Each r In .DataBodyRange.Rows
If WorksheetFunction.CountA(.Parent.Range(r.Address)) = .ListColumns.Count Then
' Rest of your code here
End If
Next r
End With
End Sub

Select multiple values from a slicer

I have a pivot table with a slicer. The slicer has 50+ values in it and these values can change. I use the slicer to filter the data - I am always looking for the same 5 values, and my 5 values start with the same text string.
I had recorded a macro to select my 5 values. this macro selects the items that are true and then lists all the other values and sets selection as false.
When new values are added the macro crashes because I don't have an explicit line of code to set the selection of the new value to false.
I found code to select one value from the slicer and deselect all other values without having to list them all explicitly, but I can't find code to find the 5 values and deselect all other values without having to list them explicitly, or
a way to modify this code to select all slicer items that "contains" the consistent text string. either would help...
this is the code I have to find one specific value, but deselect the rest without stating them explicitly:
For Each slcCache In ActiveWorkbook.SlicerCaches
slcCache.ClearManualFilter
Next
With ActiveWorkbook.SlicerCaches("Slicer_Fruit")
For Each oSlicerItem In .SlicerItems
If oSlicerItem.Name = "abcx Apple" Then
oSlicerItem.Selected = True
Else
oSlicerItem.Selected = False
End If
Next oSlicerItem
End With
How can I change the code to select the slicer items that starts with "abcx" or contains this text?
or is there a way to select "abcx Apple" and "abcx Pear" and "abcx Banana" but not select any other values?
Try this :
Sub Slicer_select()
ActiveWorkbook.SlicerCaches("Slicer_Fruit").ClearManualFilter
Dim Sl_I As SlicerItem
For Each Sl_I In ActiveWorkbook.SlicerCaches("Slicer_Fruit").SlicerItems
If Not Sl_I.Value Like "abcx*" Then Sl_I.Selected = False
Next
End Sub

Formula to hide rows based on the value of a cell

I have a worksheet that contains the names of all managers and their employees, ideally the way this sheet needs to work is that there is a drop down in the top left and when a manager selects their name, all rows that don't have their name against, are hidden, so only their team is shown.
I know auto filtering and having them choose their name would be the easiest way and is a good option to fall back on, but I'm hoping there is a way to do this with VBA or a formula to just hide rows when its not their team when they select their name in the drop down. As i'm trying to create something that's quite slick and looks nice
I did try to do something around having a helper cell to display true and false if the names matched, but came a bit stuck at this point. Tried using the code below, but it doesn't seem to actually be doing anything. Column with TRUE/FALSE is in Col A
Sub TEST()
Dim cell As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each cell In Range("A4:A34")
If cell.Value = "FALSE" Then
cell.EntireRow.Hidden = True
Else
cell.EntireRow.Hidden = False
End If
Next
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Any ideas on how to do this without just using autofilter would be great
Given the following assumptions:
Drop down with Manager name is in cell A1
Column listing manager name for each row is in column A
Data set starts in row 5
Column A is contiguous with no blank spaces
Place the following code into the Worksheet module of the data sheet and change assumptions to fit your data set.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" and Target.Cells.Count = 1 Then
Application.ScreenUpdating = False
Range("A5:A1000").EntireRow.Hidden = False
Dim mgrList as Range
Set mgrList = Range(Range("A5"),Range("A5").End(xlDown))
Dim mgrCheck as Range
For each mgrCheck in mgrList
mgrCheck.EntireRow.Hidden = mgrCheck <> Target
Next
End If
End Sub
Use an if then else statement with a call that shows/hides the rows that you'd like to show.
If Range("A1").Value = "John Snow" Then
Call Show_John_Snow
Else
If Range("A1").Value = "Daenerys Targaryen" Then
Call Show_Daenerys
Else....
'subroutine
Show_John_Snow
Rows("17:20").EntireRow.Hidden = True 'hide others
Rows("21:53").EntireRow.Hidden = False 'show John Snow
Rows("54:75").EntireRow.Hidden = True 'hide others
I have this data, in which I have Headers at A3:D3, data starting from row 4 to 99.
I tried applying Autofilter, check if this one works for you.
Sub test()
Range("A3:D3").Select
Selection.AutoFilter
ActiveSheet.Range("A3:D99").AutoFilter Field:=2, Criteria1:="0"
ActiveSheet.Range("A3:D99").AutoFilter Field:=2, Criteria1:="1"
End Sub
Here, I selected option named "0" from drop-down filter from Field-2, that is Range A4, and as you told, other cells automatically gets hidden, and the cells corresponding to that criteria only gets visible.
Also I tried with other option "1".
This seems a very difficult or involved way to do this, I have to show students their results without them seeing other students results.
So, one sheet has all the data and on a "front" sheet I call the relevant data for the particular student using index() and match(). Each student has an ID number which is entered, then for confirmation the name is returned then the relevant grades.

Resources