简体   繁体   English

如何在VBA的do until循环中指定范围?

[英]How do I specify a range in the do until loop in VBA?

I'm writing a code, so that 我在写代码,这样

Do Until Range("A1:A10")=5 

""

Loop

I want a certain range to all have the same numbers, but VBA keeps on telling me there's a type mismatch. 我希望某个范围的数字都相同,但是VBA不断告诉我类型不匹配。 It seems that you can only work with one cell at a time or you would have to use the "And" function? 似乎您一次只能使用一个单元格,或者必须使用“ And”功能? (Do Until Range("A1")=5 And Range("A2")=5 , etc.) But is there a way to have the loop run until a certain range of cells satisfies the condition? (直到Range(“ A1”)= 5和Range(“ A2”)= 5等等)但是有没有办法让循环运行直到一定范围的单元格满足条件?

I think you're looking for something like this: 我认为您正在寻找这样的东西:

Dim rngUpdate As Range

Set rngUpdate = Range("A1:A5")
Do Until WorksheetFunction.CountIf(rngUpdate, 5) = rngUpdate.Cells.Count
    'Your code goes here
Loop

You can also use "for next", in your case: 在您的情况下,您也可以使用“ for next”:

For i = 1 To 10
    If Cells(i, 1) = "5" Then
    Exit For ' when cell value is 5, it exits loop

    Else
    *do other code*
    End if

Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM