Enter number into a cell and search an array for that number using a 2D range

那年仲夏 提交于 2020-01-07 03:49:20

问题


I am reconciling accounts and was wondering if there is a formula/VBA I can use to search for certain values, as the find function doesn't work?

For example: enter -54.12 into a cell, formula/VBA searches an array for that number.

This may explain better what I'm trying to achieve, albeit more of an explanation than coding:

Sub Test()

Dim Value As Integer
Dim Account As Range
Dim Lookup As Boolean

Value = Range("D8")
Account = Range("E:E")

Set Value = A
Set Account = B
' IF A is present in B then look up = true
' if look up is true then scroll to cell

End Sub

I could then set a Private Sub by value change to do the same thing each time a new value is entered?


回答1:


Lifted directly from https://stackoverflow.com/a/19126873/1505120 (where I forgot to mention the search term goes in G1):

Lifted direct from MrExcel

Assuming data array is in A1:D5:

In G2: =COUNTIF(B2:D5,G1)

In G4: =IF(ROWS($G$4:G4)>$G$2,"",INDEX($A$2:$A$5,INT(SMALL(IF($B$2:$D$5=$G$1,(ROW($B$2:$D$5)-ROW($B$2)+1)*10^5+COLUMN($B$2:$D$5)-COLUMN($B$2)+1),ROWS($G$4:G4))/10^5)))

In H4: =IF(ROWS($G$4:G4)>$G$2,"",INDEX($B$1:$D$1,MOD(SMALL(IF($B$2:$D$5=$G$1,(ROW($B$2:$D$5)-ROW($B$2)+1)*10^5+COLUMN($B$2:$D$5)-COLUMN($B$2)+1),ROWS($G$4:G4)),10^5)))

The latter two entered with Ctrl+Shift+Enter and copied down.



来源:https://stackoverflow.com/questions/34224403/enter-number-into-a-cell-and-search-an-array-for-that-number-using-a-2d-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!