尝试在excel中创建特定循环以获得输出

时间:2013-08-15 00:23:48

标签: excel vba loops

在这个问题上忍受我。我很确定那些在这个领域有知识的人会很容易,但我对VBA或如何在Excel中创建循环以创建这个公式知之甚少:

请查看此处的图片

enter image description here

我正在尝试构建的是一个循环,它将连接这些数字。 EX。我想以这个顺序连接A2,“ - ”,B2; A3, “ - ”,B2; A4, “ - ”,B2 ..... A16, “ - ”,B2 一旦A1-A16中的所有内容与B2连接,我想继续将A1-A16与B3.EX连接:A2,“ - ”,B3; A3,“ - ”,B3 ...... A16,“ - ”,B3

我知道这是可能的,因为可以创建某些循环来完成此过程,但我不知道VBA并且不确定这是否可以仅使用Excel中预先存在的公式。感谢任何帮助过的人。

1 个答案:

答案 0 :(得分:0)

根据您的描述,它是非常简单的嵌套循环。下面的代码将连接您想要的方式并将其存储到C列。

Sub MyConcat()
    Const lColA As Long = 1
    Const lColB As Long = 2
    Const lColTxt As Long = 3 ' concatenated result in Column C
    Dim oWS As Worksheet, sTxt As String
    Dim lRowA As Long, lRowB As Long, lRowTxt As Long

    Set oWS = ThisWorkbook.Worksheets("Sheet1") ' Change this to match yours
    lRowA = 1
    lRowTxt = 1
    oWS.Columns(lColTxt).Clear ' remove previous data on Column C
    Do Until IsEmpty(oWS.Cells(lRowA, lColA))
        sTxt = ""
        lRowB = 2
        Do Until IsEmpty(oWS.Cells(lRowB, lColB))
            sTxt = oWS.Cells(lRowA, lColA).Text & "-" & oWS.Cells(lRowB, lColB).Text
            oWS.Cells(lRowTxt, lColTxt) = sTxt
            lRowB = lRowB + 1
            lRowTxt = lRowTxt + 1
        Loop
        lRowA = lRowA + 1
    Loop
    Set oWS = Nothing
End Sub

编辑:这应该适用于父SKU数量的许多情况。 可用于第二张图像中的数据,包括另一组不同长度的“TuTi”和父级SKU。请尝试理解它,这将是一整页解释。

Private Const lColA As Long = 1
Private Const lColB As Long = 2
Private Const lColTxt As Long = 3 ' concatenated result in Column C

Dim oWS As Worksheet, sGroup As String, lRowCurr As Long, lRowTxt As Long

Sub MyConcat()
    Dim oRng As Range, lStopRow As Long

    Set oWS = ThisWorkbook.Worksheets("Sheet1") ' Change this to match yours
    lRowCurr = 1 ' Current Row index
    lRowTxt = 1 ' Results from Row 1
    sGroup = ""
    With oWS
        .Columns(lColTxt).Clear ' remove previous data on Column C
        ' Row of LastCell in current sheet + 1
        lStopRow = .Cells.SpecialCells(xlLastCell).Row + 1
        ' Row of "Ctrl-Up" from LastCell Row at column A
        lStopRow = .Cells(lStopRow, lColA).End(xlUp).Row + 1
        ' Start processing rows until until StopRow in column A
        Do Until lRowCurr = lStopRow
            Set oRng = .Cells(lRowCurr, lColA)
            If IsGroupCell(oRng) Then
                sGroup = oRng.Value ' Stores Group text
            ElseIf IsParentSKU(oRng) Then
                Call MyConcat2 ' Invoke the mix sub that writes the result in column C
            End If
            lRowCurr = lRowCurr + 1
            Set oRng = Nothing
        Loop
    End With
    Set oWS = Nothing
End Sub

Private Sub MyConcat2()
    Dim sTxt As String, oRng As Range
    Dim lRowA As Long, lRowB As Long

    lRowA = lRowCurr + 1
    Set oRng = oWS.Cells(lRowA, lColA)
    ' Stop mixing the values when it is a Group or Parent SKU row
    Do Until IsGroupCell(oRng) Or IsParentSKU(oRng) Or IsEmpty(oRng)
        sTxt = ""
        lRowB = lRowCurr + 1
        ' Don't mix if it is a Parent SKU
        Do Until IsParentSKU(oWS.Cells(lRowB, lColA)) Or IsEmpty(oWS.Cells(lRowB, lColB))
            sTxt = oWS.Cells(lRowA, lColA).Text & "-" & oWS.Cells(lRowB, lColB).Text
            oWS.Cells(lRowTxt, lColTxt) = sGroup & "-" & sTxt
            lRowB = lRowB + 1
            lRowTxt = lRowTxt + 1
        Loop
        lRowA = lRowA + 1
        Set oRng = oWS.Cells(lRowA, lColA)
    Loop
    lRowCurr = lRowA - 1
    Set oRng = Nothing
End Sub

Private Function IsGroupCell(oRng As Range) As Boolean
    IsGroupCell = (Not IsNumeric(Left(oRng.Value, 1)) And IsEmpty(oRng.Offset(0, 1)))
End Function

Private Function IsParentSKU(oRng As Range) As Boolean
    IsParentSKU = (IsNumeric(oRng.Value) And IsNumeric(oRng.Offset(0, 1).Value))
End Function