Faster Loop with VLOOKUP from another workbook - excel

The code takes information from another range of cells in the same sheet ("Non Order RAW Detail Report") and creates a new one beside it using data from the one to the left.
My code takes from 45 minutes to an hour for 70k rows of cells. Is there a way to make the loops go faster?
Sub cruzar()
Dim i As Long
Dim last As Long
Dim user As String
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Sheets("Non Order RAW Detail Report").Select
last = ActiveSheet.UsedRange.Rows.Count
'-------------Cruzar--------------------------------
Range("W1").Value = "Year"
Range("x1").Value = "Month"
Range("y1").Value = "Creation Date"
Range("z1").Value = "Closed Date"
Range("AA1").Value = "Type Inquiry"
Range("AB1").Value = "Value"
Range("AC1").Value = "Status"
Range("AD1").Value = "Equal Month"
Range("AE1").Value = "Days"
Range("AF1").Value = "Bracket"
Range("W1:AF1").Interior.ColorIndex = 49
Range("W1:AF1").Font.Color = vbWhite
Range("W1:AF1").Font.Bold = True
user = Environ("username")
For i = 2 To last
Cells(i, 23).Value = Cells(i, 15).Value
Cells(i, 23).NumberFormat = "yyyy"
Cells(i, 24).Value = Cells(i, 15).Value
Cells(i, 24).NumberFormat = "mm"
Cells(i, 25).Value = Cells(i, 15).Value
Cells(i, 25).NumberFormat = "mm/dd/yyyy"
Cells(i, 26).Value = Cells(i, 16).Value
Cells(i, 26).NumberFormat = "mm/dd/yyyy"
Cells(i, 27).Formula = "=VLOOKUP(N" & i & ",'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[CategoryList_NAM_v2.xlsx]CATEGORIES'!$I:$J,2,0)"
Cells(i, 28) = 1
If Cells(i, 5).Value = "FCR" Then
Cells(i, 29).Value = "FCR"
Else
Cells(i, 29).Value = "Follow Up"
End If
Cells(i, 30).Formula = "=IF(E" & i & "=""FCR"",""FCR"",IF(AND(MONTH(O" & i & ")=MONTH(P" & i & "),YEAR(O" & i & ")=YEAR(P" & i & ")),""Closed"",""Open""))"
Cells(i, 31).Formula = "=IF(E" & i & "=""FCR"",""FCR"",IF(AD" & i & "=""Open"",""Open"",IF(((P" & i & "-O" & i & ")*24)<24,0,LOOKUP(((P" & i & "-O" & i & ")*24),'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$A$2:$B$366,'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$C$2:$C$366))))"
Cells(i, 32).Formula = "=IF(AE" & i & "=""FCR"",""FCR"",IF(AE" & i & "=""Open"",""Open"",LOOKUP(AE" & i & ",'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$E$2:$F$9,'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$G$2:$G$9)))"
Next
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Related

Excel VBA bug query

I currently am using a userform I created to log data into a spreadsheet. During data validation, I notice that when I key in dates in the DD/MM/YYYY format in the userform, some rows swap the DD/MM to MM/DD, which causes confusion downstream.
I adjusted the data type for the entire column, but the userform code seems act differently for the particular row. Is this a bug or am I overlooking a line of code somewhere?
Here are the images of the userform and the data in the spreadsheet, as well as the code for the information transfer segment.
Data from spreadsheet
Userform Date segment
Code for transfer information:
'Transfer information
Cells(emptyRow, 1).Value = p
Cells(emptyRow, 3).Value = hour.Value & ":" & minute.Value & " " & ampm.Value
Cells(emptyRow, 4).Value = PTID.Value
Cells(emptyRow, 2).Value = cmbdate.Value & "/" & cmbmonth.Value & "/" & cmbyear.Value
Cells(emptyRow, 5).Value = UNIT.Value
Cells(emptyRow, 6).Value = PCBOX.Value
Cells(emptyRow, 7).Value = WASTE.Value
Cells(emptyRow, 8).Value = REPORTED.Value
Cells(emptyRow, 9).Value = DETBOX.Value
Cells(emptyRow, 10).Value = FOLBOX.Value
Cells(emptyRow, 11).Value = SUMBOX.Value
Cells(emptyRow, 12).Value = CAPBOX.Value
Cells(emptyRow, 13).Value = EHOR.Value
Cells(emptyRow, 14).Value = TECHS.Value & "," & TECHS2.Value & "," & TECHS3.Value & "," & TECHS4.Value
Cells(emptyRow, 15).Value = ERRORBOX.Value
Cells(emptyRow, 16).Value = PREVBOX.Value
Cells(emptyRow, 17).Value = SOP.Value
Cells(emptyRow, 18).Value = AUDIFILE.Value
Cells(emptyRow, 19).Value = INTERFILE.Value
Cells(emptyRow, 20).Value = cmbdate2.Value & "/" & cmbmonth2.Value & "/" & cmbyear2.Value
Cells(emptyRow, 23).Value = Phase.Value
Cells(emptyRow, 24).Value = QIM.Value
MsgBox "Please check your entry in the sheet", , "Entry Complete"
MsgBox "Your entry serial number is " & p
SN.Text = p
VNCFORM.Hide
Code for recall information (to same userform, when a serial number for the entry is entered into the userform)
Private Sub SN_AfterUpdate()
'TO RETRIEVE S/N DATA TO THE USERFORM'
Dim x As Range
Dim y As Long
Set WS = Worksheets("Data")
y = Application.WorksheetFunction.Match(CLng(Me.SN.Value), WS.Range("A:A"), 0)
'POSSIBLE PROBLEM AREA'
Me.cmbdate.Value = Left(WS.Range("B" & y).Value, 2)
Me.cmbmonth.Value = Mid(WS.Range("B" & y).Value, 4, 2)
Me.cmbyear.Value = Right(WS.Range("B" & y).Value, 4)
Me.hour.Value = CStr(Left(WS.Range("C" & y).Value, 2))
Me.minute.Value = CStr(Mid(WS.Range("C" & y).Value, 4, 2))
Me.ampm.Value = CStr(Right(WS.Range("C" & y).Value, 2))
Me.PTID.Value = WS.Range("D" & y).Value
Me.UNIT.Value = WS.Range("E" & y).Value
Me.PCBOX.Value = WS.Range("F" & y).Value
Me.WASTE.Value = WS.Range("G" & y).Value
Me.REPORTED.Value = WS.Range("H" & y).Value
Me.DETBOX.Value = WS.Range("I" & y).Value
Me.FOLBOX.Value = WS.Range("J" & y).Value
Me.SUMBOX.Value = WS.Range("K" & y).Value
Me.CAPBOX.Value = WS.Range("L" & y).Value
Me.EHOR.Value = WS.Range("M" & y).Value
'Techs involved in case transcribed back to userform
Dim MYARRAY() As String, MYSTRING As String
MYSTRING = WS.Range("N" & y).Value
MYARRAY = Split(MYSTRING, ",")
For N = 0 To UBound(MYARRAY)
Me.TECHS.Value = MYARRAY(0)
Me.TECHS2.Value = MYARRAY(1)
Me.TECHS3.Value = MYARRAY(2)
Me.TECHS4.Value = MYARRAY(3)
Next N
Me.ERRORBOX.Value = WS.Range("O" & y).Value
Me.PREVBOX.Value = WS.Range("P" & y).Value
Me.SOP.Value = WS.Range("Q" & y).Value
Me.AUDIFILE.Value = WS.Range("R" & y).Value
Me.INTERFILE.Value = WS.Range("S" & y).Value
Me.cmbdate2.Value = Left(WS.Range("T" & y).Value, 2)
Me.cmbmonth2.Value = Mid(WS.Range("T" & y).Value, 4, 2)
Me.cmbyear2.Value = Right(WS.Range("T" & y).Value, 4)
Me.Phase.Value = WS.Range("W" & y).Value
Me.QIM.Value = WS.Range("X" & y).Value
End Sub
The problem seems to occur when I recall data back into the userform where the month value and the date values get swapped for some reason.
Is there a property of code I am overlooking? Or could I improve the code somehow; I think the error comes from the recall segment (see: 'POSSIBLE PROBLEM AREA')
I would make sure when writing/reading dates you're being more explicit:
'write date to sheet
Cells(emptyRow, 2).Value = DateSerial(CLng(cmbyear.Value), _
CLng(cmbmonth.Value), _
CLng(cmbdate.Value))
'read date from sheet
Dim dt As Date
dt = WS.Range("B" & y).Value
Me.cmbdate.Value = Day(dt)
Me.cmbmonth.Value = Month(dt)
Me.cmbyear.Value = Year(dt)
Also likely need some code to check there's an entry before trying to write/read it.

Why is data being added to other worksheets in this particular code?

Using a userform, I have created an 'Update' function to the form to make amendments to certain rows of data.
For some reason, when I make an amendment and click on the 'Update' button, the data from columns T, P and W are randomly added to Sheet1 instead of Sheet2.
Private Sub Update_Click()
Dim selectedRow As Long
'Make Sheet2 active
Sheet2.Activate
Dim x As Range
Set WS = Worksheets("Data")
selectedRow = Application.WorksheetFunction.Match(CLng(Me.SN.Value), WS.Range("A:A"), 0)
'Transfer information
Cells(selectedRow, 3).Value = hour.Value & ":" & minute.Value & " " & ampm.Value
Cells(selectedRow, 4).Value = PTID.Value
Cells(selectedRow, 2).Value = cmbdate.Value & "/" & cmbmonth.Value & "/" & cmbyear.Value
Cells(selectedRow, 5).Value = UNIT.Value
Cells(selectedRow, 6).Value = PCBOX.Value
Cells(selectedRow, 7).Value = WASTE.Value
Cells(selectedRow, 8).Value = REPORTED.Value
Cells(selectedRow, 9).Value = DETBOX.Value
Cells(selectedRow, 10).Value = FOLBOX.Value
Cells(selectedRow, 11).Value = SUMBOX.Value
Cells(selectedRow, 12).Value = CAPBOX.Value
Cells(selectedRow, 13).Value = EHOR.Value
Cells(selectedRow, 14).Value = TECHS.Value & "," & TECHS2.Value & "," & TECHS3.Value & "," & TECHS4.Value
Cells(selectedRow, 15).Value = ERRORBOX.Value
Cells(selectedRow, 16).Value = PREVBOX.Value
Cells(selectedRow, 17).Value = SOP.Value
Cells(selectedRow, 18).Value = AUDIFILE.Value
Cells(selectedRow, 19).Value = INTERFILE.Value
Cells(selectedRow, 20).Value = cmbdate2.Value & "/" & cmbmonth2.Value & "/" & cmbyear2.Value
Cells(selectedRow, 23).Value = Phase.Value
Cells(selectedRow, 24).Value = QIM.Value
MsgBox "Entry updated, please check your entry.", , "Entry Update"
End Sub
The problem seems to occur with the lines for columns, 16, 20 and 23 (which refer to the PREVBOX, dates, and Phase values respectively. Attached below is an image of the stray cells being copied to Sheet1 cells highlighted in yellow (and effectively overwriting any cells on that sheet).
The rows and columns also match the rows and columns of the data on Sheet2 but I'm not sure if the cells are being overwritten in Sheet2 which is the intention of the update button.

Dynamic Update Concatenate function for multiple columns

I have code which is used for concatenation. The cells are dynamic; whenever a change in cells in a range the concatenation function will automatically execute and gives the value. Currently I asked the concatenation function which has to run for the complete range even though the modification is in a single row. Which is causing a lot of time during the execution.
Is there is any way to define to update only a single row that is modified? I know the technique if the range is single column, for multiple columns I didn't have any idea.
My Code
ColumnLetter3 = Split(Cells(1, c1_column).Address, "$")(1)
ColumnLetter4 = Split(Cells(1, c6_column).Address, "$")(1)
Range3 = ColumnLetter3 & st_workrow2 + 1 & ":" & ColumnLetter4 & last_cell1
Set xrng3 = Range(Range3)
If Not Application.Intersect(xrng3, Range(Target.Address)) _
Is Nothing Then
For i = c_row + 1 To last_cell1
If Cells(i, c1_column) = "" And Cells(i, c2_column) = "" And Cells(i, c3_column) = "" And Cells(i, c4_column) = "" And Cells(i, c5_column) = "" And Cells(i, c6_column) = "" Then
Cells(i, c_column) = ""
Else
Cells(i, c_column) = Cells(i, c1_column) & "-" & Cells(i, c2_column) & "-" & Cells(i, c3_column) & "-" & Cells(i, c4_column) & "-" & Cells(i, c5_column) & "-" & Cells(i, c6_column)
Cells(i, c_column).Replace what:="+", Replacement:=""
Cells(i, c_column).Replace what:="-----", Replacement:="-"
Cells(i, c_column).Replace what:="----", Replacement:="-"
Cells(i, c_column).Replace what:="---", Replacement:="-"
Cells(i, c_column).Replace what:="--", Replacement:="-"
If Right(Cells(i, c_column), 1) = "-" Then
l = Len(Cells(i, c_column))
Cells(i, c_column) = Left(Cells(i, c_column), l - 1)
End If
If Left(Cells(i, c_column), 1) = "-" Then
l = Len(Cells(i, c_column))
Cells(i, c_column) = Right(Cells(i, c_column), l - 1)
End If
End If
Next I
Endif
It's hard to tell exactly what you're doing here (perhaps strip down your question?), however looks like you want to get a list of the rows in your target? In that case you can isolate it using Columns(1). See below...
If Not Application.Intersect(xrng3, Range(Target.Address)) _
Is Nothing Then
Dim aCell As Range
For Each aCell In Target.Columns(1).Cells
i = aCell.Row
If Cells(i, c1_column) = "" And Cells(i, c2_column) = "" And Cells(i, c3_column) = "" And Cells(i, c4_column) = "" And Cells(i, c5_column) = "" And Cells(i, c6_column) = "" Then
'skipped....
End If
Next aCell
End If

Runtime Error: 1004 : Save as method of worksheet class failed on MAC

This is MAC specific issue, on WindowsOS, it works absolutely fine.
In this macro, I am fetching values from certain cells (from Sheet 1) & then copying this concatenated text to Sheet 3 of this spreadsheet.
While executing the code on MAC having Excel 2016, I am receiving the error
RunTime Error 1004: SaveAs method of Worsheet class Failed.
Error code as below:
Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", FileFormat:=xlCSV, CreateBackup:=False
Complete code is as below:
Sub Generate()
ThisWorkbook.Worksheets("Sheet1").Unprotect ("laces")
Dim sOutputPath As String
Dim val2 As Variant
Dim lastrow As Long
path1 = ActiveWorkbook.Path
Set oExcel = CreateObject("Excel.Application")
Set Osheet = ActiveWorkbook.Worksheets("Sheet1")
sVal1 = Osheet.Cells(37, 2).Value & Osheet.Cells(37, 3).Value & Osheet.Cells(37, 4).Value & ";" 'Reference Mapping
sVal1 = sVal1 & Osheet.Cells(2, 9).Value & ";" 'Date Mapping
sVal1 = sVal1 & Osheet.Cells(29, 2).Value & Osheet.Cells(29, 3).Value & Osheet.Cells(29, 4).Value & ";" 'Name Mapping
sVal1 = sVal1 & Osheet.Cells(28, 2).Value & Osheet.Cells(28, 3).Value & Osheet.Cells(28, 4).Value & ";" 'Company Mapping
sVal1 = sVal1 & Osheet.Cells(30, 2).Value & Osheet.Cells(30, 3).Value & Osheet.Cells(30, 4).Value & ";" 'Street1 Mapping
sVal1 = sVal1 & "a" & ";" 'Street2 Mapping
sVal1 = sVal1 & Osheet.Cells(31, 2).Value & Osheet.Cells(31, 3).Value & Osheet.Cells(31, 4).Value & ";" 'Zip Code Mapping
sVal1 = sVal1 & Osheet.Cells(32, 2).Value & Osheet.Cells(32, 3).Value & Osheet.Cells(32, 4).Value & ";" 'City Mapping
sVal1 = sVal1 & "" & ";" 'Region Mapping
sVal1 = sVal1 & Osheet.Cells(33, 2).Value & Osheet.Cells(33, 3).Value & Osheet.Cells(33, 4).Value & ";" 'Country Mapping
sVal1 = sVal1 & Osheet.Cells(34, 2).Value & Osheet.Cells(34, 3).Value & Osheet.Cells(34, 4).Value & ";" 'Email Mapping
sVal1 = sVal1 & Osheet.Cells(35, 2).Value & Osheet.Cells(35, 3).Value & Osheet.Cells(35, 4).Value & ";" 'Phone Mapping
hData1 = "Reference;Date;Name;Company;Street1;Street2;Zip Code;City;Region;Country;Email;Phone;Product;SKU;Quantity"
Worksheets("Sheet3").Range("A1").Value = hData1
hData1 = ""
'Product & Quality Mapping
For i = 6 To 13
S = Osheet.Cells(i, 6).Value
M = Osheet.Cells(i, 7).Value
L = Osheet.Cells(i, 8).Value
XL = Osheet.Cells(i, 9).Value
If (S = "" And M = "" And L = "" And XL = "") Then
Else
' SKU Mapping
SKU = Osheet.Cells(i, 1).Value & " - " & Osheet.Cells(i, 2).Value
If S <> "" Then
expKey = Osheet.Cells(i, 2).Value & "S"
sQuantity = Osheet.Cells(i, 6)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 6).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 6).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If M <> "" Then
expKey = Osheet.Cells(i, 2).Value & "M"
sQuantity = Osheet.Cells(i, 7)
val4 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 7).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 7).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val4 & ";" & sQuantity
val4 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If L <> "" Then
expKey = Osheet.Cells(i, 2).Value & "L"
sQuantity = Osheet.Cells(i, 8)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 8).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 8).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If XL <> "" Then
expKey = Osheet.Cells(i, 2).Value & "XL"
sQuantity = Osheet.Cells(i, 9)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 9).Value, 2))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 9).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
End If
Next
Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
MsgBox "You can find the generated CSV file in " & path1
Sheet3.Cells.Clear
ActiveWorkbook.Close savechanges:=False
Set oExcel = Nothing
ThisWorkbook.Worksheets("Sheet1").Protect ("laces")
End Sub
Function findSKU(farb As String, size As String) As String
Dim ri As Long
For ri = 2 To 33
If Sheet2.Cells(ri, 1).Value = farb And Sheet2.Cells(ri, 2).Value = size Then
findSKU = Sheet2.Cells(ri, 3).Value
Exit Function
End If
Next ri
End Function
Any help would be highly appreciated.
Many thanks.

How to run this code faster

I have this code where the content is: filling formulas in cells that have no formula, then copy/paste values of entire row of those cells. I used 2 times "For Next", first for filling formula and the second to paste values
Sub CDPSKoCongThuc()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim RowNKC As Integer
RowNKC = Range("CuoiNKC").Row - 1
Dim RowCDPS As Integer
RowCDPS = Range("CuoiCDPS").Row - 1
Dim i As Integer
Dim x As Integer
For i = 9 To RowCDPS
If Cells(i, 9).HasFormula = False Then
Cells(i, 7).FormulaR1C1 = "=SUMIF(NKC!R9C12:R" & RowNKC & "C12,CDPS!RC[-6],NKC!R9C15:R" & RowNKC & "C15)"
Cells(i, 8).FormulaR1C1 = "=SUMIF(NKC!R9C13:R" & RowNKC & "C13,CDPS!RC[-7],NKC!R9C15:R" & RowNKC & "C15)"
Cells(i, 9).FormulaR1C1 = "=ROUND(SUMIF(NKC!R9C12:R" & RowNKC & "C12,CDPS!RC[-8],NKC!R9C14:R" & RowNKC & "C14),0)"
Cells(i, 10).FormulaR1C1 = "=ROUND(SUMIF(NKC!R9C13:R" & RowNKC & "C13,CDPS!RC[-9],NKC!R9C14:R" & RowNKC & "C14),0)"
Cells(i, 11).FormulaR1C1 = "=MAX(RC[-8]+RC[-4]-RC[-3]-RC[-7],0)"
Cells(i, 12).FormulaR1C1 = "=MAX(RC[-4]+RC[-8]-RC[-5]-RC[-9],0)"
Cells(i, 13).FormulaR1C1 = "=ROUND(MAX(RC[-8]+RC[-4]-RC[-7]-RC[-3],0),0)"
Cells(i, 14).FormulaR1C1 = "=ROUND(MAX(RC[-4]+RC[-8]-RC[-5]-RC[-9],0),0)"
End If
Next i
'Paste Values Formula
For x = 9 To RowCDPS
If Cells(x, 9).Font.Bold = False And Len(Cells(x, 1).Value) > 3 Then
Rows(x).EntireRow.Copy
Rows(x).PasteSpecial xlPasteValues
End If
Next x
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I'm not going to write them all but you could use VBA to do the caculations and put the value in the cell so you don't have to paste the values or work with excel calculations at all.
For example...
Cells(i, 11).FormulaR1C1 = "=MAX(RC[-8]+RC[-4]-RC[-3]-RC[-7],0)"
would be
Cells(i, 11) = Application.Max(Cells(i, 3) + Cells(i, 7) - Cells(i,8) -Cells(i, 4), 0)
You should also make a variable for each sheet and use them to reference the cells so that its clearer to read
Dim shCDPS as Worksheet
Dim shNKC as worksheet
Set shCDPS = Sheets("CDPS")
Set shNKC = Sheets("NKC")
Then this formula
Cells(i, 7).FormulaR1C1 = "=SUMIF(NKC!R9C12:R" & RowNKC & "C12,CDPS!RC[-6],NKC!R9C15:R" & RowNKC & "C15)"
would become
shCDPS.Cells(i, 7) = Application.SumIf(shNKC.Range("L9:L" & RowNKC) , shCDPS.Range("A" & i), shNKC.Range("O15:O" & RowNKC))

Resources