我有一个Excel电子表格,其中包含带有要填写的问题的标签。
我对列进行了计数,以确保我拥有正确的列,并使用调试器逐步遍历代码以查看其读出的值。这些值似乎都是按顺序排列的。
(大多数情况下)内容是机密的,要清空该内容太多了,因此我将尝试通过这种方式向您显示:
我想根据单元格的值隐藏或显示问题。 0表示隐藏,1表示显示。
我发现代码可以随意隐藏和取消隐藏。
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
答案 0 :(得分:1)
使用With语句。并使用isEmpty函数。
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
答案 1 :(得分:0)
这应该有效:
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
此过程取决于0
或1
仅(根据您的屏幕截图)排在偶数行中。
注意:如果要隐藏 all 个空行,请改用以下循环:
For rowNum = beginRow To endRow
With ws.Cells(rowNum, checkCol)
.EntireRow.Hidden = .Value = 0
End With
Next rowNum