メインコンテンツへスキップ

 Excelの列ヘッダーに基づいて列を選択するにはどうすればよいですか?

複数の列を含む大きなワークシートがあり、列ヘッダー名に基づいて特定の列を選択するとします。 大きなワークシートで列ごとに見つけるのは時間がかかります。この記事では、Excelでこのジョブを解決する簡単な方法を紹介します。

VBAコードを含む列ヘッダー名に基づいて列を選択します


VBAコードを含む列ヘッダー名に基づいて列を選択します

次のVBAコードは、特定のヘッダー名に基づいて列を選択するのに役立ちます。次のようにしてください。

1。 を押し続けます Alt + F11 キーを押して アプリケーション向け Microsoft Visual Basic 窓。

2に設定します。 OK をクリックします。 インセット > モジュール、次のコードをに貼り付けます モジュール 窓。

VBAコード:列ヘッダー名に基づいて列を選択します:

Sub FindAddressColumn()
'Updateby Extendoffcie
    Dim xRg As Range
    Dim xRgUni As Range
    Dim xFirstAddress As String
    Dim xStr As String
    On Error Resume Next
    xStr = "Name"
    Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlWhole, , , True)
    If Not xRg Is Nothing Then
        xFirstAddress = xRg.Address
        Do
            Set xRg = Range("A1:P1").FindNext(xRg)
            If xRgUni Is Nothing Then
                Set xRgUni = xRg
            Else
                Set xRgUni = Application.Union(xRgUni, xRg)
            End If
        Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)
    End If
    xRgUni.EntireColumn.Select
End Sub

Note:上記のコードでは、 A1:P1 列を選択するヘッダーの範囲であり、「お名前スクリプト内の」 xStr = "名前" に基づいて列を選択するヘッダー名です。 必要に応じて変更してください。

3。 コードをコピーして貼り付けた後、を押してください F5 このコードを実行するためのキー。特定のヘッダー名を持つすべての列が一度に選択されています。スクリーンショットを参照してください。

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Good job, works great!
This comment was minimized by the moderator on the site
Hi everyone...

How can I make it so that only the column data is selected, without the header, instead of the entire column?

Thanks
This comment was minimized by the moderator on the site
Thank you spo much saved my day !
This comment was minimized by the moderator on the site
Thanks - at first it seemed to work, but then I noticed it didn't include the first-found column - it started with the second and on.

Which is explainable: you set xRg twice, but you do not include the first one.

TBH: initially I thought it worked fine, but then that 'error' showed up and I had to figure out why....

I fixed it as follows: (in Excel the comments will turn green and show my edits: '***)

<code>

Sub FindAddressColumn()

'Updateby Extendoffcie

Dim xRg As Range

Dim xRgUni As Range

Dim xFirstAddress As String

Dim xStr As String

On Error Resume Next
xStr = "Name"
Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlPart, , , True)

If Not xRg Is Nothing Then

xFirstAddress = xRg.Address

Do

If xRgUni Is Nothing Then

Set xRgUni = Range(xFirstAddress) '*** Using the first-found range

Else

Set xRgUni = Application.Union(xRgUni, xRg)

End If



Set xRg = Range("A1:P1").FindNext(xRg) '*** Now searching for the second/next ones



Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)

End If

xRgUni.EntireColumn.Select

End Sub


</code>

This now works for me, but I hope to learn from you if I overlooked anything.
This comment was minimized by the moderator on the site
Hi, Can you suggest the macro for the issue mentioned below by Shaun
This comment was minimized by the moderator on the site
Hi, looks great work. But I am getting the compile error: Argument not optional while highlighting the ". Union". What should I do?
This comment was minimized by the moderator on the site
This is great, but what if I wanted to copy say, CustomerName, OrderNumber, OrderDate, FulfillmentDate, OrderStatus from Sheet1 with many more columns.
How do I only specify the columns that are needed to be copied to another sheet? This seems to only specify one column.
I have tried some things, but the problem is more where the columns are not necesarily in the same order on the source sheet. The source data in Sheet1 is pasted from the Windows clipboard to the Excel sheet.

It may be, CustomerName, OrderDate, OrderNumber, OrderStatusm FulfillmentDate or some other order.

I need them to be copied into a new sheet in a specific order as there are formulas in Sheet2 that are applied to what is copied from Sheet1 to Sheet2.

Other than shifting columns around after pasting from Windows clipboard, I can't figure this out.

Please help if possible.

Thank you kindly for the above though.
This comment was minimized by the moderator on the site
Dim xRg As Range
Dim xRgUni As Range
Dim xFirstAddress As String
Dim xStr As String

Are variables being declared.

Hence to use the above code again for another column you would have to declare new variables.

Dim xRg2 As Range
Dim xRgUni2 As Range
Dim xFirstAddress2 As String
Dim xStr2 As String

And replace every instance of xRg with xRg2 in the code ect. Does that make sense?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations