Button 带有';粘贴日期';函数的左下一个单元格

Button 带有';粘贴日期';函数的左下一个单元格,button,excel,command,cell,vba,Button,Excel,Command,Cell,Vba,我想创建一个命令按钮,该按钮具有将日期粘贴到单元格左侧的功能-我需要在将来复制下面的这个按钮 我正在努力: Private Sub CommandButton2_Click() Dim Str As String Str = Date Range(TopLeftCell).Value = Str End Sub 要指向特定范围,请执行以下操作: Private Sub CommandButton1_Click() Dim Str As String S

我想创建一个
命令按钮
,该按钮具有将日期粘贴到单元格左侧的功能-我需要在将来复制下面的这个按钮

我正在努力:

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

要指向特定范围,请执行以下操作:

Private Sub CommandButton1_Click()

    Dim Str As String
    Str = Date
    Range("C5").Value = Str

End Sub
Private Sub CommandButton2_Click()
Dim r As Long, c As Long, i As Long
Dim str As String
    r = Rows.Count
    c = Columns.Count
    str = Date
    For i = 1 To r
        If Cells(i, 1).Top > CommandButton2.Top Then
            Exit For
        End If
    Next
    r = i - 1 ' you have to calibrate, what fits better r = i or r = i - 1 or r = i - 2 ....
    For i = 1 To c
        If Cells(1, i).Left > CommandButton2.Left Then
            Exit For
        End If
    Next
    c = i - 2 ' you have to calibrate, what fits better c = i or c = i - 1 or c = i - 2 ....
    Cells(r, c) = str
End Sub
或者使用命名范围,即为
范围
C5命名为“TopLeftCell”

…然后您可以使用以下选项:

Private Sub CommandButton1_Click()

    Dim Str As String
    Str = Date
    Range("TopLeftCell").Value = Str

End Sub
大概是这样的:

Private Sub CommandButton1_Click()

    Dim Str As String
    Str = Date
    Range("C5").Value = Str

End Sub
Private Sub CommandButton2_Click()
Dim r As Long, c As Long, i As Long
Dim str As String
    r = Rows.Count
    c = Columns.Count
    str = Date
    For i = 1 To r
        If Cells(i, 1).Top > CommandButton2.Top Then
            Exit For
        End If
    Next
    r = i - 1 ' you have to calibrate, what fits better r = i or r = i - 1 or r = i - 2 ....
    For i = 1 To c
        If Cells(1, i).Left > CommandButton2.Left Then
            Exit For
        End If
    Next
    c = i - 2 ' you have to calibrate, what fits better c = i or c = i - 1 or c = i - 2 ....
    Cells(r, c) = str
End Sub

这应该是可行的,但这实际上取决于你把按钮放在哪里。查看
r=i-1
c=i-2

行,我建议使用
表单控件
而不是
ActiveX控件
,原因很简单复制按钮时,指向宏的链接保持不变。,这也是您的要求之一

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

子按钮1\u单击()
Dim cellAddr作为字符串
Dim aCol尽可能长
“~~>获取单元格的地址
cellAddr=ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
“~~>还要获取列号
aCol=ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
“~~>如果按钮位于第1列中,则此选项是必需的
如果aCol 1那么_
ActiveSheet.Range(cellAddr).偏移量(,-1).值=日期
端接头

您的意思是,如果按钮位于D5上方,则需要C5中的日期?确切地说,需要按钮将实际日期添加到旁边的左单元格中it@user3090201:很高兴能提供帮助:)+1了解
Form
控件优于
ActiveX
控件的优点