Copy column based on title in VBA

AJ12345

New Member
Joined
Feb 8, 2014
Messages
22
Hi.
I need to copy values in a column. Which column varies, but the title of the column (in row 1) is always the same. I had:

Dim Constant
Constant = Range("C:C")

The problem is that the values I want may or may not be in column C.

The column I want to copy has the text "Resource" in row 1. This text will never change.

Please help.

Regards

AJ
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi. The following sub will do as you ask. It assumes the column you're copying is in the activesheet and you'll have to change the destination. Good Luck.

Code:
Sub CopyResourceCol()
ColumnNumber = Application.Match("Resource", ActiveSheet.Rows(1), 0)
ActiveSheet.Columns(ColumnNumber).Copy Destination:=Sheets("Sheet1").Columns(10)
End Sub
 
Upvote 0
Thank you.
If I would like to give the values in the column a name (previous I used dim constant constant = Range("C:C") - And then when I needed to paste the values from column C I would use Range"(Whatever") = Constant... how would I do that?

The truth is that I have several columns I want to copy (they got different text in row 1) and I want to past them several times in different places in the workbook. So the active columns must be copied and named "Constant1", "Constan2" etc.

Hi. The following sub will do as you ask. It assumes the column you're copying is in the activesheet and you'll have to change the destination. Good Luck.

Code:
Sub CopyResourceCol()
ColumnNumber = Application.Match("Resource", ActiveSheet.Rows(1), 0)
ActiveSheet.Columns(ColumnNumber).Copy Destination:=Sheets("Sheet1").Columns(10)
End Sub
 
Upvote 0
Sure.

If this is my spreadsheet: The data are downloaded from another source, so the columns might swich place. For instance "Art" (which is now in column B) might as well be in column A,C or D.
I need to give the different columns names so I later in my macro can paste them correctly. Previous I would just write:
dim Resource
dim Art
dim company
Resource = Range("A:A")
Art = Range("B:B")
Company = Range("D:D")

And when I wanted to paste the different values, I could just use Range(X:X) = Company



ResourceArtValueCompany
451291
4613102
4714113
4815124

<tbody>
</tbody><colgroup><col><col span="3"></colgroup>



I'm not too clear. Can you give a couple of examples.
 
Upvote 0
Ok. You could use the following to allocate the correct column numbers, which you'd then use when you copy/paste (changing the YourSheet and YourColumnNumber to wherever you want them to go)

Code:
Sub CopyColumns()
ResourceColumn = Application.Match("Resource", ActiveSheet.Rows(1), 0)
ArtColumn = Application.Match("Art", ActiveSheet.Rows(1), 0)
CompanyColumn = Application.Match("Company", ActiveSheet.Rows(1), 0)
ValueColumn = Application.Match("Value", ActiveSheet.Rows(1), 0)


' Now if you want to copy, say, the Art column you'd put

ActiveSheet.Columns(ArtNumber).Copy Destination:=Sheets("YourSheet").Columns(YourColumnNumber)
End Sub
 
Upvote 0
Hi. The point is that i need to copy those colums in to several different places. When I gave the colums a name (for instance: dim art) I could past the art values several times.

I need the different columns to be defined in that way. Do you know how?

Ok. You could use the following to allocate the correct column numbers, which you'd then use when you copy/paste (changing the YourSheet and YourColumnNumber to wherever you want them to go)

Code:
Sub CopyColumns()
ResourceColumn = Application.Match("Resource", ActiveSheet.Rows(1), 0)
ArtColumn = Application.Match("Art", ActiveSheet.Rows(1), 0)
CompanyColumn = Application.Match("Company", ActiveSheet.Rows(1), 0)
ValueColumn = Application.Match("Value", ActiveSheet.Rows(1), 0)


' Now if you want to copy, say, the Art column you'd put

ActiveSheet.Columns(ArtNumber).Copy Destination:=Sheets("YourSheet").Columns(YourColumnNumber)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

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