If Range EntireRow.Hidden - vba

I would really appreciate your help with following issue I am facing.
I want 3 rows to be hidden unless something is written in a specific cell.
If something is written in the specific cell I want the next row to become unhidden. And so it suppose to continue with the next two cells.
I have written the following code in th module, please guide me on how to solve this (what am I doing wrong?)
Sub InsertRow()
If Range("U6") <> "" Then
Rows("7").EntireRow.Hidden = False
Rows("8:9").EntireRow.Hidden = True
End If
End Sub

Not sure if this is what you want, but this goes through all used cells in col U and checks rows beneath
Public Sub InsertRow()
Dim targetCol As Range, itm As Range
Set targetCol = Worksheets("Sheet1").UsedRange.Columns(21) 'UsedRange starts at A1
Application.ScreenUpdating = False 'Update sheet name and column number
For Each itm In targetCol.Cells
itm.Offset(1).EntireRow.Hidden = (Len(itm.Value2) = 0)
Next
Application.ScreenUpdating = True
End Sub

Try this solution:
Sub InsertRow()
Rows("7").EntireRow.Hidden = Range("U6") = ""
Rows("8:9").EntireRow.Hidden = True
End Sub

Related

If condition associated with applying a column filter

I'm trying to use an if-condition regarding filters. I wish I could write a code to check if a specific filter is applied and then do something... (of course)
My first attempt was:
If ActiveSheet.Range("$D$4:$Q$20").AutoFilter Field:=2 then
Rows("22:22").Select
Selection.EntireRow.Hidden = True
End If
In the very first line, VBA doesn't accept the condition that is written...
Any guess?
tks
This will tell you if a range is in a filter and the filter is active:
Public Function IsFilterOn(rng As Range) As Boolean
Dim ws As Worksheet
Dim iFilterNum As Integer
Set ws = rng.Parent
If Not ws.AutoFilter Is Nothing Then
If Not Application.Intersect(rng, ws.AutoFilter.Range) Is Nothing Then
With ws.AutoFilter.Filters
iFilterNum = rng.Column - .Item(1).Parent.Range.Column + 1
If iFilterNum <= .Count Then
IsFilterOn = .Item(iFilterNum).On
Exit Function
End If
End With
End If
End If
End Function
Note that you don't need to select a row before hiding it.
Sub HideWhenFiltered()
With ActiveSheet
If .AutoFilterMode Then
If .AutoFilter.Filters(2).On Then
.Rows(22).Hidden = True
End If
End If
End With
End Sub

Finding a cell based on the header of a section of data, then selecting the last row of that section

I am attempting to find the text of a header row based on the value of a cell relative to the cell that is clicked in. The way I have attempted to do this is follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim var1 As Variant
Dim var2 As Variant
Dim var3 As Variant
Dim FormName As String
FormName = "New Form"
Static NewFormCell As Range
Application.ScreenUpdating = False
If Not Intersect(Target, Range("G16:X80")) Is Nothing Then
If Target.Cells.Count = 1 Then
var1 = Cells(Target.Row, 2).Value
var2 = Cells(15, Target.Column).Value
If Not (IsEmpty(var1)) And Not (IsEmpty(var2)) And var2 <> "+" And Target.Interior.ColorIndex <> 2 And Target.Borders(xlEdgeLeft).LineStyle <> xlNone Then
If IsEmpty(Target) Then
Target.Value = "X"
Target.HorizontalAlignment = xlCenter
Target.VerticalAlignment = xlCenter
Target.Font.Bold = True
Dim Header As Range
Set Header = Range("A54:E160").Find(var2, LookIn:=xlValues)
Header.Offset(1, 1).End(xlDown).EntireRow.Select
Dim CopyCell As Range
'Header.End(xlDown).EntireRow.Insert
'Set CopyCell = Header.End(xlDown). [offset?]
'CopyCell.Value = var1
Else
Target.ClearContents
End If
Else
Exit Sub
End If
End If
End If
Application.ScreenUpdating = True
End Sub
The issue is VBA is throwing Run-Time Error 91 ("Object variable or With block variable not set"). It then highlights the last row in that section of code. Since I set that variable in the previous line, I'm not sure why I'm receiving this error or if I'm even going about this the right way.
Any input would be greatly appreciated!
EDIT: I cleared the above issue by searching over a wider range. The cell I wanted to select was merged, but I still assumed the value was stored within column A. But this code still isn't quite doing what I'd like it to:
I want to select the last row in the section (not the last row of data in the sheet, but the last contiguous data in column B), but right now my code is jumping me all the way to the bottom of the sheet.
The problem is that your .Find isn't finding the value. In this case, you can add some code to handle that.
...
Dim Header As Range
Set Header = Range("A59:A159").Find(var2, LookIn:=xlFormulas)
If Header Is Nothing Then
' There's no value found, so do something...
msgbox(var2 & " was not found in the range, will exit sub now."
Exit Sub
End If
MsgBox Header
...
...of course there are myriad ways/things you can do to handle this. If you still want to execute other code, then wrap everything in an If Header is Nothing Then // 'do something // Else // 'other code // End IF type thing.
It really just depends on what you want to do. Again, your error is being caused by the fact that the var2 isn't being found, so just find other things to do in that case.

Hide a column if corresponding cell "is blank"/"formula is null" upon sheet's activation

I am new to VBAing but have a small amount of javascript knowledge and hoping for a bit of guidance/help as I can't seem to get the code working. There's probably some glaring mistakes so thanks for the patience.
I'm trying to get a spreadsheet to update a sheet upon that sheet's activation, where it will hide columns whose 2nd row cell is "blank" (or the formula x(y) in the cell is null).
Below is what have tried so far.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "FSM" Then
Function hideBlankColumns()
i = 2
For i = 2 To 30
If IsNull(Cells(2, i).Value) = True Then
Columns(i).EntireRow.Hidden = True
Else: Columns(i).EntireRow.Hidden = False
End If
Next i
End Function
End If
End Sub
Thanks in advance!
You have three errors in your code:
you cannot define a function inside another sub.
you are talking about hiding/unhiding columns, but in the code you hide rows: Columns(lCol).EntireRow.Hidden = True
function IsNull always returns False if you pass Excel cell or its value as a parameter. In this case you should use function IsEmpty instead.
This code should work properly:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim i As Integer
If Sh.Name = "FSM" Then
For i = 2 To 30
Columns(i).EntireColumn.Hidden = IsEmpty(Cells(2, i).value)
Next i
End If
End Sub
Try something like this.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "FSM" Then
lCol = 2
Do While lCol <= ws.UsedRange.Columns.count
If IsNull(Cells(2, lCol).Value) = True Then
Columns(lCol).EntireColumn.Hidden = True
Else
Columns(lCol).EntireColumn.Hidden = False
End If
lCol = lCol + 1
Loop
End if
End sub

speed up Excel off-sheet dependents search

I've incorporated the off-sheet dependents search using the "ShowDependents" and "NavigateArrow" VBA methods. Everything works well but it is just painfully slow (for a large number of dependents).
Are there alternatives, way to speed it up? I've tried disabling the ScreenUpdating but that doesn't speed it up by much.
This is what my code is based on: http://www.technicana.com/vba-for-checking-dependencies-on-another-sheet
Consider the following function which is supposed to return true if the cell you pass it has a direct dependent on a different sheet:
Function LeadsOut(c As Range) As Boolean
Application.ScreenUpdating = False
Dim i As Long, target As Range
Dim ws As Worksheet
Set ws = ActiveSheet
c.ShowDependents
On Error GoTo return_false
i = 1
Do While True
Set target = c.NavigateArrow(False, i)
If c.Parent.Name <> target.Parent.Name Then
ws.Select
ActiveSheet.ClearArrows
Application.ScreenUpdating = True
LeadsOut = True
Exit Function
End If
i = i + 1
Loop
return_false:
LeadsOut = False
ActiveSheet.ClearArrows
Application.ScreenUpdating = True
End Function
Sub test()
MsgBox LeadsOut(Selection)
End Sub
To test it, I linked the test sub to a command button on Sheet1.
In A2 I entered the formula = A1 + 1, with no other formulas on Sheet1.
On Sheet2 I entered the formula =Sheet1!A2.
Back on Sheet1, if I select A2 and invoke the sub it almost instantly pops up "True". But if I select A1 and invoke the sub it returns "False" -- but only after a delay of several seconds.
To debug it, I put a Debug.Print i right before i = i + 1 in the loop. The Immediate Window, after running it again, looks like:
32764
32765
32766
32767
Weird!!!!!
I was utterly stumped until I replaced Debug.Print i by
Debug.Print target.Address(External:=True)
Which led to output that looks ends like:
[dependents.xlsm]Sheet1!$A$1
[dependents.xlsm]Sheet1!$A$1
[dependents.xlsm]Sheet1!$A$1
[dependents.xlsm]Sheet1!$A$1
NavigateArrow(False,i) goes back to the originating cell and stays there once i exceeds the number of dependents! This is seemingly undocumented and massively annoying. The code you linked to was written by someone who hasn't discovered this. As a kludge, you should check that when you are navigating arrows you haven't returned to the starting point. The following seems to work almost instantly in all cases, although I haven't tested it very much:
Function LeadsOut(c As Range) As Boolean
Application.ScreenUpdating = False
Dim i As Long, target As Range
Dim ws As Worksheet
Set ws = ActiveSheet
c.ShowDependents
On Error GoTo return_false
i = 1
Do While True
Set target = c.NavigateArrow(False, i)
If target.Address(External:=True) = c.Address(External:=True) Then
GoTo return_false
End If
If c.Parent.Name <> target.Parent.Name Then
ws.Select
ActiveSheet.ClearArrows
Application.ScreenUpdating = True
LeadsOut = True
Exit Function
End If
i = i + 1
Loop
return_false:
LeadsOut = False
ActiveSheet.ClearArrows
Application.ScreenUpdating = True
End Function
The key lines are the three lines which begin
If target.Address(External:=True) = c.Address(External:=True)
Adding some such check in the sub you linked to should make a massive difference.

Dynamically protecting cells in a row based on "Y" Value in the Checked column?

I need to protect all the cells in a particular row if my user enters Y (yes) into a column of that particular row which indicates that the user has reviewed the data and that it is correct. I have not been able to figure out how to make this happen. Does anyone know how to do this?
Thanks so much,
Elias
As per your request and Byron's comments, I edited the code. The code should be pasted into the worksheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error GoTo Exiter
Set Sh = Target.Parent
If Target.Value = "Y" And Target.Column = 1 Then
Unprotect Password:="WHATEVER"
For Each curRow In Sh.UsedRange.Rows
If Sh.Cells(curRow.Row, 1) = "Y" Then
Sh.Cells(curRow.Row, 1).EntireRow.Locked = True
Else
Sh.Cells(curRow.Row, 1).EntireRow.Locked = False
End If
Next
Sh.Protect Password:="WHATEVER"
End If
Exiter:
Application.EnableEvents = True
End Sub