VBA:用于循环隐藏不起作用的行
我正在努力使我的VBA代码正常工作。我需要在我的文本中找到12行长的块(见图) 我想隐藏所有不是这些块的行,并用空格分隔这些块。一个块是12行(例如A13181:A13192、A13168:A13179等)。在这些块之间有几行文本,但并不总是这样。数据排序不好,所以所有行数据都在行的第一个单元格中,所以我们应该只在列中查找数据 区块: 第一排:Sopimustunnus ja Hoitokontori 最后一排:Lyhennysitoumuksen pättymispäivä 我的问题是第二个if语句。我在显示块时遇到问题VBA:用于循环隐藏不起作用的行,vba,excel,Vba,Excel,我正在努力使我的VBA代码正常工作。我需要在我的文本中找到12行长的块(见图) 我想隐藏所有不是这些块的行,并用空格分隔这些块。一个块是12行(例如A13181:A13192、A13168:A13179等)。在这些块之间有几行文本,但并不总是这样。数据排序不好,所以所有行数据都在行的第一个单元格中,所以我们应该只在列中查找数据 区块: 第一排:Sopimustunnus ja Hoitokontori 最后一排:Lyhennysitoumuksen pättymispäivä 我的问题是第二个
Public Sub Luotonpurkukorko2()
Application.ScreenUpdating = False
Dim Luottowb As Workbook
Dim Luottosht As Worksheet
Dim i As Long, lastRow As Long
Dim hidRow As Boolean
Set Luottowb = ActiveWorkbook
Set Luottosht = Sheets("Sheet0")
'Finds the last row
lastRow = Luottosht.Cells(Luottosht.Rows.Count, "A").End(xlUp).Row
'Set all rows to visible
Luottosht.Rows.Hidden = False
'Default hidden to TRUE
hidRow = True
For i = 1 To lastRow
If Left(Luottosht.Cells(i, "A").Offset(1, 0), 8) = "Sopimust" Then
hidRow = True
End If
If Left(Luottosht.Cells(i, "A"), 19) = "Lyhennyssitoumuksen" Then
hidRow = False
End If
'Hide/Unhide rows
Luottosht.Rows(i).Hidden = hidRow
Next i
Application.ScreenUpdating = True
End Sub
您可以将循环更改为:
For i = 1 To lastRow
If Left(Luottosht.Cells(i, "A"), 19) = "Lyhennyssitoumuksen" Then
hidRow = False
Else
hidRow = True
End If
Luottosht.Rows(i).Hidden = hidRow
Next i
甚至是一行中的全部:
For i = 1 To lastRow
Luottosht.Rows(i).Hidden = CBool(Left(Luottosht.Cells(i, "A"), 19) <> "Lyhennyssitoumuksen")
Next i
i=1至最后一行的
Luottosht.Rows(i).Hidden=CBool(左(Luottosht.Cells(i,“A”),19)“Lyhennysitoumuksen”)
接下来我
当前代码隐藏除“Lyhennysitoumuksen”行之外的所有内容。要保持12+1空白块可见,可以尝试以下操作:
Public Sub Luotonpurkukorko2()
Application.ScreenUpdating = False
Dim Luottowb As Workbook
Dim Luottosht As Worksheet
Dim i As Long, lastRow As Long
Dim hidRow As Boolean
Set Luottowb = ActiveWorkbook
Set Luottosht = Sheets("Sheet0")
'Finds the last row
lastRow = Luottosht.Cells(Luottosht.Rows.Count, "A").End(xlUp).Row
'Set all rows to visible
Luottosht.Rows.Hidden = False
'Default hidden to TRUE
hidRow = True
For i = 1 To lastRow
restart:
'if statement to check for 12 rows in block
If Left(Luottosht.Cells(i, "A"), 8) = "Sopimust" And Left(Luottosht.Cells(i, "A").Offset(11, 0), 19) = "Lyhennyssitoumuksen" Then
'keeps 12 rows + 1 blank row
i = i + 13
'restart the check incase there are multiple blocks of 12 rows adjacent to eachother
GoTo restart
End If
'Hide/Unhide rows
Luottosht.Rows(i).Hidden = hidRow
Next i
Application.ScreenUpdating = True
End Sub