VBA Macro for copy columns by name and paste another sheet

sinoyon780

New Member
Joined
Dec 12, 2022
Messages
21
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I'm a beginner in VBA, so please assist me.

I have a workbook, and there is one sheet with many columns with different names name line Quality, RFPC, Hi-Temp, Quality, and Hi-Team (it's a mix column name), so I want to copy the columns by a single name, like all Quality columns, to another sheet. It can be all of the quality, RFPC, etc. columns in a new sheet with a category or each column name in a new sheet.

And the workbook is live every time will add some new column it's can be quality , RFPC....etc

I appreciate your help
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this code:

VBA Code:
Sub CopyColumnsByName()
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim sourceRange As Range
    Dim targetRange As Range
    Dim columnName As String
    Dim uniqueNames As Collection
    
    ' Set the source worksheet
    Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
    
    ' Set the range to search in the source sheet
    Set sourceRange = sourceSheet.Range("A1:Z1")
    
    ' Create a collection to store the unique column names
    Set uniqueNames = New Collection
    
    ' Loop through the columns in the source range
    For Each c In sourceRange.Columns
        columnName = c.Value
        
        If Not IsEmpty(columnName) Then
            ' If the column name is not empty, check if it is unique
            On Error Resume Next
            uniqueNames.Add columnName, CStr(columnName)
            On Error GoTo 0
        End If
    Next c
    
    ' Loop through the unique column names
    For Each n In uniqueNames
        columnName = n
        
        ' Create a new sheet with the same name as the column
        Set targetSheet = ThisWorkbook.Sheets.Add
        targetSheet.Name = columnName
        On Error Resume Next
        ' Loop through the columns in the source range
        For Each c In sourceRange.Columns
            If c.Value = columnName Then
                ' If the column name matches the current unique name, copy the column to the new sheet
                c.EntireColumn.Copy Destination:=targetSheet.Cells(1, targetSheet.Columns.Count).End(xlToLeft).Offset(0, 1)
            End If
        Next c
    Next n
    
    ' Clean up
    Set sourceSheet = Nothing
    Set targetSheet = Nothing
    Set sourceRange = Nothing
    Set targetRange = Nothing
    Set uniqueNames = Nothing
End Sub
 
Upvote 0
Solution
Try this code:

VBA Code:
Sub CopyColumnsByName()
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim sourceRange As Range
    Dim targetRange As Range
    Dim columnName As String
    Dim uniqueNames As Collection
   
    ' Set the source worksheet
    Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
   
    ' Set the range to search in the source sheet
    Set sourceRange = sourceSheet.Range("A1:Z1")
   
    ' Create a collection to store the unique column names
    Set uniqueNames = New Collection
   
    ' Loop through the columns in the source range
    For Each c In sourceRange.Columns
        columnName = c.Value
       
        If Not IsEmpty(columnName) Then
            ' If the column name is not empty, check if it is unique
            On Error Resume Next
            uniqueNames.Add columnName, CStr(columnName)
            On Error GoTo 0
        End If
    Next c
   
    ' Loop through the unique column names
    For Each n In uniqueNames
        columnName = n
       
        ' Create a new sheet with the same name as the column
        Set targetSheet = ThisWorkbook.Sheets.Add
        targetSheet.Name = columnName
        On Error Resume Next
        ' Loop through the columns in the source range
        For Each c In sourceRange.Columns
            If c.Value = columnName Then
                ' If the column name matches the current unique name, copy the column to the new sheet
                c.EntireColumn.Copy Destination:=targetSheet.Cells(1, targetSheet.Columns.Count).End(xlToLeft).Offset(0, 1)
            End If
        Next c
    Next n
   
    ' Clean up
    Set sourceSheet = Nothing
    Set targetSheet = Nothing
    Set sourceRange = Nothing
    Set targetRange = Nothing
    Set uniqueNames = Nothing
End Sub
Thank you for your support, which I really appreciate. The codes are working fine, with one exception: if I add a new column to the current worksheet, I run the code, and it returns an error, but it should be run, and the new column should be moved to another sheet based on the column name. For example, there were previously 5 columns named "Quality." after run all 5 gose to 1 workshhet with name quality but when i added one new column with name quality then it's not going but it's shoud go and added with previous 5 column.
or can i fix the sheet name with column name and every time run and get all column from main sheet to Named shhet. because data in the main sheet is updated with column entries every time.
Thank you
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top