2016-01-21 116 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" 
相關問題