用於連接一系列資料的單元格的 VBA 程式碼

用於連接一系列資料的單元格的 VBA 程式碼

我的產品可以有多種變體和尺寸。我需要一個可以獲取值並將它們連接起來以建立產品 SKU 的巨集。例如我的產品是1234。大小為 30、36 等。所以我的最終產品將是 1234-30-ABF、1234-30-PLC、1234-30-MKLN 等和 1234-36-ABF、1234-36-PLC 等。我需要讀取列並使用 & 或 concat 函數循環運行巨集。我將提供價值觀。我嘗試了宏 VBA,但無法在 concat 函數中使用變數。請幫忙。

ActiveCell.FormulaR1C1 = "=Sheet2!RC&""-""&Sheet2!RC[3]&"".""&""FR"""

圖片:

答案1

假設基本型號位於 A 列,變體位於 B 列,尺寸位於 C 列,並且建立時的 SKU 位於不同的單元格上,則您可以使用以下 VBA 巨集:

Sub AllSKU()

    Dim model, myvariant, mysize, sku As String
    Dim lRow As Long
    Dim i As Integer

    lRow = Cells(Rows.Count, 1).End(xlUp).Row 'Get the last row with data
    model = Range("A2").Value
    
    Range("E:E").Value = "" 'Clear Column E -- As it will be used for SKUs
    Range("E" & 1).Value = "SKU"

    For x = 2 To lRow

        If Range("A" & x).Value <> "" Then 'Get model when cell is not empty
            model = Range("A" & x)
        End If

        Range("E" & x).Value = model & "-" & Range("B" & x).Value & "-" & Range("C" & x).Value
    Next

End Sub

它循環遍歷單元格並在 E 列上建立 SKU。

在此輸入影像描述

相關內容