Sub UpdateColumnsBasedOnBR()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim valuesBR As Variant
Dim valuesL As Variant
Dim valuesM As Variant
Dim valuesN As Variant
' Set the worksheet
Set ws = ThisWorkbook.Sheets("BOM") ' Change "BOM" to your sheet name
' Disable screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Find the last row with data in column BR
lastRow = ws.Cells(ws.Rows.Count, "BR").End(xlUp).Row
' Read data into arrays
valuesBR = ws.Range("BR2:BR" & lastRow).Value
valuesL = ws.Range("L2:L" & lastRow).Value
valuesM = ws.Range("M2:M" & lastRow).Value
valuesN = ws.Range("N2:N" & lastRow).Value
' Loop through each row in column BR
For i = 1 To UBound(valuesBR, 1) ' Arrays are 1-based
Select Case valuesBR(i, 1)
Case "SAME"
' Carry over values
ws.Cells(i + 1, "CB").Value = valuesL(i, 1)
ws.Cells(i + 1, "CC").Value = valuesM(i, 1)
ws.Cells(i + 1, "CD").Value = valuesN(i, 1)
Case "REPLACE", "ADD"
' Populate CC with formula
ws.Cells(i + 1, "CC").Formula = "=IFERROR(INDEX(Table1[Description ( Name as defined in Windchill )],MATCH([@[(Part Number)]],Table1[Part Number],0)),""Not in Part Master"")"
Case "DELETE"
' Clear values
ws.Cells(i + 1, "CB").ClearContents
ws.Cells(i + 1, "CC").ClearContents
ws.Cells(i + 1, "CD").ClearContents
End Select
Next i
' Re-enable screen updating and calculation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I’ve also tried using the code below, but keep getting an out of memory error:
Sub UpdateColumnsBasedOnBR()
Dim ws As Worksheet
Dim lastRow As Long
Dim chunkSize As Long
Dim i As Long
Dim startRow As Long
Dim endRow As Long
Dim brData As Variant
Dim lData As Variant
Dim mData As Variant
Dim nData As Variant
Dim cbData() As Variant
Dim ccData() As Variant
Dim cdData() As Variant
' Set the worksheet
Set ws = ThisWorkbook.Sheets("BOM")
' Find the last row with data in column BR
lastRow = ws.Cells(ws.Rows.Count, "BR").End(xlUp).Row
' Set chunk size to process rows in smaller batches
chunkSize = 1000
' Process data in chunks
For startRow = 2 To lastRow Step chunkSize
endRow = startRow + chunkSize - 1
If endRow > lastRow Then endRow = lastRow
' Read relevant columns into arrays for faster processing
brData = ws.Range("BR" & startRow & ":BR" & endRow).Value
lData = ws.Range("L" & startRow & ":L" & endRow).Value
mData = ws.Range("M" & startRow & ":M" & endRow).Value
nData = ws.Range("N" & startRow & ":N" & endRow).Value
' Initialize output arrays
ReDim cbData(1 To UBound(brData, 1), 1 To 1)
ReDim ccData(1 To UBound(brData, 1), 1 To 1)
ReDim cdData(1 To UBound(brData, 1), 1 To 1)
' Loop through the arrays and update values based on BR values
For i = 1 To UBound(brData, 1)
Select Case brData(i, 1)
Case "SAME"
cbData(i, 1) = lData(i, 1)
ccData(i, 1) = mData(i, 1)
cdData(i, 1) = nData(i, 1)
Case "REPLACE", "ADD"
' Formulas will be handled separately
ccData(i, 1) = "=IFERROR(INDEX(Table1[Description ( Name as defined in Windchill )],MATCH([@[(Part Number)]],Table1[Part Number],0)),""Not in Part Master"")"
Case "DELETE"
cbData(i, 1) = vbNullString
ccData(i, 1) = vbNullString
cdData(i, 1) = vbNullString
End Select
Next i
' Write the updated arrays back to the worksheet
ws.Range("CB" & startRow).Resize(UBound(cbData, 1), 1).Value = cbData
ws.Range("CC" & startRow).Resize(UBound(ccData, 1), 1).Value = ccData
ws.Range("CD" & startRow).Resize(UBound(cdData, 1), 1).Value = cdData
Next startRow
' Handle the formula separately, as formulas cannot be directly written through arrays
For i = 2 To lastRow ' Assuming row 1 is header
If ws.Cells(i, "BR").Value = "REPLACE" Or ws.Cells(i, "BR").Value = "ADD" Then
ws.Cells(i, "CC").Formula = "=IFERROR(INDEX(Table1[Description ( Name as defined in Windchill )],MATCH([@[(Part Number)]],Table1[Part Number],0)),""Not in Part Master"")"
End If
Next i
End Sub
Andrew Okada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.