0

在这个问题上请耐心等待。我很确定对于那些在该领域有知识的人来说很容易,但我对 VBA 或如何在 Excel 中创建循环来创建这个公式知之甚少:

请在此处查看图片

在此处输入图像描述

我正在尝试构建一个将连接这些数字的循环。前任。我想按此顺序连接 A2,"-",B2; A3,"-",B2; A4,"-",B2.....A16,"-",B2 将 A1-A16 中的所有内容与 B2 连接后,我想继续将 A1-A16 与 B3.EX 连接:A2,"-" ,B3; A3,"-",B3.....A16,"-",B3

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

4

1 回答 1

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
于 2013-08-15T00:53:45.743 回答