Excel 创建一个按钮,该按钮在按下时向单元格添加值
我创建了一个按钮,然后给它分配了一个宏,它位于module1中。当我在某个范围内选择一个单元格,然后按下按钮时,我希望给该单元格一个值。如果未选择范围内的单元格,则按下按钮应弹出一个消息框。我刚刚开始编写代码,已经遇到了一个问题Excel 创建一个按钮,该按钮在按下时向单元格添加值,excel,vba,Excel,Vba,我创建了一个按钮,然后给它分配了一个宏,它位于module1中。当我在某个范围内选择一个单元格,然后按下按钮时,我希望给该单元格一个值。如果未选择范围内的单元格,则按下按钮应弹出一个消息框。我刚刚开始编写代码,已经遇到了一个问题 If Intersect(Target, Range("D12:AS23")) Is Nothing Then MsgBox "Please select a date.", , "Error" Exit Sub Else 编辑:我
If Intersect(Target, Range("D12:AS23")) Is Nothing Then
MsgBox "Please select a date.", , "Error"
Exit Sub
Else
编辑:我已经通过了这个问题,现在使用活动单元格而不是目标。但现在我又在下一点挣扎。它表示应用程序/对象定义错误并突出显示这一行。完整代码如下
Cells(LastRow, 2).Value = Application.SumIf(Sheets("Settings").range("ListEmployeeNames"), Cells(3, 2).Value, Sheets("Settings").range("ListEmployeeID"))
我自己完成了90%的工作,感谢@LeeLiFong向我透露了“目标”是一个问题,也感谢@BraX向我指出了正确的方向,包括关于鉴定事物的论点。我不能再接受48小时的答复
Dim SelectedDate As Date
SelectedDate = Cells(ActiveCell.Row, 2) - 1 + ActiveCell.Value
If intersect(ActiveCell, range("D12:AS23")) Is Nothing Then
MsgBox "Please select a date.", , "Error"
Exit Sub
Else
Call SmoothCodeStart
With Sheets("Notes")
xt4 = Application.InputBox("Insert your Comment here", "Comment")
If xt4 = vbNullString Or xt4 = False Then
End If
Dim LastRow As Integer
LastRow = 2 + .listobjects("TblNotes").range.rows.Count
.Cells(LastRow, 2).Value = Application.SumIf(Sheets("Settings").range("ListEmployeeNames"), Cells(3, 2).Value, Sheets("Settings").range("ListEmployeeID"))
.Cells(LastRow, 4).Value = SelectedDate
.Cells(LastRow, 6).Value = xt4
End With
Call SmoothCodeEnd
End If
问题是什么?您在哪个事件处理程序中使用此代码?目标?在模块1中?我需要一个错误对象如果它在模块中,那么您必须包含限定
范围
对象和目标的参数,否则函数将不知道这些东西指的是什么。您的代码应该没有问题。只需在工作表的SelectionChange事件中创建代码,而不需要在模块中创建。
Dim SelectedDate As Date
SelectedDate = Cells(ActiveCell.Row, 2) - 1 + ActiveCell.Value
If intersect(ActiveCell, range("D12:AS23")) Is Nothing Then
MsgBox "Please select a date.", , "Error"
Exit Sub
Else
Call SmoothCodeStart
With Sheets("Notes")
xt4 = Application.InputBox("Insert your Comment here", "Comment")
If xt4 = vbNullString Or xt4 = False Then
End If
Dim LastRow As Integer
LastRow = 2 + .listobjects("TblNotes").range.rows.Count
.Cells(LastRow, 2).Value = Application.SumIf(Sheets("Settings").range("ListEmployeeNames"), Cells(3, 2).Value, Sheets("Settings").range("ListEmployeeID"))
.Cells(LastRow, 4).Value = SelectedDate
.Cells(LastRow, 6).Value = xt4
End With
Call SmoothCodeEnd
End If