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

时间:2018-03-15 14:04:58

标签: excel excel-vba excel-formula ms-office vba

如果有人做了以下任何事情,请提供帮助。 我正在寻找的是查看我的A2值的宏,并在D列中根据值B复制它,后面带有“_”(下划线)。

enter image description here

2 个答案:

答案 0 :(得分:2)

你需要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

答案 1 :(得分:0)

您的要求在细节方面略有不足,但这样做可以满足您的要求。

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