我正在尝试编写一个获取一系列数据的函数,并计算用户提供的列中两个数字的比率。我想在行的末尾打印出这个比例,但由于某种原因,我无法使用cells函数引用行中的最后一个单元格。相反,Cells函数只是为我提供该单元格的值而不是单元格地址。我认为单元格功能也提供了地址。有人可以告诉我这是错的还是我的代码错了?
这是代码
Function calculateRatio(table As Range, numerator As Integer, denominator As Integer, Optional nameOfRatio As String)
On Error GoTo ExpectedError
Dim num As Double
Dim denom As Double
Dim ratio As Double
If table.Columns.Count < 2 Then
MsgBox ("Not enough data. Requires at least two or more rows.")
Exit Function
End If
If numerator < 1 Or numerator > table.Columns.Count Then
MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
If denominator < 1 Or denominator > table.Columns.Count Then
MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
For Counter = 1 To table.Rows.Count
num = table.cells(Counter, numerator)
denom = table.cells(Counter, denominator)
ratio = num / denom
temp = table.cells(counter, table.columns.count)
temp.Offset(0, 1).Value = ratio
Next Counter
Exit Function
ExpectedError:
Call MsgBox("Something went wrong. Make sure you are referencing columns with numbers and not text." & Err.Number & " : " & Err.Description)
End
End Function
更新
以下是更新后的代码:
Function calculateRatio(table As Range, numerator As Integer, denominator As Integer, Optional nameOfRatio As String)
Dim num As Double
Dim denom As Double
Dim ratio As Double
Dim temp As Range
Dim counter As Integer
If table.Columns.Count < 2 Then
MsgBox ("Not enough data. Requires at least two or more rows.")
Exit Function
End If
If numerator < 1 Or numerator > table.Columns.Count Then
MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
If denominator < 1 Or denominator > table.Columns.Count Then
MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
For counter = 1 To table.Rows.Count
num = table.cells(counter, numerator)
denom = table.cells(counter, denominator)
ratio = num / denom
Set temp = table.cells(counter, table.Columns.Count)
temp.Offset(0, 1).Value = ratio
Next counter
End Function
答案 0 :(得分:1)
尝试添加Dim temp as Range
并将temp = table.cells(counter, table.columns.count)
更改为set temp = table.cells(counter, table.columns.count)
您真正需要做的就是添加set
,如果Variant
临时变为long
,Variant
将成为Range
对象。
答案 1 :(得分:1)
无法从工作表调用的UDF中处理工作表单元格。
此处提供更多信息:
https://stackoverflow.com/a/15647054/1467082
在这里:
http://www.excel-it.com/UDF.htm
通常,子例程可以操作工作表,而函数则不能。例外情况是从子程序中调用的函数可以,但是这可能是将函数用于除了将子函数返回到子例程之外的任何函数的坏习惯。
答案 2 :(得分:0)
我假设您没有在代码中使用Option Explicit
。
您需要将“temp”声明为范围。
Dim temp As Range ' somewhere at the top of your function
For Counter = 1 To table.Rows.Count
......
Set temp = table.cells(Counter, table.columns.count)
...
Next
如果您有单元格坐标,为什么不手动偏移?
table.cells(counter, table.Columns.Count+1).Value = ratio
试试这个:
Function calculateRatio(table As Range, numerator As Integer, denominator As Integer, Optional nameOfRatio As String)
Dim num As Double
Dim denom As Double
Dim ratio As Double
Dim temp As Range
Dim counter As Integer
If table.Columns.Count < 2 Then
MsgBox ("Not enough data. Requires at least two or more rows.")
Exit Function
End If
If numerator < 1 Or numerator > table.Columns.Count Then
MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
If denominator < 1 Or denominator > table.Columns.Count Then
MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
Exit Function
End If
For counter = 1 To table.Rows.Count
num = table.cells(counter, numerator)
denom = table.cells(counter, denominator)
ratio = num / denom
table.cells(counter, table.Columns(table.Columns.Count).Column + 1).Value = ratio
'Set temp = table.cells(counter, table.Columns.Count)
'temp.Offset(0, 1).Value = ratio
Next counter
End Function
使用`table.columns(table.columns.count).column将确保您引用正确的列,不能想到一个会导致问题的示例,但最好是安全的。