此函数在一个单元格内返回多个查找匹配项:
Function NBlookup(lookupval, lookuprange As Range, indexcol As Long)
Dim result As String
Dim x As Range
result = ""
For Each x In lookuprange
If x = lookupval Then
result = result & " " & x.Offset(0, indexcol - 1) & x.Offset(0, 5)
End If
Next x
NBlookup = result
End Function
以下部分将其与数字值连接起来:
& x.Offset(0, 5)
我想四舍五入该值:if & x.Offset(0, 5) >= 5 then
四舍五入,否则四舍五入。
我该如何实现?
答案 0 :(得分:3)
只需使用Round()
函数,例如:Round(x.Offset(0, 5), 0)
重新阅读您的帖子后,您似乎在尝试向上或向下取整,大约5而不是0.5
如果是这样,我会做这样的事情:Round(x.Offset(0, 5)/10, 0)*10
Public Function NBlookup(lookUpVal, lookUpRange As Range, indexCol As Long)
Dim Result As String
Dim x As Range
Result = ""
For Each x In lookUpRange
If x = lookUpVal Then
Result = Result & " " & x.Offset(0, indexCol - 1) & Round(x.Offset(0, 5), 0)
End If
Next x
NBlookup = Result
End Function
我在变量名称中添加了一些大写字母(小写的驼峰),以了解更多内容。
我还将您的函数声明为Public
,以便您可以直接在Excel单元格中使用它。无论如何,最好声明函数或子对象的范围(公共,私有,朋友)。 ;)
答案 1 :(得分:1)
尝试将& x.Offset(0, 5)
替换为
& Application.WorksheetFunction.Round(x.Offset(0, 5),0)