Dav*_*vuz 6 excel vba excel-2007 excel-vba excel-2010
我想通过VBA设置单元格的值.我用谷歌搜索,并看到一些决议:
Sheets("SheetName").Range("A1").value = someValue
Sheets("SheetName").Cells(1,1).value = someValue
Run Code Online (Sandbox Code Playgroud)
使用这种代码,我只能从单元格A1中读取数据,但我无法为其设置新值.
更新
设置单元格A1值的代码Function
如下所示.
Function abb()
Sheets("SheetName").Range("A1").value = 122333
abb = 'any thing'
End Function
Run Code Online (Sandbox Code Playgroud)
在单元格B2中,我设置=abb()
并按Enter键.我得到了#VALUE但在A1没有任何事情发生.
将此代码放在宏中,它可以工作.
我的问题是,如何使A1在函数内具有值?
bre*_*tdj 13
从您上面的评论中,您想尝试这种方法
如果你进入
=abb()
任何细胞
然后该片材的单元格A1将被设置为12333
这是要更新以选择要更新的单元格并在其中放置值的行
Range("A1").Value = 122333
从我不希望我的Excel加载项返回一个数组(而是我需要一个UDF来更改其他单元格)
我将来自Kevin Jones又名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, i.e., 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
Run Code Online (Sandbox Code Playgroud)
您无法使用B2中的函数更改单元格A1.
访问:Excel中自定义函数的限制说明 .该文本包括:
"由工作表单元格中的公式调用的用户定义函数无法更改Microsoft Excel的环境.这意味着此类函数无法执行以下任何操作:
为什么要以这种方式更改单元格A1?解释你的目标,也许有人可以提供帮助.
归档时间: |
|
查看次数: |
28129 次 |
最近记录: |