button - 具有 'paste date' 功能的命令按钮位于其下一个左侧单元格

标签 button excel command cell vba

我想创建一个 Commandbutton ,其功能是将日期粘贴到其左侧的单元格中 - 将来我需要复制下面的此按钮。

我正在尝试:

Private Sub CommandButton2_Click()
    Dim Str As String
    Str = Date
    Range(TopLeftCell).Value = Str
End Sub

最佳答案

我建议使用Form Control而不是ActiveX control,原因很简单。 当您复制按钮时,宏的链接保持不变。,这也是您的要求之一。

这是可用于 CommandButton(表单控件)的代码

Sub Button1_Click()
    Dim cellAddr As String
    Dim aCol As Long

    '~~> Get the address of the cell
    cellAddr = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
    '~~> Also get the column number
    aCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column

    '~~> This is required if the button is in column 1
    If aCol <> 1 Then _
    ActiveSheet.Range(cellAddr).Offset(, -1).Value = Date
End Sub

关于button - 具有 'paste date' 功能的命令按钮位于其下一个左侧单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20514156/

相关文章:

linux - 查找和计算在 Web 服务器上访问的唯一文件

c# - 在MVVM中处理MouseEnter over命令

c - 在 Linux 中使用 Make

html - 如何更改按钮悬停属性中的文本颜色

python - 连续的 ipywidgets 按钮

wpf - 在按钮的内容演示器中显示图像

sql - 如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?

javascript - 根据复选框 oracle apex 禁用或启用按钮

excel - 插入新行时将公式复制到下一行

excel - 在 Qt 中格式化 Excel 文档