OzGrid

How to use VBA to copy column based on heading

< Back to Search results

 Category: [Excel]  Demo Available 

How to use VBA to copy column based on heading

 

Requirement:

 

The user has this code to copy column A to another sheet but would like to change it based on the position of a certain heading.

 

For example, the user would like it to find and copy the column "Country", which may not necessarily be column A.

Set Ash = ActiveSheet
lstrow = Ash.Cells(Rows.Count, 1).End(xlUp).Row
Set Cws = Worksheets.Add
Range("A1", "A" & lstrow) = Ash.Range("A1", "A" & lstrow).Value

 

https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/1198344-copying-column-based-on-heading

 

Solution:

 

Code:
Sub bilbo85()
Dim Ash As Worksheet, lstrow As Long, fnd As Long
    Set Ash = ActiveSheet
        lstrow = Ash.Cells(Rows.Count, 1).End(xlUp).Row
            fnd = Ash.Cells(1, 1).EntireRow.Find("Country", , , 1, xlByColumns, xlPrevious).Column
    Ash.Range(Cells(1, fnd), Ash.Cells(lstrow, fnd)).Copy Worksheets.Add.Range("A1")
End Sub

 

Obtained from the OzGrid Help Forum.

Solution provided by jolivanes.

 

See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets

 

See also:

How to copy cell data to another sheet and save it automatically
How to use a Macro to copy rows from multiple worksheets based on a cell value greater than zero
How to copy last used row to next empty row in another worksheet
How to create VBA to copy specific column from one sheet to another

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)