简体   繁体   English

#值! 在VBA / Excel中使用范围对象时出错

[英]#value! error while using Range object in VBA/Excel

I am trying to write my first vba macro for excel 2010. It needs to do some long calculations for 200,000 rows and needs to populate 2 adjacent cells on the active row. 我正在尝试为excel 2010写我的第一个vba宏。它需要对200,000行进行一些长的计算,并且需要在活动行中填充2个相邻的单元格。 The following VBA function is a crude first attempt to accomplish the above. 下面的VBA功能是完成上述任务的最初尝试。 Problem is while executing the macro, I get #value! 问题是执行宏时,我得到了#value! error. 错误。 If I remove "cells" and .Value lines, it works fine and populates the activecell with appropriate value. 如果删除“单元格”和“ .Value”行,则可以正常工作,并为活动单元格填充适当的值。 What am I doing wrong? 我究竟做错了什么?

Appreciate the help very much. 非常感谢帮助。

Public Function someCalculation() As Integer

    Dim r As Range
    Dim nrow, ncol As Integer

    nrow = ActiveCell.Row
    ncol = ActiveCell.Column

    r = Cells(nrow, ncol + 1)
    r.Value = nrow * 100 + ncol

    someCalculation = nrow * 1000 + ncol

End Function

"Cells" is an object, you must use 'Set' with any object to assign it. “单元”是一个对象,必须将“设置”与任何对象一起使用才能对其进行分配。

But it's not possible to modify another cells than the one with function. 但是,除了具有功能的单元格外,无法修改其他单元格。

Edit : remove wrong code 编辑:删除错误的代码

    Set r = Cells(nrow, ncol + 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM