2017-01-23 57 views
1

我已经通过阅读题所有其他行/答案贴在这里并没有找到一个相当匹配什么,我找之间取消隐藏单元格。我对宏来说比较新,这对我来说是一个挑战。功能按钮:值和隐藏

我建立一个包含几个部分的模板。

我需要什么: 我列出所有的章节之一,另一个在此电子表格后,在输入之间的关键字。例如,其中之一被称为“管理测试”。在本节上方的一行中,我输入了“T MaTes”。在本节下面的行中,我输入“U Fin”。我需要的是一个宏,它将查找第一个单词,找到它,然后查找第二个单词,选择它们之间的所有行,并取消隐藏它们,同时隐藏其他所有部分。 然后,我将是宏观适用于所有其他部分能够在时间显示一个同时隐藏所有其它的,这取决于哪个选项按钮被选择上。

我需要一个基于关键词的宏的原因是因为这些部分可能会在某些时候被编辑(行添加/删除等),这意味着任何基于行编号来隐藏/取消隐藏行的宏都会失败。

这是我想出迄今:

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 

不知道在哪里把它从这里...您的帮助和指导,将是非常赞赏!谢谢!!!!

玛塔

+0

不是一个解决方案,但你不能用数字启动变量名。你能否包含'getItemLocation'函数的代码? – NickSlash

回答

0

我不能让你的代码工作,但总的想法是好的(假设我什么getItemLocation所做的是正确的猜测),而不是

下面的代码是不同的方法,搜索的开始,接着结束时,它循环在整个文件(这将被改变),并且可以并[b]未[/ 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 
0

我找到了答案。这是我创建的宏。谢谢Nickfor你的回应!

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