vba中的多个条件while while循环

时间:2017-02-27 20:42:30

标签: vba excel-vba excel

我有一份报告,可以是5到数百页根据实验室测试提供患者信息的页面。每个患者记录由下划线分隔,末尾有一个复选框。 " ___________ | ____ |"例如:

LastName,第一个患者编号单位ANC 02 #### 2/23/2017 15:56  血液    GLU-ANC 416 mg / dL H * REf。 RAnge:60-100关键50-399 评论:出现患者详细信息和医生注意事项 在这里,可以非常灵活......            更多临床信息等 _________________________________________________________________________ | ____ | LastName,First Patient Number Unit CH 1234 2/23/2017 15:56  血清    TROP I 54 mg / dL H * REf。 RAnge:60-100关键50-399 评论:出现患者详细信息和医生注意事项 在这里,可以非常灵活......            更多临床信息等 _________________________________________________________________________ | ____ | 姓氏,第一个患者编号单位ANC 1234 2/23/2017 15:56  屎   适合积极的H * REF。 RAnge:" NEG" - "" 60-100关键:报告 评论:出现患者详细信息和医生注意事项 在这里,可以非常灵活......            更多临床信息等 _________________________________________________________________________ | ____ |

如果我在患者记录中找到某些值,例如ANC或FIT,我可以在查看打印报告时跳过这些记录。所以我想用| SKIP |。

替换下划线复选框

我可以通过下一个循环轻松地做到这一点,但问题是我不知道有多少" ARTERIAL BLOOD"或者" ANC"或者" FECES FIT"这将显示在给定的报告上。所以我一直试图弄清楚如何用Do While Selection.Find.Execute = True语句替换我的For Next循环。

但是尝试了很多变化并且无法正确使用语法。 任何帮助将不胜感激!

这是我的工作For Next Loop

Sub xSimplefind()
Dim vtext As String
   vtext = InputBox("Text to find") 'Here I enter 1st search value ie. "ANC"
  For i = 1 To 7
  With Selection.Find
  .Execute FindText:=vtext
  Selection.MoveRight Unit:=wdCharacter, Count:=1
 End With
'Once I find that first item, I then look for the very next "|_____|"
  With Selection.Find
  .Execute FindText:="|____|"  'TypeText overwrites Selected text.
  Selection.TypeText Text:="|SKIP|"
 End With
Next i
End Sub 

2 个答案:

答案 0 :(得分:0)

这不完全符合您的需要,但有一个例子可以帮助您走上正轨:

Option Explicit

Sub MultiFind()
    Dim valueToSkip() As String
    valueToSkip = Split("ANC,FIT,ARTERIAL BLOOD,FECES FIT", Delimiter:=",")

    Dim found As Boolean
    Dim value As Variant
    Dim result As Variant

    found = False
    For Each value In valueToSkip
        With Selection.Find
            Set result = .Execute(FindText:=value)
            If Not result Is Nothing Then
                found = True
                Exit For
            End If
        End With
    Next value

    If found Then
        '--- add |SKIP| here
    End If

End Sub

答案 1 :(得分:0)

如果其他人需要做这样的事情,这段代码就有效...... 感谢他人的投入和帮助!! = = = = =找到的解决方案= = = = = = = = = = 子测试()      Dim vtext As Variant      对于每个vtext In Array(“Arterial”,“ANC”,“FIT”,“PREVIOUS”)          Selection.HomeKey单位:= wdStory

     '
     'Inner loop to check every line
     '
     While True
         Selection.Find.ClearFormatting
         Selection.Find.Replacement.ClearFormatting
         With Selection.Find
             .Text = vtext
             .Replacement.Text = ""
             .Forward = True
             .Wrap = wdFindContinue
             .Format = False
             .MatchCase = False
             .MatchWholeWord = False
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
         End With
         Selection.Find.Execute
         With Selection.Find
             .Text = "|"
             .Replacement.Text = ""
             .Forward = True
             .Wrap = wdFindContinue
             .Format = False
             .MatchCase = False
             .MatchWholeWord = False
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
         End With
         Selection.Find.Execute
         Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
         If Selection.Text <> "|SKIP|" Then
             Selection.Text = "|SKIP|"
         Else
             GoTo DoNextWord
         End If
     Wend

DoNextWord:      下一个vtext  结束子