2016-01-21 155 views
-3

我正在使用Excel。我想按下按钮取消按钮本身左侧的单元格的值。通过在Excel中按下按钮来删除单元格

所以用户体验应该是以下几点:

当用户按下“按钮1”,在其离开该小区成了0同为“按钮2”和“BUTTON3” 如何我可不可以做? 通过使用宏?

+8

它被称为[删除]或[删除]键。 – Jeeped

+0

是的,你会使用一个类似'Cells(1,1).ClearContents'的宏。但@Jeeped说得很好 - 你想用这个按钮做什么?我希望这是一种简化的问题,您将使用它来创建比用户只能“删除”更细微的内容。潜在的[XY问题?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – BruceWayne

+0

嗨布鲁斯和嗨Jeeped。我只是简单地从列表中删除一个借记卡,我想按一个按钮来做,只是为了教学目的! 问题是:我有做3个宏来删除3个单元吗?这不是很聪明:我可以在宏的代码中参考一下:“查看按下的按钮并删除左边的值”。 因此,只需一个宏就可以完成每个按钮的所有工作 – Bernheart

回答

2

将此宏分配给任何和所有按钮,它将删除信息。在细胞直接向左。

Sub test() 
    Dim btnRow&, btnCol& 
    btnRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row 
    btnCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column 
    Cells(btnRow, btnCol).Offset(0, -1).ClearContents 
End Sub 

或者,由于@Rory,这可以是:

Sub test() 
    Activesheet.Shapes(Application.Caller).TopLeftCell.offset(0,-1).ClearContents 
End Sub 

注:请确保您的形状都处于有利地位,因为这无论使用的左上方形状的是确定行/列。此宏可减少运行三个不同位置的需要(取决于位置),并最小化任何If/Then类型语句,因为它使用Caller来确定哪个形状,它决定了它的位置。

+1

为什么不只是'ActiveSheet.Shapes(Application.Caller).TopLeftCell.Offset(0,-1).ClearContents'? – Rory

+0

@Rory - 我不知道为什么不!这看起来不错,我会编辑我的帖子。 – BruceWayne

0

如果您使用的命令按钮

Option Explicit 
Sub Button1_Click() 
    Range("A1") = 0 
End Sub 

或分配到工作表

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    If Not Intersect(Target, Range("B1")) Is Nothing Then _ 
     Range("A1") = 0 

End Sub 
+0

使用'Button1_Click',是否有方法可以确定Button所在的单元格?如果是这样,你可以用'Cells(Button1.Row,Button1.Column).Offset(0,-1).ClearContents'来代替'Range'。 – BruceWayne

+1

@BruceWayne明白了,谢谢保持简单.. – 0m3r

0

愿这帮助...

'Add three buttons on the sheet 
'And imaging that you want to delete B4 B5 B6 
'You can discover which button is, by double cliking on the desing mode 
'on your excel 
Private Sub CommandButton1_Click() 'This is the button on the cell C4 
    Range("B4").ClearContents 
    'Here B4 is the range of the cell you want to delete... 
    'This could be acomplish just pressing the button DELETE 
    'Same by the two other buttons... just adding 1 to the number 
    'B4...B5...B6 
    'With this you only can delete de contents of the cell... 
End Sub 

Private Sub CommandButton2_Click() 'This is the button on the cell C5 
    Range("B5").ClearContents 
End Sub 

Private Sub CommandButton3_Click() 'This is the button on the cell C6 
    Range("B6").ClearContents 
End Sub 
0

超链接的方法是我所做过的事情。喜欢它,因为它看起来像它可以点击。

enter image description here

添加下面的代码工作表模块,当一个超链接点击后,就会触发子

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) 
    If LCase(Target.Range.Text) Like "delete*" Then 
     ActiveCell.Offset(0, -1).ClearContents 
    End If 
End Sub 

您可以使用下面的代码来生成作为超链接你想尽可能多。

Sheets(XXX).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _ 
         "", TextToDisplay:="Delete" 
相关问题