|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
操作表2代码:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
If Target.Column = 1 Then Target.Offset(0, 1).Resize(1, 2).ClearContents
If Target.Column = 2 Then Target.Offset(0, 1).ClearContents
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
On Error Resume Next
Dim col As New Collection, ran As Range, c As Byte, val As String
If Target.Column = 1 Then
For Each ran In Sheets(1).Range("e2:e9")
col.Add ran, key:=CStr(ran)
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 2 Then
For Each ran In Sheets(1).Range("f2:f9")
If ran.Offset(0, -1) = Target.Offset(0, -1) Then
col.Add ran ', key:=CStr(ran)
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 3 Then
For Each ran In Sheets(1).Range("g2:k9")
If ran.Offset(0, -1).Row = Target.Offset(0, -1).Row And ran.Offset(0, -2).Row = Target.Offset(0, -2).Row Then
col.Add ran ', key:=CStr(ran)
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
End If
End Sub
操作表代码:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
If Target.Column = 1 Then Target.Offset(0, 1).Resize(1, 2).ClearContents
If Target.Column = 2 Then Target.Offset(0, 1).ClearContents
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
On Error Resume Next
Dim col As New Collection, ran As Range, c As Byte, val As String
If Target.Column = 1 Then
For Each ran In Sheets(1).Range("a2:a25")
col.Add ran, key:=CStr(ran)
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 2 Then
For Each ran In Sheets(1).Range("b2:b25")
If ran.Offset(0, -1) = Target.Offset(0, -1) Then
col.Add ran, key:=CStr(ran)
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 3 Then
For Each ran In Sheets(1).Range("c2:c25")
If ran.Offset(0, -1) = Target.Offset(0, -1) And ran.Offset(0, -2) = Target.Offset(0, -2) Then
' If ran.Offset(0, -2) & ran.Offset(0, -1) = Target.Offset(0, -2) & Target.Offset(0, -1) Then
col.Add ran ', key:=CStr(ran)
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
End If
End Sub
|
|