首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VBA Excel:输入框日期已指定,但正在生成所有日期的数据

VBA Excel:输入框日期已指定,但正在生成所有日期的数据
EN

Stack Overflow用户
提问于 2014-08-14 03:34:53
回答 1查看 426关注 0票数 0

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

为什么7/11/2014 (7/11或7/11/14)会从7/11/14开始拉入数据,而不仅仅是与7/11/14相关的数据?我的代码非常长,我不确定这是Excel的日期问题,还是我的代码中有问题,如果是的话,在哪里。

如果有必要,我可以分享代码,但正如我所说的,我不确定问题所在。

谢谢,台积电

EN

回答 1

Stack Overflow用户

发布于 2014-08-14 05:10:00

原始代码

代码语言:javascript
运行
复制
    ' 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 < nfDtRow Then...‘语句,并将其替换为

代码语言:javascript
运行
复制
     ' 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或真正使用它做任何事情以来,已经有十多年了),就像我一直在做这个项目一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25294645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档