【问题标题】:VBA Excel: Input Box Date Specified Yet Data for All Dates is Being GeneratedVBA Excel:指定输入框日期,但正在生成所有日期的数据
【发布时间】:2014-08-13 19:34:53
【问题描述】:

我有一个 button_click 宏,它运行一组冗长的 VBA 代码,以根据输入框中输入的日期创建和/或更新每日报告、仅包含每日总计的月度报告表,以及不匹配的包含原始数据的两个工作表之间不匹配的 UID 的报告工作表。对于大多数日期,这可以正常工作。但是,当日期“2014 年 7 月 11 日”以数字格式输入时,它会将所有日期从 2014 年 7 月 11 日拉到两张表中可用日期的末尾。但是,如果我将日期输入为“2014 年 7 月 11 日”,则脚本会正常运行。

为什么 2014 年 7 月 11 日(2014 年 7 月 11 日或 14 年 7 月 11 日)会提取 2014 年 7 月 11 日以后的数据,而不仅仅是与 2014 年 7 月 11 日特别相关的数据?我的代码很长,我不确定这是否是 Excel 的日期问题,或者我的代码是否有问题,如果有,在哪里。

如有必要,我可以共享代码,但正如我所说,我不确定问题出在哪里。

谢谢, TSC

【问题讨论】:

  • 你能检查用于输入的日期的单元格值吗?它是文本还是实际上是日期值。如果它是一个值,您可以发布加载日期的代码吗?
  • 用户手动输入日期的单元格值,我将其设置为变量。然后将其转换为 3 种不同的日期格式以创建报告名称并与每个具有原始数据的工作表匹配。
  • 我发现了我的代码中的“错误”。为了找到数据集的“结束”行,我依赖下一个日期或之后的日期在其中一个工作表中包含数据。如果两者都不存在,那么我将代码设置为转到工作表的最后一行。在这种情况下,该工作表中没有 20140712 和 20140713 的数据,这意味着结束范围是工作表的末尾。休息后,我回来并认为我解决了这个问题。 (将在下面显示代码仅供参考。)

标签: vba date excel inputbox


【解决方案1】:

原始代码

    ' if the requested date exists in wksKEY column A, then proceed.
If dtExists(sKeyDate, wksKEY, "A:A") Then
        ' nFDtRow = the first row in wksKEY with the requested date, which is stored in
        ' wksGDR A5 using Keystone_Data date format of YYYYMMDD
    nFDtRow = MatchUID(wksGDR, wksKEY, "A5", "A1:A" & lKeyLastRow)
        ' nLDtRow = last row in wksKEY with specified date; stored in wksGDR A6 as YYYYMMDD
    nLDtRow = MatchUID(wksGDR, wksKEY, "A6", "A1:A" & lKeyLastRow) - 1
        ' if the last date row is less than the first date row, then...
    If nLDtRow < nFDtRow Then
            ' wksGDR A6 = the date in A5 + 2 (i.e. if 20140102 is the date selected and 20140103
            ' is not in wksKEY, then wksGDR A6 = 20140104)
        wksGDR.Range("A6").Value = wksGDR.Range("A5").Value + 2
            ' if there is not a match in wksKEY for the value in wksGDR A6, IsError will equal True.
            ' if there is not an error, hence the match search = True, then ...
        If Not MatchUID(wksGDR, wksKEY, "A6", "A1:A" & lKeyLastRow) = 0 Then
                ' nLDtRow = the first row with the value in wksGDR A6 - 1 to equal the last row with the
                ' desired date
            nLDtRow = MatchUID(wksGDR, wksKEY, "A6", "A1:A" & lKeyLastRow) - 1
        Else
                ' Otherwise, if there is an error, nLDtRow equals the last row in wksKEY
            nLDtRow = lKeyLastRow
        End If
    End If
        ' nCtDtRow = the total count of matching date rows in wksKEY
    nCtDtRow = (nLDtRow - nFDtRow) + 1
        ' review each row with i doing the count and j representing the actual row number
    For i = 1 To nCtDtRow
            ' lDRNewRow = the row number where a new row can be added
        lDRNewRow = lDRRowEnd + i
            ' j = the first date row # + i (counting from 1 to the total #) - 1 to get the proper
            ' row #
        j = nFDtRow + (i - 1)
            ' If current UID does not already exist in a Daily_Report or the Unmatched_Report, then...
        If StopDupeUIDs(wksKEY, "AM" & j, lKeyLastRow) = True Then
                ' Verify that there is no match between the current UID and one already in wksDR
            If MatchUID(wksKEY, wksDR, "AM" & j, "P3:P" & lDRRowEnd) = "0" Then
                    ' enter wksKEY UID into wksDR column P
                wksDR.Range("P" & lDRNewRow).Value = wksKEY.Range("AM" & j)
            End If
        Else
                ' Otherwise, if the requested UID already exists in another sheet, update column R and color it bright green
            If UpdateUID(wksKEY, "AM" & j, lKeyLastRow) <> "" Then
                wksGDR.Range("A7").Value = UpdateUID(wksKEY, "AM" & j, lKeyLastRow)
                sTempWS = Left(wksGDR.Range("A7").Value, InStr(1, wksGDR.Range("A7"), Chr(124), vbTextCompare) - 1)
                lTempCell = Right(wksGDR.Range("A7").Value, Len(wksGDR.Range("A7")) - InStr(1, wksGDR.Range("A7"), Chr(124), vbTextCompare))
                Worksheets(sTempWS).Range("R" & lTempCell).Value = j
                Worksheets(sTempWS).Range("R" & lTempCell).Interior.Color = RGB(0, 255, 0)
                Call ckQty(Worksheets(sTempWS), lTempCell, lTempCell)
            End If
                wksGDR.Range("A7").Value = ""
        End If
    Next i
Else
    ' otherwise, if the requested date does not exist in wksKEY column A, exit this subroutine.
    Exit Sub
End If

为了纠正这个问题,并希望它更通用,我删除了“If nLDtRow

     ' wksGDR A6 = the number of times sKeyDate exists within wksKEY column A
    wksGDR.Range("A6").Value = WorksheetFunction.CountIf(wksKEY.Range("A1:A" & lKeyLastRow), sKeyDate)
        ' wksGDR A7 = nFDtRow + wksGDR A6
    wksGDR.Range("A7").Value = nFDtRow + wksGDR.Range("A6").Value
       ' nLDtRow = wksGDR A7
    nLDtRow = wksGDR.Range("A7").Value

我还将 nCtDtRow 更新为 nCtDtRow = wksGDR.Range("A6").Value,而不是尝试重新计算。

随着我参与这个项目,我一直在学习 VBA(自从我在学校学习 VB 或真正将它用于任何事情以来已经十多年了)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2013-10-26
    相关资源
    最近更新 更多