即使有多个条件,在满足一个条件后循环停止

时间:2018-01-31 22:29:24

标签: excel vba excel-vba loops exit-code

即使在符合条件的范围内有更多的代码,下面的代码也会在满足条件后“退出”。我该如何纠正?

<nedisCatalogue>
   <headerInfo>
    <feedVersion>1-0</feedVersion>
    <dateCreated>2018-01-22T23:37:01+0100</dateCreated>
    <supplier>Nedis_BENED</supplier>
    <locale>nl_BE</locale>
   </headerInfo>
   <productList>
    <product>
     <nedisPartnr><![CDATA[VS-150/63BA]]></nedisPartnr>
     <nedisArtlid>17005</nedisArtlid>
     <vendorPartnr><![CDATA[TONFREQ-ELKOS / BIPOL 150, 5390]]></vendorPartnr>
     <brand><![CDATA[Visaton]]></brand>
     <EAN>4007540053905</EAN>
     <intrastatCode>8532220000</intrastatCode>
     <UNSPSC>52161514</UNSPSC>
     <headerText><![CDATA[Crossover Foil capacitor]]></headerText>
     <internetText><![CDATA[Bipolaire elco met een ruwe folie en een zeer goede prijs/kwaliteits-verhouding voor de bouw van cross-overs. 63 Vdc, 10% tolerantie.]]></internetText>
     <generalText><![CDATA[Dimensions 16 x 35 mm    
    ]]></generalText>
  <images>
   <image type="2" order="15">767736.JPG</image>
  </images>
  <attachments>
  </attachments>
  <categories>
   <tree name="Internet_Tree_ISHP">
    <entry depth="001" id="1067858"><![CDATA[Audio]]></entry>
    <entry depth="002" id="1067945"><![CDATA[Speakers]]></entry>
    <entry depth="003" id="1068470"><![CDATA[Accessoires]]></entry>
   </tree>
  </categories>
  <properties>
   <property id="360" multiplierID="" unitID="" valueID=""><![CDATA[...]]></property>
  </properties>
     <status>
      <code status="NORMAL"></code>
     </status>
     <packaging quantity="1" weight="8"></packaging>
     <introductionDate>2015-10-26</introductionDate>
     <serialnumberKeeping>N</serialnumberKeeping>
     <priceLevels>
          <normalPricing from="2017-02-13" to="2018-01-23">
       <price level="1" moq="1" currency="EUR">2.48</price>
      </normalPricing>
      <specialOfferPricing></specialOfferPricing>
     <goingPriceInclVAT currency="EUR" quantity="1">3.99</goingPriceInclVAT>
     </priceLevels>
     <tax>
     </tax>
     <stock>
      <inStockLocal>25</inStockLocal>
      <inStockCentral>25</inStockCentral>
      <ATP>
       <nextExpectedStockDateLocal></nextExpectedStockDateLocal>
       <nextExpectedStockDateCentral></nextExpectedStockDateCentral>
      </ATP>
     </stock>
    </product>
  ....
</nedisCatalogue>

2 个答案:

答案 0 :(得分:1)

回应K.Davis评论,不知道你为什么要退出?

我对您的代码进行了一些修改。错误处理不包括在内,但代码应完成循环并告诉您是否找不到任何内容。

Private Sub cmdAdd_Click()
    On Error GoTo Whoa
    Dim LastRow As Long, i As Long
    LastRow = ActiveSheet.Range(Me.txtTaskCol.Value & Rows.Count).End(xlUp).Row

'Set a counter so you can message Task Not Found
    Dim matchCounter as Integer
    matchCounter = 0

'Copy input values to sheet
    For i = 1 To LastRow
        If UCase(CStr(ActiveSheet.Range(Me.txtTaskCol.Value & i).Value)) = _
        UCase(CStr(Me.txtTask.Value)) Then
            ActiveSheet.Range(Me.txtUnitCol.Value & i).Value = Me.txtQuantity.Value
            matchCounter = matchCounter + 1
        End If
    Next i

If matchCounter = 0 then MsgBox "Nothing Found"

'Clear input controls
    Me.txtTask.Value = ""
    Me.txtQuantity.Value = ""
    Exit Sub

End Sub

希望它有所帮助。

答案 1 :(得分:0)

如果True语句一次为真,只需使用布尔标志将标志设置为If...Then

如果您希望在符合条件后继续For...Next声明,则表示您不想退出。

Private Sub cmdAdd_Click()
    On Error GoTo Whoa
    Dim LastRow As Long, i As Long, tskFlg As Boolean
    LastRow = ActiveSheet.Range(Me.txtTaskCol.Value & Rows.Count).End(xlUp).Row
    'Copy input values to sheet
    For i = 1 To LastRow
        If UCase(CStr(ActiveSheet.Range(Me.txtTaskCol.Value & i).Value)) = UCase(CStr(Me.txtTask.Value)) Then
            ActiveSheet.Range(Me.txtUnitCol.Value & i).Value = Me.txtQuantity.Value
            tskFlg = True
        End If
    Next i
    If tskFlg = False Then MsgBox "Task Not Found!"
    'Clear input controls
    Me.txtTask.Value = ""
    Me.txtQuantity.Value = ""
    Exit Sub

Whoa:
        Select Case Err.Number
            Case 1004
                MsgBox "Check for Valid Column Letters!"
        End Select

End Sub
相关问题