Vai al contenuto principale

 Come selezionare una colonna in base all'intestazione della colonna in Excel?

Supponendo di avere un foglio di lavoro di grandi dimensioni che contiene più colonne e ora, si desidera selezionare la colonna specifica in base al nome di un'intestazione di colonna. Per trovare la colonna per colonna in un foglio di lavoro di grandi dimensioni ci vorrà molto tempo, in questo articolo introdurrò un metodo rapido per risolvere questo lavoro in Excel.

Seleziona una colonna in base al nome dell'intestazione della colonna con il codice VBA


Seleziona una colonna in base al nome dell'intestazione della colonna con il codice VBA

Il seguente codice VBA può aiutarti a selezionare le colonne in base a un nome di intestazione specifico, per favore fai come segue:

1. Tieni premuto il ALT + F11 chiavi per aprire il Microsoft Visual Basic, Applications Edition finestra.

2. Clic inserire > Modulie incolla il codice seguente nel file Moduli Finestra.

Codice VBA: seleziona la colonna in base al nome dell'intestazione della colonna:

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:: Nel codice sopra, A1: P1 è l'intervallo di intestazioni da cui selezionare le colonne e "Nome"Nello script xStr = "Nome" è il nome dell'intestazione su cui desideri selezionare le colonne. Si prega di modificarli secondo le proprie necessità.

3. Dopo aver copiato e incollato il codice, premere F5 chiave per eseguire questo codice e tutte le colonne con il nome dell'intestazione specifica sono state selezionate contemporaneamente, vedi screenshot:

I migliori strumenti per la produttività in ufficio

🤖 Assistente AI di Kutools: Rivoluziona l'analisi dei dati basandosi su: Esecuzione intelligente   |  Genera codice  |  Crea formule personalizzate  |  Analizzare i dati e generare grafici  |  Richiama le funzioni di Kutools...
Funzioni popolari: Trova, evidenzia o identifica i duplicati   |  Elimina righe vuote   |  Combina colonne o celle senza perdere dati   |   Round senza formula ...
Super ricerca: VLookup a criteri multipli    VLookup a valori multipli  |   VLookup su più fogli   |   Ricerca fuzzy ....
Elenco a discesa avanzato: Crea rapidamente un elenco a discesa   |  Elenco a discesa dipendente   |  Elenco a discesa a selezione multipla ....
Gestore di colonna: Aggiungi un numero specifico di colonne  |  Sposta colonne  |  Attiva/disattiva lo stato di visibilità delle colonne nascoste  |  Confronta intervalli e colonne ...
Funzionalità in primo piano: Messa a fuoco della griglia   |  Vista di progettazione   |   Grande barra delle formule    Gestore di cartelle di lavoro e fogli   |  Resource Library (Testo automatico)   |  Date picker   |  Combina fogli di lavoro   |  Crittografa/decrittografa le celle    Invia e-mail per elenco   |  Super filtro   |   Filtro speciale (filtro grassetto/corsivo/barrato...) ...
I 15 migliori set di strumenti12 Testo Strumenti (aggiungi testo, Rimuovi personaggi, ...)   |   50+ Grafico Tipi (Diagramma di Gantt, ...)   |   40+ Pratico Formule (Calcola l'età in base al compleanno, ...)   |   19 Inserimento Strumenti (Inserisci il codice QR, Inserisci immagine dal percorso, ...)   |   12 Conversione Strumenti (Numeri in parole, Conversione di valuta, ...)   |   7 Unisci e dividi Strumenti (Combina righe avanzate, Celle divise, ...)   |   ... e altro ancora

Potenzia le tue competenze di Excel con Kutools per Excel e sperimenta l'efficienza come mai prima d'ora. Kutools per Excel offre oltre 300 funzionalità avanzate per aumentare la produttività e risparmiare tempo.  Fai clic qui per ottenere la funzionalità di cui hai più bisogno...

Descrizione


Office Tab porta l'interfaccia a schede in Office e semplifica notevolmente il tuo lavoro

  • Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Publisher, Access, Visio e Project.
  • Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
  • Aumenta la produttività del 50% e riduce ogni giorno centinaia di clic del mouse!
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