VBA UDF 更改所有工作表上的值。如何限制为一个?
VBA UDF changes values on ALL sheets. How to limit to one?
我制作了一个适用于单个 sheet 的 UDF。多个 sheet 会出现此问题。如果我在多个 sheet 上有公式,并且如果我(重新)将它加载到一个 sheet 上,它也会更改所有其他 sheet 中的输出。
为什么会这样?我没有使用 ActiveWorksheet 或 Active Cell 或类似的东西。
Function customreturn(security As Range, datacheck As Range) As Variant
Dim row_num As Integer
Dim row_num2 As Integer
Dim price1 As Double
Dim price2 As Double
Dim perfo As Double
Dim blank_end As Boolean
row_num = security.Row
col_num = security.Column
row_num2 = row_num + 1
col_num2 = datacheck.Column
If WorksheetFunction.IsError(datacheck.Value) = True Then
customreturn = "No data"
Else
price1 = Cells(row_num, col_num).Value
Do While WorksheetFunction.IsError(Cells(row_num2, col_num2).Value) = True
row_num2 = row_num2 + 1
Loop
price2 = Cells(row_num2, col_num).Value
perfo = price1 / price2 - 1
customreturn = perfo
End If
End Function
您使用 Range.Cells property so the parent worksheet is defaulted to the ActiveSheet property. This can be rectified with a With ... End With statement that provides a worksheet reference to one of the range parameters' Range.Parent property 的三次都没有指定父作品sheet。
Function customreturn(security As Range, datacheck As Range) As Variant
Dim row_num As Long, row_num2 As Long, col_num As Long, col_num2 As Long
Dim price1 As Double, price2 As Double, perfo As Double
Dim blank_end As Boolean
row_num = security.Row
col_num = security.Column
row_num2 = row_num + 1
col_num2 = datacheck.Column
With security.Parent
If IsError(datacheck) Then
customreturn = "No data"
Else
price1 = .Cells(row_num, col_num).Value
Do While IsError(.Cells(row_num2, col_num2))
row_num2 = row_num2 + 1
Loop
price2 = .Cells(row_num2, col_num).Value
perfo = price1 / price2 - 1
customreturn = perfo
End If
End With
End Function
在 With ... End With 中,所有 Cells
都被引用为 .Cells
以表明父作品sheet 是 With 中引用的作品。 .. 结束于.
您不必明确比较作品sheet 的 ISERROR 或 VBA 的 IsError function 与 True。它已经知道是真还是假了。
已指出(感谢 BruceWayne) that you had two undeclared variables, col_num and col_num2. This can be avoided by adding Option Explicit¹ 声明区域中每个代码 sheet 的顶部。
¹ 在 VBE 的工具 ► 选项 ► 编辑器 属性 页面中设置 需要变量声明 将放置 Option Explicit 每个新创建的代码顶部的语句 sheet。这将避免愚蠢的编码错误,如拼写错误,并影响您在变量声明中使用正确的变量类型。没有声明的即时创建的变量都是 variant/object 类型。使用 Option Explicit 被广泛认为 'best practice'.
我制作了一个适用于单个 sheet 的 UDF。多个 sheet 会出现此问题。如果我在多个 sheet 上有公式,并且如果我(重新)将它加载到一个 sheet 上,它也会更改所有其他 sheet 中的输出。
为什么会这样?我没有使用 ActiveWorksheet 或 Active Cell 或类似的东西。
Function customreturn(security As Range, datacheck As Range) As Variant
Dim row_num As Integer
Dim row_num2 As Integer
Dim price1 As Double
Dim price2 As Double
Dim perfo As Double
Dim blank_end As Boolean
row_num = security.Row
col_num = security.Column
row_num2 = row_num + 1
col_num2 = datacheck.Column
If WorksheetFunction.IsError(datacheck.Value) = True Then
customreturn = "No data"
Else
price1 = Cells(row_num, col_num).Value
Do While WorksheetFunction.IsError(Cells(row_num2, col_num2).Value) = True
row_num2 = row_num2 + 1
Loop
price2 = Cells(row_num2, col_num).Value
perfo = price1 / price2 - 1
customreturn = perfo
End If
End Function
您使用 Range.Cells property so the parent worksheet is defaulted to the ActiveSheet property. This can be rectified with a With ... End With statement that provides a worksheet reference to one of the range parameters' Range.Parent property 的三次都没有指定父作品sheet。
Function customreturn(security As Range, datacheck As Range) As Variant
Dim row_num As Long, row_num2 As Long, col_num As Long, col_num2 As Long
Dim price1 As Double, price2 As Double, perfo As Double
Dim blank_end As Boolean
row_num = security.Row
col_num = security.Column
row_num2 = row_num + 1
col_num2 = datacheck.Column
With security.Parent
If IsError(datacheck) Then
customreturn = "No data"
Else
price1 = .Cells(row_num, col_num).Value
Do While IsError(.Cells(row_num2, col_num2))
row_num2 = row_num2 + 1
Loop
price2 = .Cells(row_num2, col_num).Value
perfo = price1 / price2 - 1
customreturn = perfo
End If
End With
End Function
在 With ... End With 中,所有 Cells
都被引用为 .Cells
以表明父作品sheet 是 With 中引用的作品。 .. 结束于.
您不必明确比较作品sheet 的 ISERROR 或 VBA 的 IsError function 与 True。它已经知道是真还是假了。
已指出(感谢 BruceWayne) that you had two undeclared variables, col_num and col_num2. This can be avoided by adding Option Explicit¹ 声明区域中每个代码 sheet 的顶部。
¹ 在 VBE 的工具 ► 选项 ► 编辑器 属性 页面中设置 需要变量声明 将放置 Option Explicit 每个新创建的代码顶部的语句 sheet。这将避免愚蠢的编码错误,如拼写错误,并影响您在变量声明中使用正确的变量类型。没有声明的即时创建的变量都是 variant/object 类型。使用 Option Explicit 被广泛认为 'best practice'.