VBA найти ближайшее значение - PullRequest
       19

VBA найти ближайшее значение

0 голосов
/ 20 сентября 2018

У меня есть электронная таблица с 12 столбцами (по 3 столбца), в которой перечислены несколько числовых значений.Мне интересно, как лучше всего отсортировать их, чтобы найти ячейку со значением, близким к 0,5.

Будет ли Vlookup эффективно работать в этой ситуации, или мне лучше перебрать значения с некоторымисвоего рода тест?

Ответы [ 3 ]

0 голосов
/ 20 сентября 2018

используйте это:

=INDEX(B:B,AGGREGATE(15,6,ROW(B2:B21)/(AGGREGATE(15,6,ABS($C$2:$C$21-0.5),1)=ABS($C$2:$C$21-0.5)),1))

И перетащите.

enter image description here

0 голосов
/ 20 сентября 2018

Вы должны будете поместить это в новый модуль в редакторе VBE и просто указать свое имя листа ниже в коде.Он печатает результаты для каждого столбца на 2 строки ниже нижней части каждого столбца.

Sub checker()

Dim row As Long
Dim lastrow As Long
Dim col As Integer
Dim dif As Double
Dim minRow As Long: minRow = 1
Dim startRow

With ThisWorkbook.Worksheets("Enter your sheet name")

' change 1 to 2 below if your first row is a header not a data value
startRow = 1

' starting at col D
For col = 4 to 4 + (3*12) Step 3

    dif = Abs(.Cells(startRow, col) - .5)
    lastRow = .Cells(.Rows.Count, col).End(xlUp).Row

    For row = startRow To lastRow
        If Abs(.Cells(row, col).Value - .5) < dif Then
            dif = Abs(.Cells(row, col).Value - .5)
            minRow = row
        End If
    Next

    ' print result at bottom of ea row
    .Cells(lastRow + 2, col).Value = "Row: " & minRow & ", dif: " & dif & ", value: " & .Cells(minRow, col).Value

Next

End With

End Sub

Удачи

0 голосов
/ 20 сентября 2018
Sub test()
Dim number(100) As Double
Dim target(100) As Range
Dim currcell As Range
Dim currline As Integer
Dim currcolumn As Integer

For currcolumn = 3 To 15 Step 3
    number(currcolumn) = Cells(currcolumn, 2)
    Set target(currcolumn) = Cells(currcolumn, 2)
    For currline = 2 To 20
        If Abs(Cells(currline, currcolumn) - 0.5) < number(currcolumn) Then
            number(currcolumn) = Abs(Cells(currline, currcolumn) - 0.5)
            Set target(currcolumn) = Cells(currline, currcolumn)
        End If
    Next currline
    MsgBox "column: " & currcolumn & " number:" & target(currcolumn) & " at line: " & target(currcolumn).Row
Next currcolumn

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...