我正在尝试使我的VBA代码正常工作。我需要在文本中找到12行长的块(参见图片)。
我想隐藏所有不是这些块的行,并用空格分隔这些块。一块是12行(例如A13181:A13192,A13168:A13179等)。在这些块之间是几行文本,但并非总是如此。数据排序不好,因此所有行数据都位于该行的第一个单元格中,因此我们只应在A列中查找数据。
阻止:
第一行:Sopimustunnus ja hoitokonttori ...
最后一行:Lyhennyssitoumuksenpäättymispäivä...
https://pasteboard.co/HqA9kxI.jpg
我的问题是第二个if语句。我在使块可见时遇到问题
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
答案 0 :(得分:1)
您可以将循环更改为此:
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
答案 1 :(得分:0)
您当前的代码将隐藏“ Lyhennyssitoumuksen”行以外的所有内容。要使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