我已经阅读了这里发布的问题/答案,但没有找到与我所寻找的相匹配的答案。我对宏相对较新,这对我来说是一个挑战。
我正在构建一个包含多个部分的模板。
我需要什么: 我在此电子表格中依次列出了所有部分,并在其间键入了关键字。例如,其中一个被称为"管理测试"。在本节上面的行中,我输入了" T MaTes"。在本节下面的行中,我输入了" U Fin"。我需要的是一个宏,它将查找第一个单词,找到它,然后查找第二个单词,选择中间的所有行并取消隐藏它们,同时保持隐藏所有其他部分。 然后我将该宏应用于所有其他部分,以便能够隐藏所有其他部分,同时显示一个宏,具体取决于选择哪个选项按钮。
我需要一个基于关键词的宏的原因是因为这些部分可能会被编辑,(行添加/删除等),这意味着任何基于行号隐藏/取消隐藏行的宏都将失败。 / p>
这是我到目前为止所提出的:
Sub ManagementTesting()
Dim 1FirstHit As Long
Dim 1SecondHit As Long
Do While True
1FirstHit = getItemLocation("T*MaTes", Columns(1), , False)
If (1FirstHit = 0) Then Exit Do
1SecondHit = getItemLocation("U*Fin", Range(Cells(lFirstHit + 1, 1), Cells(Rows.Count, 1)), , False)
If (1SecondHit = 0) Then Exit Do
Rows(1FirstHit & ":" & lSecondHit).EntireRow.Hidden = True
EndSub
不确定从哪里开始......非常感谢您的帮助和指导!!!谢谢!!!!
Marta的
答案 0 :(得分:0)
我无法让你的代码工作,但一般的想法是好的(假设我猜测getItemLocation
的做法是对的)
以下代码是一种不同的方法,而不是搜索开始,然后结束,它循环整个文档(将被更改)并且[b] un [/ b]隐藏多个组(或者只是通过编辑SingleInstance
来完成一个。
Sub Main()
Dim Index As Integer: Index = 1 ' Where to start the search from
Dim Found As Boolean: Found = False
Dim SingleInstance As Boolean: SingleInstance = False ' Stop after the first instance
Dim Start As Integer
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets(1) ' The worksheet your data is on
' This will hide groups (multiple) of rows.
' It searches column 1 for keywords "StartGroup" and "EndGroup"
' Eg- Sheet1
' 1: StartGroup
' 2: [Hidden] ExampleData
' 3: [Hidden] ExampleData
' 4: EndGroup
' 5: StartGroup
' 6: [Hidden] ExampleData
' 7: EndGroup
' Running this code with that data will unhide rows 2-3 and rows 6-6 (including the start and end group indicators)
Do
' Test to see if the current iteration indicates the start of a group
If Sheet.Cells(Index, 1).Value2 = "StartGroup" Then
Start = Index ' remember the location
Found = True ' remember that we've found a group
Else
' Skip if we are not currently in a group
If Found = True Then
' Test to see if it is the end of a group
If Sheet.Cells(Index, 1).Value2 = "EndGroup" Then
' unhide the rows
Sheet.Range(Sheet.Rows(Start), Sheet.Rows(Index)).EntireRow.Hidden = False
' reset Found and wait for the next group of EOF
Found = False
If SingleInstance = True Then Exit Do
End If
Else
' I added an EOF indicator just to exit quicker from the loop past my test data
If Sheet.Cells(Index, 1).Value2 = "EndFile" Then Exit Do
End If
End If
' Debugging - Infinite Loops are mean!
If Index < 1000 Then
Index = Index + 1
Else
Exit Do
End If
Loop
End Sub
答案 1 :(得分:0)
我找到了答案。这是我创建的宏。谢谢你的回复!
Sub MaTes()
Rows("8:1000").EntireRow.Hidden = True
Dim Index As Integer: Index = 9
Dim I As Integer
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets(1)
For Index = 1 To 1000
If Sheet.Cells(Index, 1).Value2 = "T*MaTes" Then
I = Index + 1
For I = Index To 1000
If Sheet.Cells(I, 1).Value2 = "U*Fin" Then
Sheet.Range(Sheet.Rows(Index), Sheet.Rows(I)).EntireRow.Hidden = False
Exit For
Else: I = I + 1
End If
Next
Else: Index = Index + 1
End If
Next
For Index = 1 To 1000
If Sheet.Cells(Index, 1).Value2 = "A*Sum" Then
I = Index + 1
For I = Index To 1000
If Sheet.Cells(I, 1).Value2 = "T*Mates" Then
Sheet.Range(Sheet.Rows(Index), Sheet.Rows(I)).EntireRow.Hidden = True
Exit For
Else: I = I + 1
End If
Next
Else: Index = Index + 1
End If
Next
End Sub