Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 仅当特定单元格包含特定值时才执行函数_Excel_Vba_If Statement - Fatal编程技术网

Excel 仅当特定单元格包含特定值时才执行函数

Excel 仅当特定单元格包含特定值时才执行函数,excel,vba,if-statement,Excel,Vba,If Statement,下面的代码非常有效。 现在,我只希望它在单元格B2显示“2020”时工作。 例如,如果单元格B2显示“2021”,则我希望该值转到工作表“2021” 我怎样才能做到这一点呢?我自己解决了 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim xRta As Variant If Selection.Count = 1 Then If ActiveSheet.Range("B2") = "20

下面的代码非常有效。
现在,我只希望它在单元格
B2
显示“2020”时工作。
例如,如果单元格
B2
显示“2021”,则我希望该值转到工作表“2021”

我怎样才能做到这一点呢?

我自己解决了

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Dim xRta As Variant
     If Selection.Count = 1 Then
     If ActiveSheet.Range("B2") = "2020" Then
         If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
             xRta = Application.InputBox("Insert your value please")
             Sheets("2020").Range(Target.Address).Value = xRta
            End If
         End If
     End If

Dim xRtb As Variant
     If Selection.Count = 1 Then
     If ActiveSheet.Range("B2") = "2021" Then
         If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
             xRtb = Application.InputBox("Insert your value please")
             Sheets("2021").Range(Target.Address).Value = xRtb
            End If
         End If
     End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Dim xRta As Variant
     If Selection.Count = 1 Then
     If ActiveSheet.Range("B2") = "2020" Then
         If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
             xRta = Application.InputBox("Insert your value please")
             Sheets("2020").Range(Target.Address).Value = xRta
            End If
         End If
     End If

Dim xRtb As Variant
     If Selection.Count = 1 Then
     If ActiveSheet.Range("B2") = "2021" Then
         If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
             xRtb = Application.InputBox("Insert your value please")
             Sheets("2021").Range(Target.Address).Value = xRtb
            End If
         End If
     End If
End Sub