vba 判断 数据大小和方向?
发布网友
发布时间:2023-07-21 12:54
我来回答
共1个回答
热心网友
时间:2024-11-04 05:17
把下面的代码复制到sheet1代码编辑窗口: Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Count = 1 Then
If Target.Column > 1 And Target.Row > 1 Then
Dim arry As Variant
Dim arr As Range
Dim kk As String
Set arry = Target.Offset(-1, -1).Resize(3, 3)
For Each arr In arry
If Len(arr) > 0 Then
If arr.Address <> arry(2, 2).Address Then
If arr.Column = arry(2, 2).Column Then
If arr.Row < arry(2, 2).Row Then
If arr = arry(2, 2) Then
kk = "等于"
Else
If arr > arry(2, 2) Then
kk = "大于"
Else
kk = "小于"
End If
End If
MsgBox arr & "在的北侧" & kk
Else
If arr = arry(2, 2) Then
kk = "等于"
Else
If arr > arry(2, 2) Then
kk = "大于"
Else
kk = "小于"
End If
End If
MsgBox arr & "在的南侧" & kk
End If
End If
If arr.Row = arry(2, 2).Row Then
If arr.Column < arry(2, 2).Column Then
If arr = arry(2, 2) Then
kk = "等于"
Else
If arr > arry(2, 2) Then
kk = "大于"
Else
kk = "小于"
End If
End If
MsgBox arr & "在的西侧" & kk
Else
If arr = arry(2, 2) Then
kk = "等于"
Else
If arr > arry(2, 2) Then
kk = "大于"
Else
kk = "小于"
End If
End If
MsgBox arr & "在的东侧" & kk
End If
End If
End If
End If
Next
End If
End If
End Sub 点击b所在的单元格会判断b附近和b在同一行和同一列的单元格相对b的位置和大小