更改值时,函数不会自动更新,但范围中的第一个单元格除外

时间:2014-03-18 08:31:25

标签: excel vba excel-vba

我创建了一个Excel / VBA函数来计算具有以下功能的学生分数:

  • 仅通过指定的第一个单元格形成两个范围: 一个用于marks_obtained,另一个用于Maximum_marks
  • 学习科目总数
  • 如果Rng1中的所有单元格都为空,则返回“_”
  • 如果Rng1中的单个单元格具有单词“Left”,则返回“Left”
  • 如果Rng1中包含标记的单元格总数小于NUm argumnt则返回“RL”
  • if Else:然后以两种形式计算,即如果为True&如果为假则总和

一切正常,问题是:

  

在工作表上更改值时,它不会自动更新。

需要使用 Ctrl + Alt + F9 或编辑( F2 ),然后按 Enter

请尊敬的专家帮助我。

Function RSLT_N(r1 As Range, r2 As Range, Num As Integer, Level As Integer, Optional Calc_Type As Boolean = False)

Dim i As Byte
        If Level = 1 Then
          i = 40
        ElseIf Level = 2 Then
          i = 45
        Else
          RSLT_N = "Error"
         ' Exit Function
        GoTo L1
        End If

Dim cell1 As Range, cell2 As Range, cell3 As Range, cell4 As Range
Dim Rng1 As Range, Rng2 As Range
    Set cell1 = Cells(r1.Row, r1.Column)
    Set cell2 = Cells(r1.Row, r1.Column + Num - 1)
    Set cell3 = Cells(r2.Row, r2.Column)
    Set cell4 = Cells(r2.Row, r2.Column + Num - 1)

    Set Rng2 = Range(cell3, cell4)
    Set Rng1 = Range(cell1, cell2)

    With WorksheetFunction

        If Check_Left(Rng1) Then
            RSLT_N = "Left"
            GoTo L1

        ElseIf .CountBlank(Rng1) = Num Then
            RSLT_N = "-"

        ElseIf .Count(Rng1) < Num Then
            RSLT_N = "RL"
            GoTo L1
        Else

            For c = 1 To Num
                If Rng1.Cells(1, c) / Rng2.Cells(1, c) * 100 < i Then
                    RSLT_N = "RL"
                    GoTo L1
                End If
            Next

            If Calc_Type = True Then
                RSLT_N = .Subtotal(109, Rng1)
                GoTo L1
            ElseIf Calc_Type = False Then
                RSLT_N = .Sum(Rng1)
                GoTo L1
            Else
                RSLT_N = "Error"
                GoTo L1
            End If

        End If
    End With

L1:
End Function

Function Check_Left(Rng As Range)
    Dim strTextString, Str As String
    Dim btCount As Byte
        btCount = Rng.Cells.Count

            For c = 1 To btCount
                On Error GoTo Last
                Set cl = Rng.Cells(1, c)
                strTextString = strTextString & cl
            Next

    With WorksheetFunction
        On Error GoTo Last
        Str = .IsNumber(.Search("left", strTextString))
    End With

        Check_Left = True
        Exit Function
Last:     Check_Left = False
End Function

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否尝试过插入代码:

application.calculatefull 

希望它适合你。

安德烈

P.S。我忘了提一下,有一些计算方法:

F9 - 重新计算

Application.Calculate

CTRL / ALT / F9 - 完整计算

在Excel 2000/2002/2003中:

Application.CalculateFull 

Shift F9 - Sheet Recalculate

Worksheets(“SheetName”).Calculate

范围计算

Range(“$a$1:$d$10”).Calculate

(我认为现在这是一个更完整的答案)