首页 > 解决方案 > 如何在 Microsoft excel 中相对于变量重复数据?

问题描述

嗨,我有一个名称列表和一个变量,我需要一个 Excel 公式或插件或自动化方法来根据变量中的数字重复名称。我需要一个公式来自动化输出列,例如:

我需要一个公式来自动化输出

标签: excelexcel-formulaexcel-2010formulaexport-to-excel

解决方案


可能尽可能干净。有一个For循环和Do Until循环给你选择。

Sub repeat()
    ' define varible types
    Dim i, n As Integer
    Dim wb As Workbook: Set wb = Workbooks(ThisWorkbook.Name)
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    Dim LastRow As Long
    
    ' initialize variables
    n = 1
    i = 1
    
    ' do loop until empty cell is found in column 1 (A)
    Do Until IsEmpty(ws.Cells(i, 1))
        ' second loop to define number of iterations
        For n = 1 To ws.Cells(i, 2)
            ' find last row to keep adding to the bottom of the list
            LastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
            ' populate list
            ws.Cells(LastRow + 1, 3) = ws.Cells(i, 1)
        Next
        i = i + 1
    Loop
    
End Sub

推荐阅读