简体   繁体   中英

How to paste from column B all rows to column A's nth row in Excel VBA

I have two data columns as Col A and Col B in excel like A1, A2, A3, A4,. . . and B1, B2, B3, B4,..... Now, I want to copy Col B data (B1, B2, B3, B4, ....) to Col A's every nth row OR into Odd rows Or into even rows. I have clearified my question more like shown below,

Data before in

ColA 
A1     
A2     
A3    
A4    
A5    

.

And ColB has data like

B1 
B2
B3
B4
B5

...

I want the ColA data like following,

A1
B1
A3
B2
A4
B3
A6
B4    and so on

This will produce an interleaved arrangement:

Sub interleave()
    Dim nB As Long
    Dim j As Long, i As Long

    nB = Cells(Rows.Count, "B").End(xlUp).Row
    j = 2

    For i = 1 To nB
        Cells(j, 1).Value = Cells(i, 2).Value
        j = j + 2
    Next i
End Sub
Sub Interleaved()
  Dim i As Integer, j As Integer

  i = 2
  j = 1

  Do
    Range("A" & i).Value = Range("B" & j).Value
    i = i + 2
    j = j + 1
  Loop Until Range("A" & j) = ""
End Sub

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