根据另一个单元格中的数字在单元格中输入值

Enter value in cell based on number from another cell

如果有人做过类似下面的事情,请帮忙。 我正在寻找的是查看我的 A2 值的宏,并根据值 B 将其复制到 D 列中,并在其后添加“_”(下划线)。

您的要求在细节上有点不足,但这会满足您的要求。

dim i as long
with worksheets("sheet1")
    for i=1 to .cells(2, "B").value2
        .cells(.rows.count, "D").end(xlup).offset(1, 0) = .cells(2, "A").value & format(i, "\_0")
    next i
end with

为此你需要 2 个循环。一个循环遍历 A 列,一个循环计算 B 列中的值。

Option Explicit

Public Sub WriteValues()
    With Worksheets("Sheet1")
        Dim aLastRow As Long
        aLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'get last used row in col A

        Dim dRow As Long
        dRow = 1 'start row in col D

        Dim aRow As Long
        For aRow = 1 To aLastRow 'loop through col A
            Dim bCount As Long
            For bCount = 1 To .Cells(aRow, "B").Value 'how many times is A repeated?
                .Cells(dRow, "D").Value = .Cells(aRow, "A") & "_" & bCount 'write into column D
                dRow = dRow + 1 'count rows up in col D
            Next bCount
        Next aRow
    End With
End Sub