不能在函数中将数据写入Excel 2007/2010中的单元格

我想通过VBA为单元格设置值。 我GOOGLE了,看到一些解决scheme:

Sheets("SheetName").Range("A1").value = someValue Sheets("SheetName").Cells(1,1).value = someValue 

有了这样的代码,我可以只读取单元格A1的数据,但是我不能为它设置一个新的值。

更新

设置单元格A1值的代码放在一个Function ,如下所示。

 Function abb() Sheets("SheetName").Range("A1").value = 122333 abb = 'any thing' End Function 

在B2单元格中,我设置=abb()并回车。 我得到#VALUE,但在A1没有发生。

把这个代码放在一个macros中,它可以工作。

我的问题是,如何让A1有一个函数内的值?

从上面的评论你想尝试这种方法

如果你input
=abb()
进入任何细胞

然后该单元格A1将被设置为12333

这是要更新的行来select要更新的单元格,并在其中放置一个值
Range("A1").Value = 122333

从我不希望我的Excel加载项返回一个数组(而是我需要一个UDF来更改其他单元格)

我从凯文·琼斯(又名Zorvek)转载了一段魔法,因为它位于EE Paywall后面 (如果任何人有权访问,链接都会附上)

虽然Excel严格禁止UDF更改任何单元格,工作表或工作簿属性,但是在使用Windows计时器和Application.OnTime计时器依次调用UDF时,有一种方法可以实现此类更改。 Windows计时器必须在UDF中使用,因为Excel忽略UDF内的任何Application.OnTime调用。 但是,由于Windows计时器有限制(如果Windows计时器尝试运行VBA代码(如果正在编辑单元格或打开对话框时,Excel将立即退出),它仅用于计划Application.OnTime计时器,一个安全计时器只有当单元格不被编辑且没有打开对话框时才允许触发Excel。

下面的示例代码演示了如何从UDF内部启动Windows计时器,如何使用该计时器例程来启动Application.OnTime计时器,以及如何将仅知道UDF的信息传递给后续的计时器执行的例程。 下面的代码必须放在常规模块中。

 Declare Function SetTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long, _ ByVal uElapse As Long, _ ByVal lpTimerFunc As Long _ ) As Long Private Declare Function KillTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long _ ) As Long Private mCalculatedCells As Collection Private mWindowsTimerID As Long Private mApplicationTimerTime As Date Public Function abb() ' This is a UDF that returns the sum of two numbers and starts a windows timer ' that starts a second Appliction.OnTime timer that performs activities not ' allowed in a UDF. Do not make this UDF volatile, pass any volatile functions ' to it, or pass any cells containing volatile formulas/functions or ' uncontrolled looping will start. abb = "Whatever you want" ' Cache the caller's reference so it can be dealt with in a non-UDF routine If mCalculatedCells Is Nothing Then Set mCalculatedCells = New Collection On Error Resume Next mCalculatedCells.Add Application.Caller, Application.Caller.Address On Error GoTo 0 ' Setting/resetting the timer should be the last action taken in the UDF If mWindowsTimerID <> 0 Then KillTimer 0&, mWindowsTimerID mWindowsTimerID = SetTimer(0&, 0&, 1, AddressOf AfterUDFRoutine1) End Function Public Sub AfterUDFRoutine1() ' This is the first of two timer routines. This one is called by the Windows ' timer. Since a Windows timer cannot run code if a cell is being edited or a ' dialog is open this routine schedules a second safe timer using ' Application.OnTime which is ignored in a UDF. ' Stop the Windows timer On Error Resume Next KillTimer 0&, mWindowsTimerID On Error GoTo 0 mWindowsTimerID = 0 ' Cancel any previous OnTime timers If mApplicationTimerTime <> 0 Then On Error Resume Next Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2", , False On Error GoTo 0 End If ' Schedule timer mApplicationTimerTime = Now Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2" End Sub Public Sub AfterUDFRoutine2() ' This is the second of two timer routines. Because this timer routine is ' triggered by Application.OnTime it is safe, ie, Excel will not allow the ' timer to fire unless the environment is safe (no open model dialogs or cell ' being edited). Dim Cell As Range ' Do tasks not allowed in a UDF... Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Do While mCalculatedCells.Count > 0 Set Cell = mCalculatedCells(1) mCalculatedCells.Remove 1 Range("A1").Value = 122333 Loop Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub 

您不能使用B2中的function更改单元格A1。

访问: Excel中自定义函数的限制说明 。 文本包括:

“由工作表单元格中的公式调用的用户定义的函数不能更改Microsoft Excel的环境,这意味着这样的函数不能执行以下任何操作:

  • 在电子表格中插入,删除或格式化单元格。
  • 更改另一个单元格的值。 [我的亮点]
  • 移动,重命名,删除或将工作表添加到工作簿。
  • 更改任何环境选项,例如计算模式或屏幕视图。
  • 将名称添加到工作簿。
  • 设置属性或执行大多数方法。“

你为什么要这样改变单元格A1? 解释你的目标,也许有人可以帮助。

如果你想用一个公式修改两个单元格,你可能要考虑从你的函数返回一个数组。 这是一个例子:

 Function abb() Dim arr As Variant ReDim arr(1 To 2) arr(1) = "aardvark" arr(2) = "bee" abb = arr End Function 

select单元格A2到B2。 Type =abb()并按下Shift Ctrl Enter来指定它是一个数组公式。 这个公式然后同时修改两个单元格(A2和B2)。

也许你可以自定义这个来做你想做的事情。

它应该工作 – 试试这个

  1. 打开一个新的Excel表格
  2. 创build一个新的macros
  3. 添加此Sheets("Sheet1").Range("A1").Value2 = "value"

您可以同时使用.Value.Value2 ,确保工作表名称正确。