Copy columns by headers

Solved
alberkrimo Posted messages 20 Status Member -  
alberkrimo Posted messages 20 Status Member -
Hello,

I am reaching out to you hoping to get some help. As a beginner in VBA, I am using a code to search for a column in one sheet and then copy and paste it into another sheet. This code allows me to search for only one column at a time.

What I want to do is select multiple different columns based on their headers.
Does anyone have a code for that?

The code is as follows:

Sub Col_Select() Dim Cel As Range Set Cel = Cells.Find(what:="Bât") If Not Cel Is Nothing Then Cells(1, Cel.Column).Resize(Cells(Rows.Count, Cel.Column).End(xlUp).Row).Select Else MsgBox "Not found the name " Exit Sub End If End Sub


Thank you in advance.

Best regards.

2 answers

  1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    Where are you with this request, as I have not received a response from you?
    https://forums.commentcamarche.net/forum/affich-34305471-capacite-combobox

    For copying columns, naming the columns to copy would make it simpler
    you would have a code like this, copy column D and H to L and M
    Sub Macro2() Range("Col_D" & "," & "Col_H").Copy Range("L1") End Sub 
    0
    1. alberkrimo Posted messages 20 Status Member
       
      Hello,

      I just tried this code. It doesn't work. Indeed, I would like a code that will recognize the titles of my columns and then copy them. Because the columns are likely to change as it is an Excel extraction coming from software.

      Thank you

      Best regards.
      0
  2. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
     
    Hello,

    An example to adapt.
    .Row can be replaced by .Column (for example)...

    --
    Before, I never managed to finish my sentences... but now I
    0
    1. alberkrimo Posted messages 20 Status Member
       
      https://www.cjoint.com/c/GAEkNhdRk5k

      Here is an example. I would like to copy column A "Request Number" and B "Status", knowing that these two columns will change next week.

      I cannot adapt your code. I'm not very good at VBA.

      Can you help me please?

      Thank you
      0
      1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773 > alberkrimo Posted messages 20 Status Member
         
        For this, your code provided in the first message is enough.
        It's up to you to adapt it by doing it twice...
        Sub Col_Select() Dim Cel As Range Set Cel = Rows(1).Cells.Find(what:="Request Number") If Not Cel Is Nothing Then 'HERE copy/paste code Else MsgBox "Request Number not found" End If Set Cel = Rows(1).Cells.Find(what:="Status") If Not Cel Is Nothing Then 'HERE copy/paste code Else MsgBox "Status not found" End If End Sub
        0
    2. alberkrimo Posted messages 20 Status Member
       
      You're welcome.
      0