请教各位老师,如图表格,使用单元格触发事件怎样写VBA代码?
2个回答
展开全部
给你做了个示例文件。文件中采用了两个方法:
采用工作表函数的办法,见sheet1。其中G列的公式以G2单元格为例:
=IF((D2<B2)*(E2=1),"提前完成",IF((B2=D2)*(E2=1),"及时完成",IF(D2>B2,"滞后完成","")))
shee2采用VBA的方法,只要你A:D列有更新,就会自动更新G列状态,代码如下:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range, i As Integer, str As String
If Not Intersect(Target, Range("A2:D10000")) Is Nothing Then
For Each Cell In Target
i = Cell.Row
str = ""
If Cells(i, 4) < Cells(i, 2) And Cells(i, 5) = 1 Then str = "提前完成"
If Cells(i, 4) = Cells(i, 2) And Cells(i, 5) = 1 Then str = "及时完成"
If Cells(i, 4) > Cells(i, 2) Then str = "滞后完成"
Cells(i, 7) = str
Next Cell
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询