简体   繁体   English

Excel VBA一次仅更新一个单元格

[英]Excel VBA to update only one cell at one time

The motive of my code is to update one cell at a time with a comment who updated the cell at what time. 我的代码的动机是一次更新一个单元格,并带有一条注释,该注释在什么时候更新了该单元格。 But Instead of updating one cell at a time it's updating all my cells in K row. 但是,不是一次更新一个单元,而是更新了我在K行中的所有单元。 For Eg, if I want to update K10 its updating cell from K6 to K10 with the same value. 例如,如果我想用相同的值将K10的更新单元从K6更新到K10。 Please help 请帮忙

Sub ActiveCell_Initial()
 strUserName = Application.UserName
 Range(ActiveCell, "K6").Value = " by " & strUserName & "@ " & Now()
End Sub
Application.Calculation=xlCalculationManual
range("K6:K10").calculate

does this help? 这有帮助吗?

or maybe you merely meant to write this ? 还是您只是想写这个? (not sure I catch the question) (不确定我是否抓住了这个问题)

Sub ActiveCell_Initial()
 strUserName = Application.UserName
 ActiveCell.Value = " by " & strUserName & "@ " & Now()
End Sub

This will update only the ActiveCell : 这只会更新ActiveCell

Sub ActiveCell_Initial()
    strUserName = Application.UserName
    ActiveCell.Value = " by " & strUserName & "@ " & Now()
End Sub

you could not use the excel funciton NOW() at all and use the VBA function to get the datetime (i cant remember the commands) so that within the cell it says "by bob smith @ 1/1/2016 00:00:01" instead of "by bob smith @ NOW()" 您根本无法使用excel函数的NOW()并无法使用VBA函数获取日期时间(我不记得命令),因此该单元格中的内容为“ by bob smith @ 1/1/2016 00:00:01 ”,而不是“由鲍勃·史密斯@ NOW()”

alternatively you could copy the cell and special paste - values only it back which would give you the same result but most likely be more computationally hevy 或者,您可以复制单元格和特殊粘贴-仅对其重新赋值,这将为您提供相同的结果,但最有可能在计算上更加繁琐

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

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