【问题标题】:Find a matching cell value in a range and paste cell value if no match is found在范围内查找匹配的单元格值,如果未找到匹配项,则粘贴单元格值
【发布时间】:2022-01-20 12:23:06
【问题描述】:

我正在尝试遍历标题为 mineral 的范围,并在标题为 compList 的单独列表中找到匹配的单元格,前提是某个单元格范围包含数值。如果未找到匹配项,则将单元格(字符串)与相邻单元格(数字)一起复制并粘贴到 compList 中的下一个可用行。如果找到匹配项,则只会将相邻的单元格添加到现有单元格中。

这是我迄今为止所做的,它会按预期粘贴单元格值和相邻单元格,但即使它已经存在于 compList 中,它也会继续粘贴这些单元格。因为我试图找出这个问题,所以我无法创建代码来将这些值添加到现有匹配中。

如果可以的话,请添加一个简短的评论行,以便我学习!

提前致谢。

        
        Dim wsMC As Worksheet
        Dim emptyRow As Long
        Dim mineral, cell, compList As Range, i
        
        
        Set wsMC = Sheets("Mining Calculator")
        Set mineral = Range("B10:B29")
        Set compList = Range("I11:I30")
        emptyRow = wsMC.Cells(Rows.Count, "I").End(xlUp).Row + 1

   
        If Application.CountA(wsMC.Range("D10:D29")) = 0 Then                     ' Checks if "D" column contains any value
            MsgBox ("Nothing to Add")                                             ' If 'D' column is empty (equals 0) then nothing happens, otherwise go to else
            
            Else
            For Each cell In mineral                                              'For each cell located in 'mineral' range
                If cell.Offset(0, 2).Value = 0 Then GoTo skip                     'If cells 2 columns from 'cell' is empty (equals 0) then skip, otherwise
                
                If Not StrComp("cell", "complist", vbTextCompare) = 0 Then        'Check if 'cell' value already exists within range 'compList' if not then
                        Cells(emptyRow, 9).Value = cell.Value                        'Copy 'cell' value to new row in 'compList'
                        Cells(emptyRow, 10).Value = cell.Offset(0, 3).Value          'Copy adjacent cell values from 'cell' to corresponding match in 'compList'
                        Cells(emptyRow, 11).Value = cell.Offset(0, 2).Value          'Copy adjacent cell values from 'cell' to corresponding match in 'compList'
                        Cells(emptyRow, 12).Value = cell.Offset(0, 4).Value          'Copy adjacent cell values from 'cell' to corresponding match in 'compList'
                        emptyRow = emptyRow + 1                                   'Add 1 to emptyRow to avoid replacing last cell value in 'compList'
                        
                    
                        Else                                                      'If 'cell' exists in 'compList' only add adjacent cells to the matching row
                        MsgBox ("it already exists")
                        Exit For
                End If
                
skip:
            Next cell
        End If
End Sub

【问题讨论】:

  • 那么只会添加相邻的单元格是什么意思?您是否有数字以便添加它们,或者您是否有要连接的字符串(在 I:L 列中)?
  • @VBasic2008 相邻单元格都是numbers。唯一的字符串是cell 变量。我也会在原帖中澄清这一点。
  • 那么,如果有匹配项,从compList 中的匹配项中复制相邻单元格并粘贴到mineral 列表中的比较值旁边?一种同步?
  • @bugdrown 反过来。如果mineral 列表中的单元格与compList 中的单元格匹配,那么我希望将相邻单元格添加到compList 中的现有单元格中以匹配单元格。例如:如果 A1:A10 中的单元格在 Z 列中不存在,则将该单元格(即 A1)复制到 W 列中的下一个可用行以及相邻单元格(B1、C1、D1)到 X 列中, Y,Z。如果 A1 已经存在于 W 列中,则只需将那些相邻的单元格 (B1,C1,D1) 添加到 A1 已经存在的值中。

标签: excel vba loops sum match


【解决方案1】:

如果存在则总结 Else 新条目

Option Explicit

Sub UpdateMinerals()
    
    ' s - Source (read from) ('Mineral')
    ' d - Destination (written to) ('CompList')
    
    Const scOffset As Long = 2 ' from column 'B' to column 'D'
    
    Dim scOffsets As Variant: scOffsets = VBA.Array(1, 2, 3)
    Dim dcOffsets As Variant: dcOffsets = VBA.Array(2, 1, 3)
    Dim oUpper As Long: oUpper = UBound(scOffsets)
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Mining Calculator")
    
    Dim slRow As Long: slRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    Dim srg As Range: Set srg = ws.Range("B10:B" & slRow)
    Dim dlRow As Long: dlRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
    Dim drg As Range: Set drg = ws.Range("I11:I" & dlRow)
    Dim dnCell As Range ' Destination Next Cell
    Set dnCell = ws.Cells(ws.Rows.Count, "I").End(xlUp).Offset(1)
    
    Dim sCell As Range ' Source Cell
    Dim sValue As Variant ' Source Value
    Dim diCell As Range ' Destination Indexed Cell ('n'-th cell of 'drg')
    Dim dIndex As Variant ' Destination Index ('n')
    Dim o As Long ' Offset Counter
    
    If Application.CountA(srg.Offset(, scOffset)) = 0 Then
        MsgBox "Nothing to Add"
    Else
        For Each sCell In srg.Cells
            If sCell.Offset(, scOffset).Value <> 0 Then
                ' Get the row of the match: if no match, then error.
                dIndex = Application.Match(sCell.Value, drg, 0)
                If IsError(dIndex) Then ' source not found in destination
                    dnCell.Value = sCell.Value
                    For o = 0 To oUpper
                        sValue = sCell.Offset(, scOffsets(o))
                        ' Write new values.
                        If IsNumeric(sValue) Then
                            dnCell.Offset(, dcOffsets(o)).Value = sValue
                        End If
                    Next o
                    Set dnCell = dnCell.Offset(1) ' next row
                    Set drg = drg.Resize(drg.Rows.Count + 1) ' include new
                Else ' source found in destination
                    Set diCell = drg.Cells(dIndex)
                    For o = 0 To oUpper
                        sValue = sCell.Offset(, scOffsets(o))
                        ' Add new to old values (sum-up).
                        If IsNumeric(sValue) Then
                            diCell.Offset(, dcOffsets(o)).Value _
                                = diCell.Offset(, dcOffsets(o)).Value _
                                + sValue
                        End If
                    Next o
                End If
            End If
        Next sCell
    End If
            
End Sub

【讨论】:

  • 它正在做我想做的事,谢谢。但是,它有一个小问题。源范围来自该范围最后一行的B10:B29B30,我有“总计”以及目标范围的第一行 (I10)。当我运行此代码时,它将按预期添加单元格,但也会在标有“总计”的目标中添加另一个单元格。如何防止它添加“总计”?谢谢。此外,偏移量的第一个单元格是number,第二个是percentage,第三个是dollar value。它目前都作为美元值输入
  • 删除Dim slRow As Long: slRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row并设置范围为:Dim srg As Range: Set srg = ws.Range("B10:B29")
猜你喜欢
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多