首页 > 解决方案 > #VALUE 特定日期值后 VBA 中的错误

问题描述

我已经编写了一些 VBA 函数(在下面的代码中列出) 我正在比较两个工作表中的记录,使用函数将相关值从一张表返回到另一张表。

所有其他函数所依赖的第一个函数返回患者 ID 号。

选择患者 ID 的标准:

该功能比较患者在 30 分钟间隔内到达的日期和时间(因为从一个来源收到的信息通常与另一个来源相差几分钟)、性别、诊所 ID 和出生年份。患者ID号从50000左右开始,一直持续到150000左右。我需要比较日期时间,因为有时会有两个相同性别、出生日期和诊所的患者在同一天到达。

该函数在 100000 行后失败 仅此 #VALUE!返回错误。

以下是我测试的一个复杂场景,发现日期和时间有问题。

  • 仅比较日期,没有间隔,返回正常值。
  • 最后工作的患者 ID 是 98472(尚未报告所有患者 ID),患者的到达日期为 2018 年 5 月 1 日晚上 8:42。
  • 下一个患者 ID 是 100471,于 2018 年 5 月 4 日上午 10:43 到达。* 该函数将此患者返回为#VALUE!错误,尽管所有参数都在那里。

这是代码(请原谅任何菜鸟的错误,我不是专业的编码器):

Function EINSATZ(aufnahmdat As Date, geburtsdat As Integer, geschlecht As Integer, klinik As Integer)
    'DEFINING PARAMETERS
    'rsu_r is the regional stroke unit row
    'rsu_c is the regional stroke unit column
    'size is the patient list size
    'iffunction allows the function to work through the patient list
    'converter converts letter to integer for sex

    Dim rsu_r As Integer
    Dim rsu_c As Integer
    Dim size As Variant
    Dim iffunction As Single
    Dim converter As Integer

    'here starts the dimension definition for rsu cells
    rsu_r = ActiveCell.Row
    rsu_c = ActiveCell.Column


    'here starts the size function
    'size is predetermined to measure and print the highest value within the first 9996 cells
    For iffunction = 4 To 9999
        If Application.WorksheetFunction.IsNumber(Worksheets("Präklinik").Cells(iffunction, 5)) Then
            size = size + 1
        End If
    Next iffunction

    'here starts the if function
    For iffunction = 4 To size        
        If Worksheets("Präklinik").Cells(iffunction, 6).Value = "m" Then
            converter = 2
        Else
            converter = 1
        End If

        If Worksheets("Präklinik").Cells(iffunction, 4).Value + Worksheets("Präklinik").Cells(iffunction, 17).Value < aufnahmdat + 1 / 48 _
           And Worksheets("Präklinik").Cells(iffunction, 4).Value + Worksheets("Präklinik").Cells(iffunction, 17).Value > aufnahmdat - 1 / 48 _
           And Worksheets("Präklinik").Cells(iffunction, 5).Value = geburtsdat _
           And converter = geschlecht _
           And Worksheets("Präklinik").Cells(iffunction, 41).Value = klinik Then
            EINSATZ = Worksheets("Präklinik").Cells(iffunction, 2).Value
            Exit For
        End If
    Next iffunction      
End Function

请帮我诊断错误的实际原因!

标签: excelvba

解决方案


推荐阅读