이 기사에서는 열 번호를 열 참조로 변환하는 함수와 열 참조를 열 번호로 변환하는 다른 함수의 두 가지 사용자 지정 함수를 만듭니다.

이 예의 원시 데이터는 두 개의 샘플 데이터로 구성되며 하나의 샘플 데이터에는 임의의 열 번호가 포함되고 두 번째 샘플 데이터에는 임의의 열 참조가 포함됩니다.

ArrowMain

두 개의 사용자 지정 함수 “ColNoToColRef”및 “ColRefToColNo”를 만들었습니다. “ColNoToColRef”함수는 열 번호를 열 참조로 변환하는 데 사용됩니다. 정수 값을 입력으로 사용합니다.

마찬가지로 “ColRefToColNo”함수는 열 참조를 열 번호로 변환하는 데 사용됩니다.

ArrowOutput

코드 설명

Cells (1, ColNo) .Address (True, False, xlA1)

위의 코드는 지정된 셀의 주소를 가져 오는 데 사용됩니다.

왼쪽 (ColNoToColRef, InStr (1, ColNoToColRef, “$”)-1)

위 코드는 “$”기호의 왼쪽에있는 문자를 추출하는 데 사용됩니다.

아래 코드를 따르세요

Option Explicit

Function ColNoToColRef(ColNo As Long) As String





If ColNo < 1 Or ColNo > Columns.Count Then



ColNoToColRef = "Invalid input value"



Else



'Finding the address of cell in the first row based on the specified column number

ColNoToColRef = Cells(1, ColNo).Address(True, False, xlA1)



'Extracting the column name from the address

ColNoToColRef = Left(ColNoToColRef, InStr(1, ColNoToColRef, "$") - 1)



End If



End Function

Function ColRefToColNo(ColRef As String)

Dim Rng As Range



On Error GoTo LastPart



'Assigning cell in the first row of the specified column reference as range

Set Rng = Range(ColRef & "1")



'Getting the column number of the specified range

ColRefToColNo = Rng.Column



Exit Function



LastPart:

ColRefToColNo = "Invalid input value"



End Function

이 블로그가 마음에 들면 Facebook 및 Facebook에서 친구들과 공유하십시오.

여러분의 의견을 듣고 싶습니다. 작업을 개선하고 더 나은 서비스를 제공 할 수있는 방법을 알려주십시오. [email protected]로 문의 해주세요