Run time error 13 in an If Else

coconutmango

New Member
Joined
Mar 10, 2011
Messages
10
Still new to VBA, and I'm on the verge of tearing my hair out. I'm running into a run time error 13 in my If Else statement. What I'm trying to do is run through data in column F "Product ID" of my sheet called "Raw Sales" and if the value is a 4 it uses a vlookup to look up value in column B (a date) and pull column 2 data in spreadsheet Cost of Goods. If the value is a 5 then it does the same process, except it pulls column 3 data from spreadsheet Cost of Goods. I end up with a run time error 13 on this line of code:

WSales.Cells(i, 14).Value = (WSales.Cells(i, 8) - WSales.Cells(i, 13)) * WSales.Cells(i, 7)

Full code:
Code:
Sub LoopCOGSCal()
'
' LoopCOGSCal Macro
'
 
'
'Creates the variables
Dim WData As Worksheet
Dim WCogs As Worksheet
 
'Defines worksheets
Set WSales = Worksheets("Raw Sales")
Set WCogs = Worksheets("Cost of Goods")
 
'Finds the end of the COGS list
FinalCOGS = WCogs.Range("A" & WCogs.Rows.Count).End(xlUp).Row
 
'Redefines the COGS list
WCogs.Range("A2:C" & FinalCOGS).Name = "COGSList"
 
'Finds the last row of data in the Raw Sales Sheet and returns the row number
LastRow = Cells(Rows.Count, 6).End(xlUp).Row
 
'Inserts the word "Margin" in cell M1
WSales.Range("M1").Value = "Margin"
 
'Inserts the word "Profit" in cell N2
WSales.Range("N1").Value = "Profit"
 
'Searches in Raw Sales sheet starting from row 2 column F for any product ID = 4 and then looks up column B for the date against the COGS date to return the price sold that day
For i = 2 To LastRow
If WSales.Cells(i, 6).Value = 4 Then
 
'This inserts the Vlookup value of the date on Raw Sales for product ID #4 against the data on COGS, and returns the price
WSales.Cells(i, 13).FormulaR1C1 = "=VLOOKUP(RC2,COGSList,2,False)"
WSales.Cells(i, 14).Value = (WSales.Cells(i, 8) - WSales.Cells(i, 13)) * WSales.Cells(i, 7)
 
Else: WSales.Cells(i, 6).Value = 5
'This inserts the Vlookup value of the date on Raw Sales for productID #5 against the data on COGS, and returns the price
WSales.Cells(i, 13).FormulaR1C1 = "=VLOOKUP(RC2,COGSList,3,False)"
WSales.Cells(i, 14).Value = (WSales.Cells(i, 8) - WSales.Cells(i, 13)) * WSales.Cells(i, 7)
 
End If
Next i
 
End Sub
 
Last edited by a moderator:
The problem is the #REF, that's an error and can't be used in a calculation.:)
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,215,336
Messages
6,124,329
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top