這樣做的另一種方式.....不需要。循環。
假設
1.跳過列按升序排列。
2.跳過列值從1開始而不是0.
3.範圍(「Source」)是源數據中的第一個單元格。
4.範圍(「目標」)是目標數據中的第一個單元格。
Sub SelectiveCopy(rngSource As Range, rngTarget As Range, intTotalColumns As Integer, skipColumnsArray As Variant)
If UBound(skipColumnsArray) = -1 Then
rngSource.Resize(1, intTotalColumns).Copy
rngTarget.PasteSpecial (xlPasteValues)
Else
Dim skipColumn As Variant
Dim currentColumn As Integer
currentColumn = 0
For Each skipColumn In skipColumnsArray
If skipColumn - currentColumn > 1 Then 'Number of colums to copy is Nonzero.'
rngSource.Offset(0, currentColumn).Resize(1, skipColumn - currentColumn - 1).Copy
rngTarget.Offset(0, currentColumn).PasteSpecial (xlPasteValues)
End If
currentColumn = skipColumn
Next
If intTotalColumns - currentColumn > 0 Then
rngSource.Offset(0, currentColumn).Resize(1, intTotalColumns - currentColumn).Copy
rngTarget.Offset(0, currentColumn).PasteSpecial (xlPasteValues)
End If
End If
Application.CutCopyMode = False
End Sub
如何撥打:
SelectiveCopy Range("Source"), Range("Target"), 20, Array(1) 'Skip 1st column'
SelectiveCopy Range("Source"), Range("Target"), 20, Array(4,5,6) 'Skip 4,5,6th column'
SelectiveCopy Range("Source"), Range("Target"), 20, Array() 'Dont skip any column. Copy all.
感謝。
感謝您的回覆。我已經編輯了這個問題..添加示例來清除我的需求。請檢查。 – Sambhaji 2010-03-04 10:20:18
非常感謝..代碼工作... – Sambhaji 2010-03-04 11:54:23