简体   繁体   中英

VBA Excel: copy separated cells

Quick question:

In Excel I have a sheet with the following structure:

Sheet1     
A      B     C     D     E    F    G    H    I    J    K   ...
2001   x     x     x     x   2002  x    x    x    x   2003 ...

I want to create a macro in VBA that copies and pastes the values with years in another sheet:

Sheet2
A      B     C     D    ...
2001   2002  2003  2004 ...

How can I do this in a simple way?

My solution sketch so far

Set sourceRng = sourceSheet.range(Mark every fifth cell)
Set targetRng = destinationSheet.range(something)

sourceRng.Copy
targetRng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

You can loop and copy single cells...

Set StartCellSheet1 = Sheet1.Range("A2")
Set StartCellSheet2 = Sheet2.Range("A2")

Sheet2Index = 0
for Sheet1Index = 0 to 2000 step 5 'use the limit of your sheet
    StartCellSheet2.Offset(0, Sheet2Index).Value = StartCellSheet1.Offset(0,Sheet1Index).Value
    Sheet2Index = Sheet2Index + 1 
next

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