How to get multiple copies of a cell in a single row by n-number?

Deadly 提交于 2019-12-24 08:20:09

问题


I would like to use vba code that can sort this problem out.

I have a row and i want multiple copies of each cell in the same row. It needs to copy the cell by n-numbers. In row 1 will be the information to be copied. and in row 2 the n-number.

So the example:

Input: (say the n-number = 3)

  • John
  • Hendrik
  • Sara

Output:

  • John
  • John
  • John
  • Hendrik
  • Hendrik
  • Hendrik
  • Hendrik
  • Sara
  • Sara
  • Sara ....

Hope someone could help me out!


回答1:


From:

To:

Use this code:

Option Explicit

Sub CopyInAWeirdWay()
    Dim sh As Excel.Worksheet
    Dim LastRow As Long
    Dim currentCopyRow As Long
    Dim i As Long
    Dim k As Long

    Set sh = ActiveSheet
    LastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
    currentCopyRow = 1

    For i = 1 To LastRow
        For k = 1 To sh.Cells(i, 2)
            sh.Cells(currentCopyRow, 3).Value = sh.Cells(i, 1).Value
            currentCopyRow = currentCopyRow + 1
        Next k
    Next i
End Sub


来源:https://stackoverflow.com/questions/15125286/how-to-get-multiple-copies-of-a-cell-in-a-single-row-by-n-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!