简体   繁体   中英

How to use a variable to copy an entire column contents in VBA?

I am assigning "c" as a variable and using it in a For statement. If Z1 = B1 or C1, I want it to copy the entire column.

I have tried:

Dim c As Range
Dim name As String

name = Range("Z1").Value

For Each c In Range("B1:C1").Cells
If c.Text = name Then
Columns.Range(c).Copy

But this doesn't seem to be working.

How do I work with variables that I have previously assigned in Columns/Range?

Very, very close:

replace:

Columns.Range(c).Copy

with:

c.EntireColumn.Copy

EDIT#1:

Once we have found the proper column, we can copy any portion of it. Say we want to copy rows 17 through 23:

Dim kolumn As Long
kolumn = c.Column
Range(Cells(17, kolumn), Cells(23, kolumn)).Copy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM