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:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Perhaps you meant to have ElseIf there?

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)
    
    ElseIf WSales.Cells(i, 6).Value = 5 Then
        '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

Also, when posting code, please wrap your code in [CODE][/CODE] tags so that it retains indention.
 
Upvote 0
Thanks for the note on the code. I'll remember that from now on. I tried Else If and it It still has the same run time error.

Code:
ElseIf WSales.Cells(i, 6).Value = 5 Then
'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)
 
Upvote 0
Have you checked if any of the cells in that piece of code refer to cells/ranges with errors?

Or even the wrong data type for a calcualtion, eg text?

PS That line of code doesn't actually appear to be anything to do with an if.:)

Just saw your last post, and I think you should check if thatVLOOKUP in the first line of code is returning #N/A!.

If it is then that might be what's causing the problem in the second line of code.
 
Last edited:
Upvote 0
actually cell B2 has the same date as B3, but different product ID and different quantities in Column G. So Column B can have multiple dates with the same product ID.

Column B Column F Column G
1/1/11 4 25
1/1/11 5 35
1/1/11 4 10
1/1/11 4 2
 
Upvote 0
Before I started the If Else statement I use just a simple If statement to check if product ID was 4, which was the first line of vlookup, and that worked. Then I figured I could add a IfElse statement since I had more than 1 product ID, and that's when things finally went to hell.
 
Upvote 0
I really don't think this has anything to do with the If statements.

The line of code you say causes the error seems to be doing some sort of calculation.

One of the cells you are using for that calculation contains a VLOOKUP formula.

If that formula is returning an error, eg N/A, then the code for the calculation won't work.:)
 
Upvote 0
Can you clarify what you mean that the cells have a vlookup in them? When I try to run the macros it stops at row 3 with a "#Ref"
 
Upvote 0
This line of code puts a VLOOKUP formula in a cell you then use in the next line.
Code:
WSales.Cells(i, 13).FormulaR1C1 = "=VLOOKUP(RC2,COGSList,3,False)"
If that formula is returning a #REF! error, or any other error then the next line of code which uses the value (actually error) from that cell will fail.
 
Upvote 0
Oh well, I still can't figure it out. :( I just decided to duplicate the code and instead of searching for the value 4 it searches for 5, and it works just fine. Thanks again to everyone who tried to help me.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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