【发布时间】:2014-07-28 19:21:47
【问题描述】:
我比 VB 更熟悉 VBA。我在使用 ADO.NET 时遇到过项目损坏的情况,而您因为找不到更好的词而破坏了您的项目。此过程来自一个更大的项目,该项目将呼叫中心代理绩效的自动报告汇总在一起,特别是会找到最近 7 天的结果,然后删除该范围内的任何其他值,以由工作簿中的另一个工作表计算。
背景故事:我已经使用这个程序大约三周了,没有任何问题。有一天,我在运行我的报告时发现默认格式函数的 ByRef 错误,特别是变量“d”。尝试了一堆东西来重写格式函数,以防语法有点偏离:Format([string], "Short Date"), Format([date], "Short Date"), Format([date], "mmddyyyy") 和 Format([string], "mmddyyyy") 都会导致 ByRef 错误。我希望有一个简单的 .toshortdatestring 像 VB.NET。尝试创建我自己的格式函数也无济于事。
但是,当我将所有代码 - 完全 - 粘贴到我的一个备份中时,ByRef 错误消失了......我以为我刚刚破坏了那个工作簿,所以我将所有模块复制并粘贴到我的备份中继续我的快乐之路。一周后,再次运行我的报告,我在 Format 函数中得到相同的 ByRef 错误,突出显示变量 d。关于为什么这一直发生的任何想法?提前致谢!!
Excel 2013 - 文件大小约为 7 MB - 28 张 - 14 张带有多个 countifs/sumifs 以从原始数据中提取特定代理统计信息 - 没什么特别的。
Sub Last7Days(lastcolumn As String, wksht As Worksheet, width As Integer, datasheet As String)
Dim sht As Worksheet
Dim column As Long
Set sht = wksht
Dim rng As Range, inclusiveRange As Range
Dim startDate As Long, endDate As Long
column = 1
Dim d As Date
d = DateAdd("d", -7, Now)
d = Format(d, "Short Date")
Dim startdatestring As String
startdatestring = CStr(d)
Dim enddatestring As String
wksht.Activate
Call LastRowInA
Range("a" & LastRowInA).Select
enddatestring = CStr(ActiveCell.value)
startDate = DateValue(startdatestring)
endDate = DateValue(enddatestring)
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
wksht.Activate
sht.Cells(1, column).AutoFilter Field:=column, Criteria1:=">=" & startDate, Operator:=xlAnd _
, Criteria2:="<=" & endDate
Set rng = sht.Range(sht.Cells(2, column), sht.Cells(sht.Cells(sht.Rows.Count, column).End(xlUp).Row, column)).SpecialCells(xlCellTypeVisible)
sht.AutoFilterMode = False
If rng.Address = sht.Cells(1, column).Address Then
MsgBox Format(startDate, "dd-mmm-yyyy") & " - " & Format(endDate, "dd-mmm-yyyy") _
& vbCrLf & vbCrLf & "No instances of the date range exist"
Else
Set inclusiveRange = sht.Range(rng.Cells(1, 1), rng.Cells(rng.Count, width))
inclusiveRange.Select
Selection.Cut
ActiveSheet.Paste Destination:=Worksheets(datasheet).Range("a2")
Dim Start As String
Dim size As Long
'Set size = Nothing
Start = "A" & (rng.Count + 1)
size = Range("a" & rng.Count, Range("a" & rng.Count).End(xlDown)).Rows.Count
Range("a" & (rng.Count + 1) & ":I675000").Select
Selection.Clear
End If
Dim LastDateValue As String
LastDateValue = enddatestring
startdate1 = DateValue(LastDateValue)
endDate1 = DateValue(LastDateValue)
Dim testDate As Date
Dim testDateInteger As Integer
testDate = startdate1
testDateInteger = Weekday(testDate)
If testDateInteger = 2 Then
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
wksht.Activate
sht.Cells(1, column).AutoFilter Field:=column, Criteria1:=">=" & startdate1, Operator:=xlAnd _
, Criteria2:="<=" & endDate1
Set rng = sht.Range(sht.Cells(2, column), sht.Cells(sht.Cells(sht.Rows.Count, column).End(xlUp).Row, column)).SpecialCells(xlCellTypeVisible)
sht.AutoFilterMode = False
Set inclusiveRange1 = sht.Range(rng.Cells(1, 1), rng.Cells(rng.Count, width))
inclusiveRange1.Select
Selection.Clear
End If
End Sub
【问题讨论】:
-
你想用
d = Format(d, "Short Date")做什么?格式化日期并将其存储回日期变量而不是字符串变量似乎很奇怪。 -
不确定如何在回复中屏蔽代码...抱歉,看起来很乱
-
在 cmets 中的代码前后使用波浪号下方的刻度线。通常在 Tab 键上方。
-
仅使用 startdatestring 会导致 Format 函数
Dim d As Date Dim startdatestring As String d = DateAdd("d", -7, Now) startdatestring = Format(d, "Short Date")中的 d 出现相同的 ByRef 错误 -
这里有一个临时修复的想法,创建代码,将您的模块自动移动到您的备份之一。然后,每当出现错误时,让您的错误处理程序运行将所有内容迁移到备份的代码。它不漂亮,但它以自己的方式很酷:D 这是一些启动代码:ThisWorkbook.VBProject.VBComponents.Item(2).Export(YOUR_FILE_PATH_HERE)