I am trying to loop through a range titled mineral and find a matching cell within a separate list titled compList only if a certain range of cells contains a numeric value. If no match is found, then the cell (a string) is copied and pasted into the next available row within compList along with the adjacent cells (numbers). If a match is found, then only the adjacent cells would be added to the existing cells.
This is what I managed to do thus far, it would paste the cell value and adjacent cells as expected, but it would continue to paste these cells even if it already exists in compList. I was not able to create a code to add those values to an existing match since I was trying to figure out this issue.
If you can, please add a brief comment line so I can learn!
Thanks in advance.
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