この記事では、2つのカスタム関数を作成します。1つは列番号を列参照に変換する関数で、もう1つは列参照を列番号に変換する関数です。

この例の生データは2つのサンプルデータで構成されています。1つのサンプルデータにはランダムな列番号が含まれ、2番目のサンプルデータにはランダムな列参照が含まれています。

ArrowMain

2つのカスタム関数「ColNoToColRef」と「ColRefToColNo」を作成しました。 「colNoToColRef」関数は、列番号を列参照に変換するために使用されます。入力として整数値を取ります。

同様に、「ColRefToColNo」関数は、列参照を列番号に変換するために使用されます。

ArrowOutput

コードの説明

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

上記のコードは、指定されたセルのアドレスを取得するために使用されます。

Left(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]までご連絡ください