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:
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: