问题
I'm newer to VBA than VB. I've had circumstances while using ADO.NET where the project gets corrupted and you, for lack of a better word, break your project. This procedure is from a larger project that pulls together automated reports for call center agent performance and specifically will find the last seven days results then delete any other values within that range to be calculated by another sheet in the workbook.
Backstory: I've used this procedure for about three weeks with no issue. One day, I was running my reports and got a ByRef error for the default Format function, specifically the variable "d". Tried a bunch of stuff to rewrite around the format function in case the syntax was a little off: Format([string], "Short Date"), Format([date], "Short Date"), Format([date], "mmddyyyy"), and Format([string], "mmddyyyy") all result in ByRef Error. I wish there was a simple .toshortdatestring like VB.NET. Tried creating my own format function as well to no avail.
However, when I paste all of my code - EXACTLY - into one of my backups, the ByRef error goes away.... Thought I just broke that workbook, so I copied and pasted all of my modules into my back up and went on my merry way. A week later, running my reports again, I get the same ByRef error at the Format function, highlighting the variable d. Any thoughts as to why this keeps happening?? Thanks in advance!!
Excel 2013 - file size approx 7 MB - 28 sheets - 14 with multiple countifs/sumifs to pull specific agent stats from the raw data - nothing fancy.
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
回答1:
It's not entirely clear what is going on, it's true, but odds are if it is a compiler bug as the conversation in the comments indicates, it's probably hyper-specific to the exact usage of functions you have in your code. To fix it, you could try rewriting the same code logic a slightly different way. Assuming that your error is with the following section of code, I can suggest a couple things and based on how Excel reacts to those there could be other things to try.
Dim d As Date
d = DateAdd("d", -7, Now)
d = Format(d, "Short Date")
Dim startdatestring As String
startdatestring = CStr(d)
Try adding
Option Explicitto the top of your module of code. This causes VBA to force you to explicitlyDimall your variables and while it probably won't solve your specific issue, it's good programming practice and it may reveal other issues with your code that you can fix and get out of the way.Try rewriting the section of code without the variable
d.Dim startdatestring As String startdatestring = Format(DateAdd("d", -7, Now), "Short Date")
来源:https://stackoverflow.com/questions/25002720/vba-intermittent-byref-error-format-function