【问题标题】:Find and Replace with multiple values for each occurrence (VBA)查找并替换每次出现的多个值(VBA)
【发布时间】:2019-04-18 19:34:08
【问题描述】:

我的 SKU 必须根据件数进行多次转换。 例如 原始数据:

  • brp-100_cn_3pc_16x20

想要的结果:

  • brp-100a_cn_16x20

  • brp-100b_cn_16x20

  • brp-100c_cn_16x20

每个都在同一列的单独单元格中(注意 3pc = a,b,c 对于其他 SKU 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

【问题讨论】:

  • 您要避免使用Activate,活动单元格并不总是您认为的那个。看看this

标签: excel vba replace find


【解决方案1】:

你可以这样使用:

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

【讨论】:

  • 效果很好!但我还需要从列中清除/删除原始 SKU。想不通。我以前从未使用过函数。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 2018-03-24
相关资源
最近更新 更多