Excel公式查找最大/最小值

时间:2014-07-19 02:20:58

标签: excel

我有两列,X和Y列,它有数据点,当绘制图形(200多个数据点)时,它绘制一个手绘方形图。我的职责是找到四个角的(X,y)点。为此,我需要找到X的最小值,以及获得左上角点的最大Y点

我需要确定最大X点和最大Y值以找到左下角点,依此类推。

但是,我不知道怎么做,因为我可以找到X / Y的最大/最小值,但是我找不到4对角。有什么建议?

谢谢

1 个答案:

答案 0 :(得分:0)

如果边总是与坐标系平行,则点为:

A: min(x), min(y)
B: min(x), max(y)
C: max(x), min(y)
D: max(x), max(y)

如果两边不平行,那么需要更聪明一点:

A: min(x), the value of y where x is minimum
B: the value of x where y is maximum,  max(y)
C: max(x), the value of y where x is maximum
D: the value of x where y is minimum,  min(y)

仅当min或max x中只有一个y,并且min和max x只有一个x时,这才有效。代码需要完成所有这些。

假设坐标在Sheet1中,x坐标在范围名称“x”中,y坐标在范围名称“y”中,以下代码应返回角坐标:

Option Explicit
Sub addresses()
Dim wks As Worksheet
Dim sXMinAddress As String, sXMaxAddress As String
Dim sXMinAddressY As String, sXMaxAddressY As String
Dim xMin As Double, xMax As Double
Dim xMinY As Double, xMaxY As Double
Dim sYMinAddress As String, sYMaxAddress As String
Dim sYMinAddressX As String, sYMaxAddressX As String
Dim yMin As Double, yMax As Double
Dim yMinX As Double, yMaxX As Double
Dim testAddress As String, i As Integer

Set wks = Worksheets("Sheet1")

sXMaxAddress = wks.Range("x").Cells(1, 1).Address
sXMinAddress = wks.Range("x").Cells(1, 1).Address
sXMaxAddressY = wks.Range(sXMaxAddress).Offset(0, 1).Address
sXMinAddressY = wks.Range(sXMinAddress).Offset(0, 1).Address
xMax = wks.Range(sXMaxAddress).Value
xMin = wks.Range(sXMinAddress).Value

sYMaxAddress = wks.Range("y").Cells(1, 2).Address
sYMinAddress = wks.Range("y").Cells(1, 2).Address
sYMaxAddressX = wks.Range(sYMaxAddress).Offset(0, -1).Address
sYMinAddressX = wks.Range(sYMinAddress).Offset(0, -1).Address
yMax = wks.Range(sYMaxAddress).Value
yMin = wks.Range(sYMinAddress).Value

For i = 2 To wks.Range("x").Count
    testAddress = wks.Range("x").Cells(i, 1).Address
    If wks.Range(testAddress).Value < xMin Then
        xMin = wks.Range(testAddress).Value
        sXMinAddress = testAddress
        sXMinAddressY = wks.Range(testAddress).Offset(0, 1).Address
        xMinY = wks.Range(sXMinAddressY).Value
    End If
    If wks.Range(testAddress).Value > xMax Then
        xMax = wks.Range(testAddress).Value
        sXMaxAddress = testAddress
        sXMaxAddressY = wks.Range(testAddress).Offset(0, 1).Address
        xMaxY = wks.Range(sXMaxAddressY).Value
    End If
Next i

For i = 2 To wks.Range("y").Count
    testAddress = wks.Range("y").Cells(i, 1).Address
    If wks.Range(testAddress).Value < yMin Then
        yMin = wks.Range(testAddress).Value
        sYMinAddress = testAddress
        sYMinAddressX = wks.Range(testAddress).Offset(0, -1).Address
        yMinX = wks.Range(sYMinAddressX).Value
    End If
    If wks.Range(testAddress).Value > yMax Then
        yMax = wks.Range(testAddress).Value
        sYMaxAddress = testAddress
        sYMaxAddressX = wks.Range(testAddress).Offset(0, -1).Address
        yMaxX = wks.Range(sYMaxAddressX).Value
    End If
Next i
Debug.Print "Vertices:"
Debug.Print "A: " & xMin & ", " & xMinY
Debug.Print "B: " & xMax & ", " & xMaxY
Debug.Print "C: " & yMaxX & ", " & yMax
Debug.Print "D: " & yMinX & ", " & yMin

Debug.Print "Sheet addresses"
Debug.Print "A: " & sXMinAddress & "," & sXMinAddressY
Debug.Print "B: " & sYMaxAddressX & "," & sYMaxAddress
Debug.Print "C: " & sXMaxAddress & "," & sXMaxAddressY
Debug.Print "D: " & sYMinAddressX & "," & sYMinAddress
End Sub