이 문제를 해결할 수있는 여러 가지 방법이 있습니다 (예 : 특정 필터 조건이있는 피벗 테이블, 일치하는 항목을 찾아 원하는 출력으로 인쇄하는 등). 일반적으로 Range.Find
메서드를 사용하고 모든 일치를 반복하는 것은 나쁜 생각이 아닙니다.
Excel을 많이 사용하는 대부분의 사람들이 궁극적으로는 vLookup
을 사용하지만 결국에는 일정한 프로그래밍 시간과 에너지가 필요합니다. 저는 항상 vLookup
에 만족하지 않았습니다. 이는 vba Range.Find
방법과 비교하여 너무 제한적입니다. 그저 크리스마스이기 때문에 실제로 돈을 지불해야하는 일이 없어 졌기 때문에 문제를 해결하는 데 도움이되는 작은 보석이 있습니다.
반환 할 일치하는 숫자를 지정하고 행 또는 열에 사용자 지정 오프셋을 반환하여 값으로 검색 할 수있게 해주는 UDF 조회입니다. 변수 matchNum
을 증가 시키면 해당 범위의 모든 일치 항목이 제공되고 적절한 오프셋을 사용하여 원하는 열을 반환 할 수 있습니다.
Range.Find
메서드를 사용하면 코드를 사용하여 UDF 조회 기능을 사용하지 않고 원하는대로 정확히 워크 시트를 채울 수있는 방법을 알 수 있습니다. 나는 그것을 독자를위한 운동으로 남겨 둘 것이다.
'################################################################################################################################
' findwhat: the value you want to find. (needle)
' where: the range you want to look for findwhat (haystack)
' matchNum: if the needle is found more than once in the haystack, find the nth target.
' rowoffset: offset your return value from the target, positive will return the value of the cell below the target.
' columoffset: offset your return value from the target, positive will return the value of the cell to the right of the target.
'################################################################################################################################
Public Function powerLookup(findwhat As Variant, where As Range, Optional matchNum As Long = 1, Optional rowOffset As Long = 0, Optional columnOffset As Long = 0) As Variant
Dim rngResult As Range, firstAddress As String
Set rngResult = Nothing
On Error GoTo Errorhandler ' if something breaks return #NA (will definitely happen if first find call fails)
Do While matchNum > 0 'loop through the matches until there are no matches or we've reached the target matchnumber
'the first time, rngResult will be Nothing, so the first find can't have rngResult as an input.
With where
If rngResult Is Nothing Then
Set rngResult = .find(findwhat, , xlValues)
firstAddress = rngResult.Address 'keep this to know if we've looped past the start
Else
'if rngResult is not nothing we've already found a match, find the next match until matchnum is 0
Set rngResult = .find(findwhat, rngResult, xlValues)
If rngResult.Address = firstAddress Then
'if we reach the first address we've looped around, no more matches found, throw #NA error
powerLookup = CVErr(xlErrNA)
Exit Function
End If
End If
End With
matchNum = matchNum - 1
Loop
powerLookup = rngResult.offset(rowOffset, columnOffset).Value 'offset the output
Exit Function
Errorhandler:
powerLookup = CVErr(xlErrNA)
End Function
A 열을 A 열로 다시 구성하면 VBA가 필요없는 V- 조회를 사용할 수 있습니다. – serakfalcon
무엇을 시도 했습니까? 모든 것이 당신을 위해 끝나기를 기대하기보다는 당신의 노력을 포함하는 것이 더 낫습니다. COUNTIF와 같은 기능과 여기에서 도움이 될 수있는 피벗 테이블과 같은 도구가 있습니다. – SJR
나는 VBA를 사용하지 않고 시도했지만 이전 질문에서 내가받은 유일한 응답은 VBA 없이는 불가능하다는 것입니다. 열 순서를 바꾸는 것은 실행 가능한 옵션이 아닙니다. – hsquared