엑셀 매크로 vba 함수 indexof, lastindexof
Function indexOf(cell, textToFind)
'cell의 값을 가져옴
Dim val
val = Range(cell, cell).Value
'cell의 값 1번째 글자부터 textToFind 값 찾기
Dim idx
idx = InStr(1, val, textToFind, vbTextCompare)
'인덱스 값을 리턴
indexOf = idx
End Function
'========================================
Function lastIndexOf(cell, textToFind)
'cell의 값을 가져옴
Dim val
val = Range(cell, cell).Value
'cell의 값 뒤에서부터(-1) textToFind 값 찾기
Dim idx
idx = InStrRev(val, textToFind, -1, vbTextCompare)
'인덱스 값을 리턴
lastIndexOf = idx
End Function
'========================================
참고) inStr 함수
앞에서부터 문자열을 찾는 함수. 사용방법은 다음과 같다.
inStr(찾기시작위치, 대상텍스트, 찾을문자열, 텍스트비교방식)
참고 2) inStrRev 함수
뒤에서부터 문자열을 찾는 함수. 사용방법은 다음과 같다.
inStrRev(대상텍스트, 찾을문자열, 찾기시작위치, 텍스트비교방식)
출처: https://blog.naver.com/PostView.naver?blogId=bb_&logNo=221513849269&parentCategoryNo=&categoryNo=84&viewDate=&isShowPopularPosts=false&from=postView