我想创建一个 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/