I'm updating a table of capacities which are based on an availability code. Availability codes in specific ranges have the same capacity (with a few exceptions). Each availability code is listed in column "F". The associated capacity is to be listed in column "I".
The goal of the code is to:
- Read the availability code in cell "F" - can be any nnn from 300-799
- Determine the range in which it falls - 300-499, 500-599, 600-699, 700-799, with exceptions 762, 763, 764, 765, 768
- Assign a capacity in cell "I" - currently using
Row.Offset
Iterate down one row and repeat to the last "F" cell with data
Sub Capacity() Dim ACNum As Range Dim WB As Workbook Dim WS As Worksheet Set WB = ActiveWorkbook Set WS = WB.ActiveWorksheet Set ACNum = WS.Range("F:F") For Each Row In ACNum.Cells Set AC = WS.Cells(WS.Rows.Count, "F").End(xlUp).Offset(1) If (AC = "762" Or AC = "763" Or AC = "764" Or AC = "765" Or AC = "768") Then Row.Offset(0, 3).Value = "72" ElseIf (AC >= "300" And AC <= "499") Then Row.Offset(0, 3).Value = "181" ElseIf (AC >= "500" And AC <= "599") Then Row.Offset(0, 3).Value = "163" ElseIf (AC >= "600" And AC <= "699") Then Row.Offset(0, 3).Value = "124" ElseIf (AC >= "700" And AC <= "799") And _ (AC <> "762" Or AC <> "763" Or AC <> "764" Or AC <> "765" Or AC <> "768") Then Row.Offset(0, 3).Value = "144" End If Next Row End Sub
Each time I execute this I receive a generic error, highlighted in yellow, at Line 13, the start of the first If
statement. If I attempt to use Excel's Evaluate tool, it's options are greyed out and I can't step through the macro.
I'm unable to determine what I'm doing wrong. I haven't worked in Excel VBA for some time so I may be missing a minor detail. This seems so straight-forward.
edited: added a "formula" approach solution
this should do
as per your explanation I assumed that "availability code" is actually a number so it's much better to declare and use a variable (
code
) of type "integer" to handle it and then useSelect Case
statement to go away with parsing criteria avoiding the repetition of a lot of charactersas Dan Donoghue proposed you may want to use a formula approach. I came up to the following, using the "Median" function as from http://chandoo.org/wp/2010/06/24/between-formula-excel/
which, of course, you may also implement directly in excel cells instead of having a VBA macro running