VBA 间歇性 ByRef 错误 - 格式函数

Posted

技术标签:

【中文标题】VBA 间歇性 ByRef 错误 - 格式函数【英文标题】:VBA Intermittent ByRef Error - Format function 【发布时间】: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) 【参考方案1】:

不完全清楚发生了什么,这是真的,但如果它是编译器错误,正如 cmets 中的对话所表明的那样,它可能与代码中函数的确切用法有关。要修复它,您可以尝试以稍微不同的方式重写相同的代码逻辑。假设您的错误与以下代码部分有关,我可以提出一些建议,并根据 Excel 对这些问题的反应,可以尝试其他方法。

Dim d As Date
d = DateAdd("d", -7, Now)
d = Format(d, "Short Date")
Dim startdatestring As String
startdatestring = CStr(d)

    尝试将Option Explicit 添加到代码模块的顶部。这会导致 VBA 强制您显式地 Dim 您的所有变量,虽然它可能无法解决您的特定问题,但这是一种很好的编程习惯,它可能会揭示您的代码中的其他问题,您可以修复并摆脱这些问题。

    尝试重写没有变量d的代码部分。

    Dim startdatestring As String
    startdatestring = Format(DateAdd("d", -7, Now), "Short Date")
    

【讨论】:

不幸的是,我确实尝试过。在原始 ByRef 错误和项目“中断”之后,它将以不同的方式在有效代码上出错。在我转向这个更简化的参数后,我开始得到:参数数量错误或无效的属性分配错误。这真的很奇怪。代码可以完全正常运行几次(可能执行 25-30 次),然后在此过程中崩溃。 那么我有点难过 - 您是否尝试在不同的计算机上运行相同的 Excel 工作簿进行比较? 是的,我的台式电脑最近被更换了。第一次休息是在旧的,然后几天前又发生了……这整件事简直太疯狂了。这就是我在这里发帖的原因......我想不出任何会导致这种情况继续发生的事情。 澄清一下,我的电脑被更换是为了提高性能 - 不是因为故障。

以上是关于VBA 间歇性 ByRef 错误 - 格式函数的主要内容,如果未能解决你的问题,请参考以下文章

从被调用的函数调用函数时 VBA byref 参数类型不匹配

ByRef 参数类型不匹配 - Excel VBA

VBA ByRef Argument Type Mismatch string into string

ByRef 参数类型与布尔值不匹配

vba 格式函数抛出类型不匹配错误

VBA 中 Byref 和Byvel 怎么用呢