VBA 中 Byref 和Byvel 怎么用呢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA 中 Byref 和Byvel 怎么用呢相关的知识,希望对你有一定的参考价值。
Byvel 写错了,应该是Byvalbyval就是按参数的值传递,再传递过程中,参数不会发生变化;与之对应的是byref,指按参数的地址传值,byref可以省去。Byref存址参数,通过操作参数的内存地址实现全局控制
Sub Test(ByRef a As Integer, ByVal b As Integer)
a = 3
b = 4
End Sub
Sub main()
Dim a As Integer
Dim b As Integer
a = 1
b = 2
Test a, b
MsgBox "a=" & a & ";b=" & b
End Sub
输出结果 a=3;b=2 参考技术A 一个是按值传递,一个是按地址传递。一个是参数传递给函数之后,参数值在函数内发生作用,一个是参数传递给函数之后,参数的值会改变到参数。另外,应该是byval 不是byvel
VBA:私有子中的编译错误 ByRef 参数类型不匹配
【中文标题】VBA:私有子中的编译错误 ByRef 参数类型不匹配【英文标题】:VBA: Compile Error ByRef Argument Type Mismatch in Private Sub 【发布时间】:2021-11-24 08:46:57 【问题描述】:我一直收到编译错误:(myQuarterNo)
的 ByRef 参数类型不匹配。
我将包含提供QuarterName
值的模块。目标是在指定的文本框中显示“Quarter1”、“Quarter2”、“Quarter3”或“Quarter4”。类似于MonthName
的方式。如果需要,我还可以包含frmQuarterlyExciseInput
。
我可能忽略了一些简单的事情。如果需要更多信息,请询问。有错误的整个代码非常长,所以我只包括了相关的块。我可以随时发布更多内容。
错误代码:
Option Compare Database
Option Explicit
Dim intReportVersion As Integer
Dim myQuarterNo As String
Dim myYear As Integer
Dim myStartDate As String
Dim myBottleConvertValue As Double
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim myDate
Dim myQuarterName As String
Dim myYear As Integer
Dim dblLine13Gallons As Double
Dim dblLine15Gallons As Double
Dim dblLine21Gallons As Double
Dim dblLine22Gallons As Double
Dim dblLine24Gallons As Double
Dim dblLine25Gallons As Double
Dim dblLine26Gallons As Double
Dim dblLine27Gallons As Double
Dim dblLine29Gallons As Double
Dim dblLine30Gallons As Double
Dim dblLine31Gallons As Double
Dim dblLine32Gallons As Double
Dim dblLine33Gallons As Double
Dim dblLine34Gallons As Double
Dim dblLine35Gallons As Double
Dim dblLine37StateWineTax As Double
myQuarterNo = [Forms]![frmQuarterlyExciseInput]![cboquarter]
myQuarterName = QuarterName(myQuarterNo)
myYear = [Forms]![frmQuarterlyExciseInput]![cboCurrentYear]
Me.txtQuarterName = myQuarterName
Me.txtYear = myYear
' ok lets to the fetching and calculating
dblLine13Gallons = Round(CalculateLine13Gallons(myBottleConvertValue), 2)
dblLine15Gallons = Round(CalculateLine15Gallons(myBottleConvertValue), 2)
dblLine21Gallons = Round(dblLine13Gallons + dblLine15Gallons, 2)
dblLine30Gallons = Round(CalculateLine30Gallons(myBottleConvertValue), 2) 'Out of stste include NULL stste
'Line 30 moved to calculate so as to add to line 24
dblLine24Gallons = Round(CalculateLine24Gallons(myBottleConvertValue), 2) + dblLine30Gallons
dblLine25Gallons = Round(CalculateLine25Gallons(myBottleConvertValue), 2)
dblLine26Gallons = Round(CalculateLine26Gallons(myBottleConvertValue), 2)
dblLine27Gallons = Round(CalculateLine27Gallons(myBottleConvertValue), 2) ' Ship in MD no NULL stste
'Added line 27 7/7/2020
'7/24/20 Now to be reported on line 30.A
dblLine29Gallons = Round(CalculateLine29Gallons(myBottleConvertValue), 2)
dblLine31Gallons = Round(CalculateLine31Gallons(myBottleConvertValue), 2)
dblLine32Gallons = Round(CalculateLine32Gallons(myBottleConvertValue), 2)
dblLine33Gallons = Round(CalculateLine33Gallons(myBottleConvertValue), 2)
dblLine34Gallons = Round(CalculateLine34Gallons(myBottleConvertValue), 2)
dblLine22Gallons = Round(dblLine21Gallons - (dblLine24Gallons + dblLine25Gallons + dblLine26Gallons + dblLine27Gallons + dblLine29Gallons + dblLine31Gallons + dblLine32Gallons + dblLine33Gallons + dblLine34Gallons), 2)
' 7/24/20 dblLine30Gallons removed to avoid double count because dblLine30Gallons is now in line 24
dblLine37StateWineTax = DLookup("SetUpValue", "DbSetup", "SetupKey='STATEWINETAX'")
dblLine35Gallons = Round(dblLine22Gallons + dblLine24Gallons + dblLine25Gallons + dblLine26Gallons + dblLine27Gallons + dblLine29Gallons + dblLine31Gallons + dblLine32Gallons + dblLine33Gallons + dblLine34Gallons, 2)
' 7/24/20 dblLine30Gallons removed to avoid double count because dblLine30Gallons is now in line 24
' ok now put the data onto the form
Me.txtLine13 = Round(dblLine13Gallons, 2)
Me.txtLine15 = Round(dblLine15Gallons, 2)
Me.txtLine21 = Round(dblLine21Gallons, 2)
Me.txtLine24 = Round(dblLine24Gallons, 2)
'7/24/20 Now line 24 includes former line 30 data
Me.txtLine25 = Round(dblLine25Gallons, 2)
Me.txtLine26 = Round(dblLine26Gallons, 2)
Me.txtLine27 = Round(dblLine27Gallons, 2)
' Added line 27 7/7/2020 will be reported on line 30A
Me.txtLine29 = Round(dblLine29Gallons, 2)
Me.txtLine30 = 0 'Round(dblLine30Gallons, 2) deleted, now included on line 24
Me.txtLine31 = Round(dblLine31Gallons, 2)
Me.txtLine32 = Round(dblLine32Gallons, 2)
Me.txtLine33 = Round(dblLine33Gallons, 2)
Me.txtLine34 = Round(dblLine34Gallons, 2)
Me.txtLine22 = Round(dblLine22Gallons, 2)
Me.txtLine35 = Round(dblLine35Gallons, 2)
' need to add/test for line 34. If Not Null AND is > 0, add it to the line 36 formula below.
If (dblLine34Gallons > 0) Then
Me.txtLine36 = Round(dblLine31Gallons + dblLine32Gallons + dblLine33Gallons + dblLine34Gallons, 2)
Else
Me.txtLine36 = Round(dblLine31Gallons + dblLine32Gallons + dblLine33Gallons, 2)
End If
Me.txtLine37 = dblLine37StateWineTax
Me.txtLine38 = Round(dblLine37StateWineTax * CDbl(Me.txtLine36), 2)
If (IsNull([Forms]![frmQuarterlyExciseInput]![txtPrepaidWineTax]) = False) Then
Me.txtLine39 = [Forms]![frmQuarterlyExciseInput]![txtPrepaidWineTax]
Else
Me.txtLine39 = 0
End If
Me.txtLine40 = CDbl(Me.txtLine38) - CDbl(Me.txtLine39)
End Sub
显示 QuarterName 的模块:
Option Compare Database
Option Explicit
Public Function QuarterName(varQuarter As Integer, varYear As Integer) As String
Select Case varQuarter
Case 1
strDateString = " [Invoices].[TransactionDate] Between #01/01/" & varYear & "# AND #03/31/" & varYear & "# "
Case 2
strDateString = " [Invoices].[TransactionDate] Between #04/01/" & varYear & "# AND #06/30/" & varYear & "# "
Case 3
strDateString = " [Invoices].[TransactionDate] Between #07/01/" & varYear & "# AND #09/30/" & varYear & "# "
Case 4
strDateString = " [Invoices].[TransactionDate] Between #10/01/" & varYear & "# AND #12/31/" & varYear & "# "
End Select
QuarterName = strDateString
End Function
【问题讨论】:
QuarterName 函数不返回季度名称字符串。你一步调试了吗?如果您已经从组合框中获得季度数,为什么还需要一个函数?只需与文本框中的表达式连接:="Quarter" & [Forms]![frmQuarterlyExciseInput]![cboQuarter]
问题是季度是几个月的范围,所以当我按照您的建议进行操作时,我会得到“Quarter 1-3”的输出。我试图找到一种方法来理解第 1-3 个月(1 月、2 月、3 月)= 第 1 季度。也许我只是过度劳累和过度思考。
从头开始,意识到我试图在一行中做两件不同的事情。现在我只需要弄清楚如何将月份转换为季度。我得到了输出来阅读我想要的内容。
可以设置组合框,使其列之一是季度名称。或者,如果您输入了特定日期:="Quarter" & DatePart("q", fieldname)
。或者从月份范围中提取季度(x 是范围输入,例如“1-3”):="Quarter" & Switch(Val(x)=1,1, Val(x)=4,2, Val(x)=7,3, True,4)
.
这不适用于如何设置报告以提取信息。这是我如何为月度报告设置其中一条线的示例(需要按季度调整)```
【参考方案1】:
您的函数 QuarterName
似乎需要 2 个参数(Quarter
和 varYear
),两个整数。
看起来你给了它 1 个参数:可能是一个 string(这是通常返回表单数据的方式)。
如果您找出第二个参数,您可能会更幸运,并在必要时使用 CInt
将字符串转换为整数。 (请参阅 Microsoft 的 "Type Conversion Functions" 列表。)
另外请注意,在调试时,您还可以使用TypeName
来检查变量或表达式当前存储在什么数据类型中。
另请参阅,VBA Data Types。
无关的旁注,当您在代码中看到大量重复时,可能是时候重新考虑您的方法了——例如,可能使用array 而不是所有dblLineXXGallons
变量
【讨论】:
谢谢,它真的不喜欢我使用数组。我已经让它像我想要的那样输出季度名称,现在的问题是它只提取月度数据而不是季度。但这与这个问题完全无关。以上是关于VBA 中 Byref 和Byvel 怎么用呢的主要内容,如果未能解决你的问题,请参考以下文章