I have an Excel sheet where some part numbers are duplicated. For each part number, I need to apply a filter, select the first occurrence, update all relevant rows for that part number, and then move to the next part number and repeat the process.
Do processing everything in DataTable. You can group by PartNumber, then update rows , then write back the DataTable to excel.
Read excel and store in Dt
Then identify duplicate part numbers by grouping or use linq like dt.AsEnumerable(). Select(Function(r) r(“PartNumber”).ToString.Trim). Distinct(). ToList()
4. Then loop for above output i.e each part number.
5. Then for each part number, get all rows in seaparate dt1, get first row details, using this update all other rows in current dt1.
6. Continue same steps for all part numbers.
7. Then write this to workbook output.
Thanks everyone — I found my solution.
Sub UpdateRows(val As String, updateValue As String, sheetName As String, filterCol As Long, updateCol As Long)
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets(sheetName)
' Get last row from the filter column
lastRow = ws.Cells(ws.Rows.Count, filterCol).End(xlUp).Row
For i = 2 To lastRow ' assuming row 1 is header
If Trim(ws.Cells(i, filterCol).Value) = Trim(val) Then
ws.Cells(i, updateCol).Value = updateValue
End If
Next i