我有一个button_click宏,用于运行一组冗长的VBA代码,根据输入框中输入的日期创建和/或更新每日报告,每月报告单只包含每日总计,并且无与伦比包含原始数据的两个工作表之间不匹配的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问题,或者我的代码中是否有问题,如果是,那么。
我可以在必要时分享代码,但正如我所说,我不确定问题的位置。
谢谢, TSC
答案 0 :(得分:0)
原始代码
' 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
为了纠正这个问题,希望能让它变得更加通用,我删除了“如果nLDtRow&lt; nfDtRow然后......&#39;声明并替换为
' 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或真正用它做任何事情已经十多年了),因为我已经接受了这个项目。