使用If语句执行“直到"操作,直到达到某个值为止 [英] Performing a Do Until with an If statement until a certain value is reached

查看:65
本文介绍了使用If语句执行“直到"操作,直到达到某个值为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个大型的数据表.我需要从特定单元格中减去的代码,直到该值大于某个数字为止.如果未达到该数字,我希望将其设置为0.

I am dealing with a large data sheet. I need code to subtract from a specific cell until the value is greater then a certain number. if that number isn't achieved I want it to go to 0.

例如,如果 A3-A2>如果< Q5,则E3 = A3-A2.Q5做A3-A1 .如果这不起作用,则 E2 = 0 .我需要这样才能使整个A列连续下来.到目前为止,我的代码如下:

For example if A3-A2 > Q5 then E3= A3-A2 if < Q5 do A3-A1. If this doesn't work E2=0. I need this to be continuous down the whole A column. So far, my code is as follows:

Do Until Range("A3").Offset(1, 0) - Range("A2") > Range("Q5")
  If Range("A3").Offset(1, 0) - Range("A2") > Range("Q5") Then
    Range("E3").End(xlDown) = Range("A3").Offset(1, 0) - Range("A2")
  Else: Range("E3").End(xlDown) = 0
  End If
Loop

它似乎不喜欢我输入的方式.而且我认为偏移量是不正确的.

It doesn't seem to like the way I enter it. and I don't think the offset is right.

推荐答案

使用同时直到同时... WEnd .将无条件的 Do ... Loop 与功能齐全的条件检查代码和 Exit Do 一起使用,您可以将其放置在循环的任何位置,但不能在循环的开始或结束位置进行只要.

It's a bad practice to use While, Until, While...WEnd. Use unconditional Do...Loop with full-featured condition check code and Exit Do, which you can locate in any place of cycle, but not in beginning or end of it only.

-1?OK:)

MS F1:提示Do ... Loop语句提供了一种执行循环的更结构化和灵活的方法.

MS F1: Tip The Do...Loop statement provides a more structured and flexible way to perform looping.

此服务"仅用于懒惰.我对 While Until 的定义是此声明直接导致实现结构化且灵活" 的直接结果,有关该主题要求使用哪种主题启动器.

This "services" are for the lazy only. My denie of While and Until is the direct consequence of this statement exactly to achieve "structured and flexible", about which topick starter is asked for.

这篇关于使用If语句执行“直到"操作,直到达到某个值为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆