按Excel中的按钮擦除单元格

时间:2016-01-21 00:36:01

标签: excel excel-vba vba

我在Excel工作。我想按一个按钮取消按钮左侧单元格的值。

因此,用户体验应如下:

当用户按下" button1"时,其左边的单元格变为0.对于" button2"和"按钮3" 我能怎么做? 使用宏?

4 个答案:

答案 0 :(得分: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

注意:确保您的形状放置得很好,因为它使用形状的左上角来确定行/列。这个宏减少了运行三个不同的需要,具体取决于where,并最小化任何If / Then类型语句,因为它使用Caller来确定哪个形状决定了它的位置。

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

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

enter image description here

在Worksheet模块上添加以下代码,当点击超链接时,它将触发子

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSheet();

  var names = sheet.getRange(1, 1, 200);

  var values = names.getValues();

  var number = 0

  var count = 0;

  var array = "'" + values[0] + "'";

  while (values[count] != "") {
    number++;
    count++;
    if (values[count] != "") {
      array = array + ", " + "'" + values[count] + "'";
    }
  }

  Logger.log(number);
  Logger.log(array);
  for (var i = 0; i < number; i++) {
    Logger.log(values[i]);

  }

  var form = FormApp.create("Cleaners");

  var formid = form.getId();

  var dropdown1 = form.addListItem();

  dropdown1.setTitle("Choose a Cleaner:");

  dropdown1.setChoices([
    dropdown1.createChoice(values[0]),
    dropdown1.createChoice(values[1]),
    dropdown1.createChoice(values[2]),
    dropdown1.createChoice(values[3]),
    dropdown1.createChoice(values[4]),
    dropdown1.createChoice(values[5]),
    dropdown1.createChoice(values[6]),
    dropdown1.createChoice(values[7]),
    dropdown1.createChoice(values[8]),
    dropdown1.createChoice(values[9]),
    dropdown1.createChoice(values[10])
  ]);

}

您可以使用以下代码生成所需的超链接。

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