我有SKU,必须根据件数进行多次转换。 例如 原始数据:
期望的结果:
brp-100a_cn_16x20
brp-100b_cn_16x20
brp-100c_cn_16x20
在同一列中的每个单独的单元格中(注意,其他SKU的3pc = a,b,c 4pc = a,b,c,d…等) 正在从数据透视表复制数据并将其粘贴到另一张工作表。我记录了一个宏,并添加了一个For Each语句。它仅适用于第一个实例,而不适用于所有粘贴的SKU。
预先感谢
Sub ReplaceEach()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
Dim myrange As range
Set myrange = Sheets("PT_Data").range("K" & Rows.count).End(xlUp)
Dim i As Variant
Columns("K:K").Select
For Each i In myrange
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="a_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="b_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="c_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="d_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="e_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="f_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="g_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="h_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Find(What:="_cn_9pc_12x12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Replace What:="_cn_9pc_12x12", Replacement:="i_cn_12x12", _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat _
:=False, ReplaceFormat:=False
Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
End Sub
答案 0 :(得分:2)
您可以使用以下内容:
Sub ExpandAll()
Dim c As Range, arr
'loop over the input values
For Each c In ActiveSheet.Range("B3:B8").Cells
arr = ExpandSKU(c.Value) '<< expand this SKU
'adjust destination to suit...
ActiveSheet.Cells(Rows.Count, 4).End(xlUp). _
Offset(1, 0).Resize(UBound(arr, 1), 1).Value = arr
c.Value = "" 'clear the original
Next c
End Sub
Function ExpandSKU(sku)
Dim arrSku, arrOut(), num, i As Long
arrSku = Split(sku, "_")
num = Replace(arrSku(2), "pc", "")
ReDim arrOut(1 To num, 1 To 1)
For i = 1 To num
arrOut(i, 1) = Join(Array(arrSku(0) & Chr(96 + i), _
arrSku(1), arrSku(3)), "_")
Next i
ExpandSKU = arrOut
End Function