对于每一个,交换内容的价值/移动部分

时间:2017-11-29 22:19:56

标签: excel excel-vba foreach vba

所以我想逐个单元地移动两个并行但没有连接的范围的内容,所以我写了一个更早和更详细的以下代码版本,不知怎的,我让它工作,但现在它没有。

Sub movedown()
Dim r As Range, r1 As Range, r2 As Range, r3 As Range, r4 As Range
Dim rc As Integer, str$

Set r = Intersect(Selection.EntireRow, Range("A:D"))
rc = r.Rows.Count
Set r1 = r.Columns(1)'for column A
Set r2 = r.Columns(4)'for column D
Set r3 = r1.Cells(1, 1).Offset(rc)'offsets to the cell beneath the selection
Set r4 = r2.Cells(1, 1).Offset(rc)'offsets to the cell beneath the selection

For Each c In r1
    str = r3.Value
    r3.Value = c.Value
    c.Value = str
Next c

For Each c In r2
    str = r4.Value
    r4.Value = c.Value
    c.Value = str
Next c

r.Offset(1).Select
End Sub

示例:(选择范围= A1:A4) 想法是在单元格1和5之间交换单元格值,然后是2和5,然后是3和5,然后是4和5,然后移动到下一个范围。

step  0 1 2 3 4
A1    a x x x x    
A2    b b a a a     
A3    c c c b b     
A4    d d d d c     
A5    x a b c d 

0 个答案:

没有答案