Przejdź do głównej zawartości

 Jak wybrać kolumnę na podstawie nagłówka kolumny w programie Excel?

Przypuśćmy, że masz duży arkusz roboczy zawierający wiele kolumn, a teraz chciałbyś wybrać określoną kolumnę na podstawie nazwy nagłówka kolumny. Aby znaleźć kolumnę po kolumnie w dużym arkuszu, zmarnuje dużo czasu, w tym artykule przedstawię szybką metodę rozwiązania tego zadania w programie Excel.

Wybierz kolumnę na podstawie nazwy nagłówka kolumny z kodem VBA


Wybierz kolumnę na podstawie nazwy nagłówka kolumny z kodem VBA

Poniższy kod VBA może pomóc w wyborze kolumn na podstawie określonej nazwy nagłówka, wykonaj następujące czynności:

1. Przytrzymaj ALT + F11 klawisze, aby otworzyć Microsoft Visual Basic for Applications okno.

2. Kliknij wstawka > Modułi wklej następujący kod w Moduł Okno.

Kod VBA: Wybierz kolumnę na podstawie nazwy nagłówka kolumny:

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: W powyższym kodzie, A1: P1 to zakres nagłówków, z których chcesz wybrać kolumny, a „Imię”W scenariuszu xStr = "Imię" to nazwa nagłówka, na podstawie którego chcesz wybrać kolumny. Zmień je według swoich potrzeb.

3. Po skopiowaniu i wklejeniu kodu naciśnij F5 klucz do uruchomienia tego kodu, a wszystkie kolumny o określonej nazwie nagłówka zostały wybrane jednocześnie, patrz zrzut ekranu:

Najlepsze narzędzia biurowe

🤖 Pomocnik AI Kutools: Zrewolucjonizuj analizę danych w oparciu o: Inteligentne wykonanie   |  Wygeneruj kod  |  Twórz niestandardowe formuły  |  Analizuj dane i generuj wykresy  |  Wywołaj funkcje Kutools...
Popularne funkcje: Znajdź, wyróżnij lub zidentyfikuj duplikaty   |  Usuń puste wiersze   |  Łącz kolumny lub komórki bez utraty danych   |   Okrągły bez wzoru ...
Super wyszukiwanie: Wiele kryteriów VLookup    Wiele wartości VLookup  |   Przeglądanie pionowe na wielu arkuszach   |   Wyszukiwanie rozmyte ....
Zaawansowana lista rozwijana: Szybko twórz listę rozwijaną   |  Zależna lista rozwijana   |  Lista rozwijana wielokrotnego wyboru ....
Menedżer kolumn: Dodaj określoną liczbę kolumn  |  Przesuń kolumny  |  Przełącz stan widoczności ukrytych kolumn  |  Porównaj zakresy i kolumny ...
Polecane funkcje: Fokus siatki   |  Widok projektu   |   Duży pasek formuły    Menedżer skoroszytów i arkuszy   |  Biblioteka zasobów (Automatyczny tekst)   |  Selektor dat   |  Połącz arkusze   |  Szyfruj/odszyfruj komórki    Wysyłaj e-maile według listy   |  Super filtr   |   Specjalny filtr (filtruj pogrubienie/kursywa/przekreślenie...) ...
15 najlepszych zestawów narzędzi12 Tekst Tools (Dodaj tekst, Usuń znaki, ...)   |   50 + Wykres rodzaje (Wykres Gantta, ...)   |   40+ Praktyczne Wzory (Oblicz wiek na podstawie urodzin, ...)   |   19 Wprowadzenie Tools (Wstaw kod QR, Wstaw obraz ze ścieżki, ...)   |   12 Konwersja Tools (Liczby na słowa, Przeliczanie walut, ...)   |   7 Połącz i podziel Tools (Zaawansowane wiersze łączenia, Podział komórki, ...)   |   ... i więcej

Zwiększ swoje umiejętności Excela dzięki Kutools for Excel i doświadcz wydajności jak nigdy dotąd. Kutools dla programu Excel oferuje ponad 300 zaawansowanych funkcji zwiększających produktywność i oszczędzających czas.  Kliknij tutaj, aby uzyskać funkcję, której najbardziej potrzebujesz...

Opis


Karta Office wprowadza interfejs z zakładkami do pakietu Office i znacznie ułatwia pracę

  • Włącz edycję i czytanie na kartach w programach Word, Excel, PowerPoint, Publisher, Access, Visio i Project.
  • Otwieraj i twórz wiele dokumentów w nowych kartach tego samego okna, a nie w nowych oknach.
  • Zwiększa produktywność o 50% i redukuje setki kliknięć myszką każdego dnia!
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