-
Give this a test. I've added a messagebox for info during testing
[vba]Option Explicit
Dim msg As String
Sub TestPrice()
Dim wasPriceListOpen As Boolean
Dim WB As Workbook, pvt As Worksheet, PLS As Worksheet
Dim FinalRow As Long, i As Long
Dim Cel As Range
Dim ThsBook As Workbook
Application.DisplayAlerts = False
On Error Resume Next
Set ThsBook = ThisWorkbook
Set WB = Workbooks("PriceListBook.xls")
Set pvt = ActiveWorkbook.Sheets("Report")
FinalRow = pvt.Cells(Rows.Count, "I").End(xlUp).Row
wasPriceListOpen = True
If Err = 9 Then
Set WB = Workbooks.Open("C:\AAA\PriceListBook.xls")
wasPriceListOpen = False
End If
Err.Clear
Application.DisplayAlerts = True
ThsBook.Activate
Set PLS = WB.Sheets("Price List")
For Each Cel In Range(Cells(2, "I"), Cells(FinalRow, "I"))
If Cel <> "" Then
Select Case WHLoc(Cel.Value)
Case "East"
CheckPrice PLS, Cel, 5: msg = "East"
Case "Cent"
CheckPrice PLS, Cel, 6: msg = "Central"
Case "West"
CheckPrice PLS, Cel, 7: msg = "West"
End Select
End If
Next
If wasPriceListOpen = False Then
WB.Close
End If
End Sub
Function WHLoc(Data As String) As String
Dim EastCoast, Central, WestCoast, tmp As Long
EastCoast = Array("NJ01", "NJH6")
Central = Array("COA5", "FLnR7", "FLn34", "FLs01", "GAG2", "MAE2", "MID5", "PAC2", "PA78", "SCC4", "TX05", "TX27")
WestCoast = Array("CAn67", "CAn99", "CAsE3", "CAs04", "CAs06", "CAs07", "CAs82")
On Error Resume Next
tmp = Application.WorksheetFunction.Match(Data, EastCoast, 0)
If tmp > 0 Then
WHLoc = "East"
Exit Function
End If
tmp = Application.WorksheetFunction.Match(Data, Central, 0)
If tmp > 0 Then
WHLoc = "Cent"
Exit Function
End If
tmp = Application.WorksheetFunction.Match(Data, WestCoast, 0)
If tmp > 0 Then
WHLoc = "West"
Exit Function
End If
End Function
Sub CheckPrice(WS As Worksheet, Cel As Range, Oset As Long)
Dim WHPrice As Single
WHPrice = WS.Columns(4).Find(Cel.Offset(, -7)).Offset(, Oset)
If Cel.Offset(, 1) < WHPrice Then
Cel.Offset(, 1).Interior.ColorIndex = 33
Else
Cel.Offset(, 1).Interior.ColorIndex = xlNone
End If
MsgBox "Item " & Format(Cel.Offset(, -7), "00-0000") & vbCr & msg & _
" Warehouse price is " & Format(WHPrice, "$##0.00") & vbCr _
& "Report price is " & Format(Cel.Offset(, 1), "$##0.00")
End Sub
[/vba]
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules