Excel宏查找列表中的行并从另一个列表替换数据。

huangapple go评论64阅读模式
英文:

Excel Macro find rows in list and replace data from another list

问题

I'm wondering if there's a way in VBA to have a macro check the SKU and Store from "SheetB" and if found in "SheetA" Then replace the "STORE," "PRODUCT_TYPE," and "CONCAT" columns with the info from "SheetB" (in blue) and loop through the table if new ones are added.

Example: All rows with Sku: 95908352 and Store: -1 in SheetA will have its information changed to the blue columns on "SheetB".
SheetA STORE will be replaced by SheetB Store(Autofill), SheetA PRODUCT_TYPE will be replaced by SheetB Product Type (Autofill), and SheetA CONCAT will be replaced by SheetB Move to.

Then loop through the list if more exceptions are added to the SheetB table.

英文:

Im wondering if theres a way in VBA to have a macro check the SKU and Store from "SheetB" and if found in "SheetA" Then replace the "STORE", "PRODUCT_TYPE" and "CONCAT" columns with the info from "SheetB" (in blue) and loop through the table if new ones are added.

Example: All rows with Sku: 95908352 and Store: -1 in SheetA will have its information changed to the blue columns on "SheetB".
SheetA STORE will be replace by SheetB Store(Autofill), SheetA PRODUCT_TYPE will be replaced by SheetB Product Type (Autofill) and SheetA CONCAT will be replaced by SheetB Move to.

Then loop through the list if more exceptions are added to the SheetB table.

SheetA (Before):

Excel宏查找列表中的行并从另一个列表替换数据。

SheetB:

Excel宏查找列表中的行并从另一个列表替换数据。

Sheet A (After):

Excel宏查找列表中的行并从另一个列表替换数据。

Dim wsD As Worksheet, wsE As Worksheet, r As Long
Dim rngData As Range, rngEx As Range, mH, mR, col As Long, hdr, v
Dim rngCopy As Range, lr As Long, addr

Set wsD = ThisWorkbook.Worksheets("Data")
Set wsE = ThisWorkbook.Worksheets("SKU Exceptions")

Set rngData = wsD.Range("A3:Z" & wsD.Cells(Rows.Count, "A").End(xlUp).Row)


For col = 1 To 3  'loop the columns on the exceptions sheet
    lr = wsE.Cells(Rows.Count, col).End(xlUp).Row
    If lr > 1 Then
    
    
    
    
        'get the values and find on "Data"
        hdr = Replace(wsE.Cells(1, col).Value, "Product Type (Move to) ", "")
        hdr2 = Replace(wsE.Cells(1, col).Value, "Store (Autofill) ", "")
        mH = Application.Match(hdr, rngData.Columns(17), 0) And Application.Match(hdr2, rngData.Columns(1), 0)

        If Not IsError(mH) Then 'matched both values?
            For r = 2 To lr
                v = wsE.Cells(r, col).Value
                mR = Application.Match(v, rngData.Columns(17), 0)
                If Not IsError(mR) Then
                    'Copy new values over
                    

                    
                Else
                    'value was not matched
                    MsgBox "Value '" & v & "' not found on Data sheet!"
                End If
            Next r
        Else
            'header not matched
            MsgBox "Header '" & hdr & "' not found on data sheet!"
        End If
    End If
Next col

MsgBox "Sku's have been reordered successfully!", vbInformation, "Reorder Sku's Macro"

答案1

得分: 1

如果我理解你的意思正确....

Sub test()
Dim wsD As Worksheet, wsE As Worksheet
Dim rgE As Range, cell As Range, rgStore As Range, rgR As Range

Set wsD = ThisWorkbook.Worksheets("Data")
Set wsE = ThisWorkbook.Worksheets("SKU Exceptions")
Set rgE = wsE.Range("B2", wsE.Range("B" & Rows.Count).End(xlDown))

For Each cell In rgE
    If Not wsD.Columns(3).Find(cell.Value) Is Nothing Then
        With wsD.Columns(3)
        .Replace cell.Value, True, xlWhole, , False, , False, False
        Set rgStore = .SpecialCells(xlConstants, xlLogical).Offset(0, -2)
        .Replace True, cell.Value, xlWhole, , False, , False, False
        End With

        If Not rgStore.Find(cell.Offset(0, -1).Value) Is Nothing Then
        
            With rgStore
            .Replace cell.Offset(0, -1).Value, True, xlWhole, , False, , False, False
            Set rgR = .SpecialCells(xlConstants, xlLogical)
            .Replace True, cell.Offset(0, -1).Value, xlWhole, , False, , False, False
            End With

            rgR.Value = cell.Offset(0, 2).Value
            rgR.Offset(0, 1).Value = cell.Offset(0, 3).Value
            rgR.Offset(0, 13).Value = cell.Offset(0, 1).Value
    
        End If
    End If
Next

End Sub

这个子程序创建了rgE,它是wsE列SKU的范围,从B2到行末。然后它循环处理rgE中的每个单元格。

在循环中,它检查循环的单元格值是否在wsD的第3列中找到,如果找到,则设置rgStore变量。因此,rgStore是wsD第A列中的范围,其中rgStore的单元格偏移(0,2)的值是循环的单元格值。

接下来,它检查循环的cell.offset(0,-1)值是否在rgStore中找到,然后设置rgR变量。因此,rgR是wsD第A列中的范围,其中rgR的单元格值是循环的cell.offset(0,-1).value,而cell.offset(0,2)的值是循环的单元格值。

最后,它填充所需的结果。

未使用虚拟数据进行测试。

英文:

If I understand you correctly....

Sub test()
Dim wsD As Worksheet, wsE As Worksheet
Dim rgE As Range, cell As Range, rgStore As Range, rgR As Range

Set wsD = ThisWorkbook.Worksheets("Data")
Set wsE = ThisWorkbook.Worksheets("SKU Exceptions")
Set rgE = wsE.Range("B2", wsE.Range("B" & Rows.Count).End(xlDown))

For Each cell In rgE
    If Not wsD.Columns(3).Find(cell.Value) Is Nothing Then
        With wsD.Columns(3)
        .Replace cell.Value, True, xlWhole, , False, , False, False
        Set rgStore = .SpecialCells(xlConstants, xlLogical).Offset(0, -2)
        .Replace True, cell.Value, xlWhole, , False, , False, False
        End With

        If Not rgStore.Find(cell.Offset(0, -1).Value) Is Nothing Then
        
            With rgStore
            .Replace cell.Offset(0, -1).Value, True, xlWhole, , False, , False, False
            Set rgR = .SpecialCells(xlConstants, xlLogical)
            .Replace True, cell.Offset(0, -1).Value, xlWhole, , False, , False, False
            End With

            rgR.Value = cell.Offset(0, 2).Value
            rgR.Offset(0, 1).Value = cell.Offset(0, 3).Value
            rgR.Offset(0, 13).Value = cell.Offset(0, 1).Value
    
        End If
    End If
Next

End Sub

The sub create rgE which is the range of wsE column SKU from B2 to end of row. Then it looped to each cell within rgE.

During the loop, it check if the looped cell value is found in wsD column 3, then it set rgStore variable. So, rgStore is the range in column A of wsD, where rgStore's cell offset(0,2) value is the looped cell value.

Next, it check if the looped cell.offset(0,-1) value is found in rgStore, then it set rgR variable. So, rgR is the range in column A of wsD, where rgR's cell value is the looped cell.offset(0,-1).value and the cell.offset(0,2) value is the looped cell value.

Finally it fill the needed result.

Not tested with dummy data.

答案2

得分: 1

匹配多于1列时,通常更容易使用Scripting Dictionary来创建复合键和相应行的映射:

Sub Test()

    Dim wsD As Worksheet, wsE As Worksheet
    Dim rngData As Range, rngEx As Range, rwEx As Range
    Dim dict As Object, rwD As Range, rwE As Range, k
    
    Set dict = CreateObject("scripting.dictionary")
    
    Set wsD = ThisWorkbook.Worksheets("Data")
    Set rngData = wsD.Range("A3:Z" & wsD.Cells(Rows.Count, "A").End(xlUp).Row)
    
    Set wsE = ThisWorkbook.Worksheets("SKU Exceptions")
    Set rngEx = wsE.Range("A2:E" & wsE.Cells(Rows.Count, "A").End(xlUp).Row)
    
    '将异常行映射到复合键“store~~SKU”,假设组合是唯一的...
    For Each rwE In rngEx.Rows
        k = rwE.Cells(1).Value & "~~" & rwE.Cells(2).Value
        dict.Add k, rwE
    Next rwE
    
    '循环数据行并检查匹配的异常
    For Each rwD In rngData.Rows
        k = rwD.Cells(1).Value & "~~" & rwD.Cells(3).Value '复合键
        If dict.exists(k) Then
            Set rwEx = dict(k)
            rwD.Cells(1).Value = rwEx.Cells(4).Value
            rwD.Cells(2).Value = rwEx.Cells(5).Value
        End If
    Next rwD
    
    MsgBox "Sku's have been reordered successfully!", vbInformation, "Reorder Sku's Macro"

End Sub
英文:

When matching on >1 column it's often easier to use a Scripting Dictionary to create a map of composite keys and corresponding rows:

Sub Test()

    Dim wsD As Worksheet, wsE As Worksheet
    Dim rngData As Range, rngEx As Range, rwEx As Range
    Dim dict As Object, rwD As Range, rwE As Range, k
    
    Set dict = CreateObject("scripting.dictionary")
    
    Set wsD = ThisWorkbook.Worksheets("Data")
    Set rngData = wsD.Range("A3:Z" & wsD.Cells(Rows.Count, "A").End(xlUp).Row)
    
    Set wsE = ThisWorkbook.Worksheets("SKU Exceptions")
    Set rngEx = wsE.Range("A2:E" & wsE.Cells(Rows.Count, "A").End(xlUp).Row)
    
    'Map exception rows to composite key of `store~~SKU`,
    '   assuming combination is unique...
    For Each rwE In rngEx.Rows
        k = rwE.Cells(1).Value & "~~" & rwE.Cells(2).Value
        dict.Add k, rwE
    Next rwE
    
    'loop the data rows and check for matching exceptions
    For Each rwD In rngData.Rows
        k = rwD.Cells(1).Value & "~~" & rwD.Cells(3).Value 'composite key
        If dict.exists(k) Then
            Set rwEx = dict(k)
            rwD.Cells(1).Value = rwEx.Cells(4).Value
            rwD.Cells(2).Value = rwEx.Cells(5).Value
        End If
    Next rwD
    
    MsgBox "Sku's have been reordered successfully!", vbInformation, "Reorder Sku's Macro"

End Sub

huangapple
  • 本文由 发表于 2023年6月15日 02:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定