根据If .Found = True结果执行多个操作

时间:2020-05-05 11:08:19

标签: excel vba ms-word

我想知道如何根据If .Found = True结果执行所有动作? 在If .Found = True之后的以下宏代码段中,仅执行了一项操作: If .Found = True Then mySheet.Cells(x, 5) = mySheet.Range("AD17")

我需要写4条不同的行来执行If .Found = True

     If Application.WorksheetFunction.CountA(mySheet.Range("A12:D15")) = 2 And _
              mySheet.Range("E12") = "????" And _
              mySheet.Range("H12") = "k" Then
                rng.SetRange Start:=startPos, End:=endPos
                  With rng.Find
                   .Text = arr(0) & " " & arr(wrdCount1 - 1) & " oświadcza ponadto, że jest wdową."
                   .MatchWildcards = False
                   .MatchCase = False
                   .Forward = True
                   .Execute
                     If .Found = True Then mySheet.Cells(x, 5) = mySheet.Range("AD17")   
                                 'how to insert more actions after IF .Found here without repeating If statement;
                 End With
     ElseIf Application.WorksheetFunction.CountA(mySheet.Range("A12:D15")) = 2 And _
           mySheet.Range("E12") = "????" And _
           mySheet.Range("H12") = "m" Then
               rng.SetRange Start:=startPos, End:=endPos
               With rng.Find
                  .Text = arr(0) & " " & arr(wrdCount1 - 1) & " oświadcza ponadto, że jest wdowcem."
                  .MatchWildcards = False
                  .MatchCase = False
                  .Forward = True
                  .Execute
                   If .Found = True Then
                       mySheet.Cells(x, 5) = mySheet.Range("AD18")
                   Else
                       rng.SetRange Start:=startPos, End:=endPos
                       With rng.Find
                           .Text = "a przedmiotowego nabycia dokona do majątku osobistego za pieniądze pochodzące z jego majątku osobistego,"
                           .MatchWildcards = False
                           .MatchCase = False
                           .Forward = True
                           .Execute
                       If .Found = True Then mySheet.Cells(x, 6) = mySheet.Range("AE14")  
                                   'how to insert more actions after IF .Found here without repeating If statement;
                       End With
                   End If
             End With
     End If

我要做的是根据If .Found = True的结果填充3或4个不同的单元格,而不必在每一行中重复If .Found = True Then

我正在寻找一些精美的代码。

     If .Found = True Then mySheet.Cells(x, 6) = mySheet.Range("AE14") And _
                       mySheet.Cells(x, 7) = "red" And _
                       mySheet.Cells(x, 8) = "100 000" And _
                       mySheet.Cells(x, 10) = mySheet.Range("AE25") * 0,23

1 个答案:

答案 0 :(得分:2)

简单就可以了

If .Found = True Then 
          mySheet.Cells(x, 6) = mySheet.Range("AE14") 
          mySheet.Cells(x, 7) = "red" 
          mySheet.Cells(x, 8) = "100 000" 
          mySheet.Cells(x, 10) = mySheet.Range("AE25") * 0,23
End if