变量在循环

时间:2015-07-14 20:17:00

标签: excel vba

我是第一次尝试学习Excel VBA的程序员,遇到了一些麻烦。

程序正在运行以计算可用区域的阻尼器的适当大小。一切都正常计算,除了变量DampW,因为它通过循环。当计算不同的阻尼器尺寸时,DampW变量在DampH变量改变时不会改变。 DampH在FOR循环中调整。

我还注意到,当针对当前条件运行时,程序应该退出,因为DampCheck的条件等于" OK"。但是,程序进入循环。

如果对我的要求有任何疑惑,请告诉我。想确保我正确发布了这个。

Public Sub DamperCalcs1()

DampType = Range("DampType").Value

'Activates the damper spreadsheet
Sheets(DampType).Activate

'Calculates the maximum height allowed from the spreadsheet with damper sizes
'Counts the number of 0 in column "O" and returns the maximum height 
MaxH1 = Application.WorksheetFunction.Max(ActiveSheet.Range("$C$7:$C$" & 2 + Application.WorksheetFunction.CountIf(ActiveSheet.Range("$O:$O"), 0)))

'Pulls out the minimum height of the specified damper for the air handling unit from the damper spreadsheet
MinH = Application.WorksheetFunction.Min(ActiveSheet.Range("$C$7:$C$7000"))

'Calculates the maximum Height allowed for damper sizing calculations
If DampType = "None" Then
    MaxH2 = Sheets("inputs Performance").Range("$C$12")

    ElseIf Sheets("Inputs Performance").Range("$C$12") > MaxH1 Then
        MaxH2 = MaxH1

    Else
        MaxH2 = Sheets("Inputs Performance").Range("$C$12")

End If

'Calculates a true damper height with standard blade configuration

'Setting max height value as an integer with a calculation
MaxH3 = [(MaxH2-3.75)/5.75]


'Calculates the Maximum damper height for calculations
If (MaxH3 * 5.75) < MinH Then
    MaxH4 = MinH

    Else
        MaxH4 = (MaxH3 * 5.75) + 3.75
End If

'Calculates the height addition for a louver
If Left(DampType, 3) = "ELF" Then
    DampH = 3 + Application.WorksheetFunction.Max(MaxH4, 9.5)

    Else
        DampH = Application.WorksheetFunction.Max(MaxH4, 9.5)

End If

'Outputs the maximum and minimum values required
Sheet33.Range("B2").Value = MaxH1
Sheet33.Range("B3").Value = MinH
Sheet33.Range("B5").Value = MaxH2

'Outputs the actual damper height
Sheet33.Range("B7").Value = DampH

End Sub

第二个子计划的开始

Public Sub DamperCalcs2()

CFM = Range("CFM").Value
MaxFPM = Range("MaxFPM").Value

  'Assigns a given value for variables required to calculate the EAML damper
EAMLA = 3.23750323750089E-07
EAMLB = -9.29262202444403E-03
EAMLC = 3.82761707988981E-03
EAMLD = -3.44782545737092E-02
EAMLE = 5.00341409432224E-07
EAMLF = 8.40487603305808E-03

If Left(DampType, 3) = "ELF" Then
     DampW = [RoundUp((((CFM / MaxFPM - Application.WorksheetFunction.VLookup(DampH, Sheets("Free Area").Range("$B$113:$E$132"), 2, False)) / Application.WorksheetFunction.VLookup(DampH, Sheets("Free Area").Range("$B$113:$E$132"), 4, False)) + 12), 0)]

     ElseIf DampType = "EAML" Then
        DampW = [RoundUp((-(EAMLD + EAMLC * DampH) + SQRT((EAMLD + EAMLC * DampH) ^ 2 - (4 * (EAMLE * (-(CFM / MaxFPM) + EAMLF + EAMLA * DampH * DampH + EAMLB * DampH))))) / (2 * EAMLE), 0)]

    Else
        DampW = [RoundUp((144 * (CFM / MaxFPM) / DampH), 0)]
End If

DampType = Range("DampType").Value

'Selects the damper spreadsheet
Sheets(DampType).Select

DampRange = ("$C$3:$C$" & 1 + Application.WorksheetFunction.CountIf(ActiveSheet.Range("$O:$O"), 0))

'number of heights in total range less than input height (use as origin for adjusted range on widths)
DampCount1 = Application.WorksheetFunction.CountIf(ActiveSheet.Range(DampRange), "<" & DampH) + 3

'number of heights in total range equal input height (use as origin for adjusted range on widths)
DampCount2 = Application.WorksheetFunction.CountIf(ActiveSheet.Range(DampRange), "=" & DampH)

'Sheets("Calc Performance 2").Activate
DampType = Range("DampType").Value

'Selects the damper spreadsheet
Sheets(DampType).Activate

'Creates a new range for damper heights
AdjDampRange = ("D" & DampCount1 & ":D" & (DampCount1 + DampCount2 - 1))

'number of widths in adjusted range less than clculated width
DampWCount = Application.WorksheetFunction.CountIf(ActiveSheet.Range(AdjDampRange), "<" & DampW)

'Calculates the line number to pull the actual damper width from
ActualDampWLine = DampCount1 + DampWCount

'Pulls the actual damper width from the spreadsheet
ActualDampW = Range("D" & ActualDampWLine).Value

DampLoc = Range("DampLoc").Value

'Calculates the hood depth for the max height of damper

If Left(DampType, 3) = "ELF" Or DampType = "EAML" Or DampLoc = "TOP" Or DampLoc = "BOTTOM" Then
     Hood1 = 0

    ElseIf Left(DampType, 3) = "AMS" And DampLoc = "Front" Then
        Hood1 = [RoundUp((DampH*DampW*1200)/((DampW+4)*1000)+8,0)]

   ElseIf Left(DampType, 3) = "AMS" And DampLoc = "Side" Then
        Hood1 = [RoundUp((DampH*DampW*1200)/((DampW+4)*1000)+16,0)]

   ElseIf Left(DampType, 2) = "CD" And DampLoc = "Front" Then
       Hood1 = [RoundUp((DampH*DampW*1200)/((DampW+4)*1000),0)]

   ElseIf Left(DampType, 2) = "CD" And DampLoc = "Side" Then
       Hood1 = [RoundUp((DampH*DampW*1200)/((DampW+4)*1000)+8,0)]

End If

'Determines if Damper or Louver is acceptable
If Sheets(DampType).Cells(3, ActualDampWLine) > DampH Or DampH > MaxH Or ActualDampW > MaxW Then  'Removed the following from check to tryout program Or Hood1 > 60
    DampCheck = "X"
    Else
        DampCheck = "OK"
End If

'Outputs the actual damper height
Sheet33.Range("B9").Value = DampW

'Outputs the actual damper height
Sheet33.Range("B11").Value = DampRange
Sheet33.Range("B12").Value = DampCount1
Sheet33.Range("B13").Value = DampCount2
Sheet33.Range("B15").Value = AdjDampRange
Sheet33.Range("B16").Value = DampWCount
Sheet33.Range("B18").Value = ActualDampWLine
Sheet33.Range("B19").Value = ActualDampW
Sheet33.Range("B20").Value = Hood1
Sheet33.Range("B22").Value = DampCheck

Sheets("Calc Performance 2").Select
End Sub

用于调用包含循环的两个子程序的程序。

Public Sub TrialCalc3()

Call DamperCalcs1
Call DamperCalcs2

If DampCheck = "OK" Then
Sheet33.Range("B24").Value = "Macro Finished on first run"

    Else
        For LoopCount = 1 To 6
            If DampCheck = "OK" Then
                Sheet33.Range("B24").Value = "Macro Finished between Loop runs"
                Exit Sub

                Else
                    DampH = DampH - 5.75
                    Call DamperCalcs2

            End If
        Next
End If

'Outputs the actual damper height
Sheet33.Range("C7").Value = DampH
Sheet33.Range("C9").Value = DampW

'Outputs the actual damper height
Sheet33.Range("C11").Value = DampRange
Sheet33.Range("C12").Value = DampCount1
Sheet33.Range("C13").Value = DampCount2
Sheet33.Range("C15").Value = AdjDampRange
Sheet33.Range("C16").Value = DampWCount
Sheet33.Range("C18").Value = ActualDampWLine
Sheet33.Range("C19").Value = ActualDampW
Sheet33.Range("C20").Value = Hood1
Sheet33.Range("C22").Value = DampCheck

End Sub

在模块顶部分配的全局变量

Option Explicit
Public Damptype As String, DampTab As Variant, MaxH1 As Double, MaxH2 As Double, MinH As Double, MaxH3 As Integer, MaxH4 As Double, DampH As Double, MaxH As Double, MaxW As Double
Public CFM As Double, MaxFPM As Double, DampW As Double, EAMLA As Double, EAMLB As Double, EAMLC As Double, EAMLD As Double, EAMLE As Double, EAMLF As Double
Public DampRange As Variant, DampCount1 As Double, DampCount2 As Double
Public AdjDampRange As Variant, DampWCount As Double, ActualDampW As Double, ActualDampWLine As Double, DampCheck As String
Public DampLoc As String, Hood1 As Integer, LoopCount As Integer

在DampW计算中添加了以下代码行,以查看我的内容。

If Left(Damptype, 3) = "ELF" Then
    DampW = [RoundUp((((CFM / MaxFPM - Application.WorksheetFunction.VLookup(DampH, Sheets("Free Area").Range("$B$113:$E$132"), 2, False)) / Application.WorksheetFunction.VLookup(DampH, Sheets("Free Area").Range("$B$113:$E$132"), 4, False)) + 12), 0)]

    ElseIf Damptype = "EAML" Then
        DampW = [RoundUp(144 * (CFM / MaxFPM) / DampH, 0)]
    Else
        DampW = [RoundUp(144 * (CFM / MaxFPM) / DampH, 0)]
        MsgBox "MaxFPM is " & MaxFPM & " and Damper Height is " & DampH & " and New Damper Width " & DampW

End If

我输出的内容如下:

MaxFPM等于1800(所有DampW计算的标准) DampH等于61.25(调整后的正确高度) DampW等于36(这是原始数字,计算后应为40)

此时的阻尼器宽度应该调整为33,而不是保留在原来的36处。不要理解我做错了什么。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您的代码看起来合理,但 Subs 无法正常通信。例如, Sub TrialCalcs3 检查变量 DampCheck 。但它不能&#34;看&#34;变量,因为它在范围内不是全局

使例程能够分享&#34;变量,它们必须是范围内的全局(即公共)或作为参数传递。