结合使用“直到”和“不”来寻找第二条件

时间:2019-06-04 18:48:07

标签: excel vba

我正在运行“直到直到”循环,该循环正在降低目标单元格中​​的值,从.99(x = .99)开始,然后使用这些值来完成计算。如果满足2个条件中的1个,我需要此循环才能停止。

  1. 计算值在目标的10%以内。 或
  2. x = .75

如果计算结果与目标不符,则循环会将x降低.005,然后重试。但是我希望.75是x的下限。

Do Until 

(Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("J23").Value * -1 >= Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("U28").Value * 0.9 And Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("J23").Value * -1 <= Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("U28").Value * 1.1) Or (x = 0.75)

    Deal_ID = VBA.Right(Workbooks("Weekly Option Update (Master).xlsm").Sheets("GDD Group").Cells(i, "G").Value, 7)
    Sheets("Correlation").Range("E7").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("F8").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("C9").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("D10").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Calculate
    x = x - 0.005
    Workbooks("Weekly Option Update (Master).xlsm").Sheets("GDD Group").Cells(i, "H") = x + 0.005

Loop

我的代码可能不是最有效的,但目前可以运行。问题是它没有考虑x的下限的第二个条件。它一直保持下去,直到计算出的值在目标值的10%以内。

我对代码格式表示歉意。 “执行至”下方的代码块是带有“和”和“或”的代码。

2 个答案:

答案 0 :(得分:3)

男孩真是满嘴!提取局部变量,无需一次又一次地重复取消对相同对象的引用!

局部变量也使调试变得更加容易。

Dim solverBook As Workbook
Set solverBook = Application.Workbooks("Group 4 Correlation Solver")

Dim weeklyOptionBook As Workbook
Set weeklyOptionBook = Application.Workbooks("Weekly Option Update (Master).xlsm")

Dim gddGroupSheet As Worksheet
Set gddGroupSheet = weeklyOptionBook.Worksheets("GDD Group")

Dim structureSheet As Worksheet
Set structureSheet = solverBook.Worksheets("STRUCTURETOOL")

Dim currentValue As Double
currentValue = structureSheet.Range("J23").Value ' CAUTION: possible type mismatch here

Dim targetValue As Double
targetValue = structureSheet.Range("U28").Value ' CAUTION: possible type mismatch here

Const threshold As Double = 0.1
Const limit As Double = 0.75

Dim correlationSheet As Worksheet
Set correlationSheet = solverBook.Worksheets("Correlation")

Do Until (currentValue * -1 >= targetValue * (1 - threshold) _
  And currentValue * -1 <= targetValue * (1 + threshold)) _
  Or x <= limit

    Deal_Id = Right$(gddGroupSheet.Cells(i, "G").Value, 7)
    correlationSheet.Range("E7,F8,C9,D10").Value = x
    structureSheet.Calculate

    gddGroupSheet.Cells(i, "H") = x
    x = x - 0.005

    currentValue = structureSheet.Range("J23").Value ' CAUTION: possible type mismatch here
    targetValue = structureSheet.Range("U28").Value ' CAUTION: possible type mismatch here
Loop

在处理浮点数时不要使用=Or x <= limit可能是您眼前的问题的解决方案。

答案 1 :(得分:0)

我只用If测试第二个条件:

x = 1

Do Until x = 0.75

    if Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("J23").Value * -1 >= Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("U28").Value * 0.9 And Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("J23").Value * -1 <= Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Range("U28").Value * 1.1 then exit do

    Deal_ID = VBA.Right(Workbooks("Weekly Option Update (Master).xlsm").Sheets("GDD Group").Cells(i, "G").Value, 7)
    Sheets("Correlation").Range("E7").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("F8").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("C9").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("Correlation").Range("D10").Value = x
    Workbooks("Group 4 Correlation Solver").Sheets("STRUCTURETOOL").Calculate
    x = x - 0.005
    Workbooks("Weekly Option Update (Master).xlsm").Sheets("GDD Group").Cells(i, "H") = x + 0.005

Loop