将VBA功能应用于按钮直接上方的单元格

发布于 2025-01-28 11:53:36 字数 377 浏览 7 评论 0原文

在我的VBA代码中,目标是在按钮上方的单元格中添加500个。现在,所讨论的按钮在“ C7”单元格中。我希望每次按下按钮时,它正上方的单元格是“ B7”。我想在整个工作表中复制按钮以添加单元格。因此,代码必须像Cell.above +500之类的东西。我在下面添加了当前的代码。

Range("b7").Value = Range("b7").Value + 500

In my vba code the goal is to add 500 to the cell above the button. Right now the button in question is in cell "c7". I want the cell directly above it which is cell be "b7" to add 500 every time the button is pressed. I would like to copy the button to add for cells throughout the worksheet. So the code has to be something like cell.above +500 or something like that. I added my current code below.

enter image description here

Range("b7").Value = Range("b7").Value + 500

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2025-02-04 11:53:36

如果您使用的是表单按钮,则是这样:

Sub FormButtonClick()
With Shapes(Application.Caller).TopLeftCell.Offset(-1, 0)
    .Value = IIF(Not IsNumeric(.Value),500,.Value + 500)
End With
End Sub

您可以添加iif以查看它是否为空白并在500(或其他)中“初始化”它,如果是你渴望。

确保您正在查看正确的表格 - 这是在Sheet1对象代码模块中添加的。

否则,您可能需要指定表格。

另外,当您复制它们时,您需要重命名它们(默认情况下,它们以相同的名称复制)。

If you're using a form button, it's like this:

Sub FormButtonClick()
With Shapes(Application.Caller).TopLeftCell.Offset(-1, 0)
    .Value = IIF(Not IsNumeric(.Value),500,.Value + 500)
End With
End Sub

You can add the IIF to see if it's blank and "initialize" it at 500 (or whatever) if that's you're desire.

Demo

Make sure you're looking at the right sheet - this was added in the Sheet1 object code module.

Otherwise, you may need to specify a sheet.

Also, when you're copying them, you need to rename them (by default they are copied with the same name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文