I have an Excel spreadsheet that has tabs with questions to fill out.
I counted the columns to make sure I had the right one and looped through the code using the debugger step by step to see the values it read out. The values all appeared to be in order.
The contents are (mostly) confidential and it is too much to empty so I will attempt to show you this way:
This shows the values. I am working with the "basis" column. The letter of the corresponding column is Y.
I want to hide or show questions based on the value of the cell. 0 means hide and 1 means show.
I have found that the code hides and unhides at will.
Sub CheckRoleQuestionsSimplified()
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Cockpit" Or ws.Name = "I-CBO" Or ws.Name = "config" Or ws.Name = "BegeleidingsFormulier" Or ws.Name = "Inhoudsopgave" Or ws.Name = "Saldibalans" Then
' Sla deze over, doe niks
Else
Worksheets(ws.Name).Activate
End If
Next ws
beginRow = 10
endRow = 46
checkCol = 25
Do While rowNum < endRow
If Cells(rowNum, checkCol).Value = 0 Then
Cells(rowNum, checkCol).EntireRow.Hidden = True
Else
If Cells(rowNum, checkCol).Value = 1 Then
Cells(rowNum, checkCol).EntireRow.Hidden = False
End If
End If
Next rowNum
End Sub
Use With statement. and use isEmpty function.
Sub CheckRoleQuestionSimplified()
Dim Ws As Worksheet
Dim Target As Range
Dim rowNum As Integer, endRow As Integer, checkCol As Integer
For Each Ws In ThisWorkbook.Worksheets
Select Case Ws.Name
Case "Cockpit", "I-CBO", "config"
Case Else
With Ws
beginRow = 10
endRow = 46
checkCol = 25
For rowNum = beginRow To endRow
Set Target = .Cells(rowNum, checkCol)
Target.EntireRow.Hidden = False
If Not IsEmpty(Target) Then '<~~ check Empty
If Target.Value = 0 Then
Target.EntireRow.Hidden = True
Else
If Target.Value = 1 Then
Target.EntireRow.Hidden = False
End If
End If
End If
Next rowNum
End With
End Select
Next Ws
End Sub
This should work:
Dim ws As Worksheet
Dim beginRow As Long, endRow As Long, checkCol As Long
beginRow = 10
endRow = 46
checkCol = 25
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Cockpit" Or ws.Name = "I-CBO" Or ws.Name = "config" Or ws.Name = "BegeleidingsFormulier" Or ws.Name = "Inhoudsopgave" Or ws.Name = "Saldibalans" Then
' do nothing
Else
For rowNum = beginRow To endRow Step 2
With ws.Cells(rowNum, checkCol)
.Resize(2, 1).EntireRow.Hidden = .Value = 0
End With
Next rowNum
End If
Next ws
This process relies upon the 0
or 1
only being in even numbered rows, as per your screenshot.
Note: if you want all empty rows hidden, use the following loop instead:
For rowNum = beginRow To endRow
With ws.Cells(rowNum, checkCol)
.EntireRow.Hidden = .Value = 0
End With
Next rowNum
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.