Rounding up with VBA

Brian Sells

Board Regular
Joined
Oct 17, 2011
Messages
58
I have a spreadsheet that is generated out of a point of sale system. I have created a macro and modified the VBA to format this into a pick list for a my warehouse. Here is my problem: Certain item numbers have to be ordered in case quantities of either 10, 12 or 24. The report generated from the point of sale only show how many were sold. How can I use VBA to round up a number only if the item number in the adjacent cell is a certain one?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
So if you had a value in A1, and there was a value in B1, you would want to round up to that multiple, otherwise return the value in A1?

You can add other checks to make sure the cells actually contain numbers, but basically:

Code:
Sub test()
If Range("B1") > 0 Then
    Range("C1") = -Int(-Range("A1") / Range("B1")) * Range("B1")
Else
    Range("C1") = Range("A1")
End If
End Sub
 
Last edited:
Upvote 0
I think that is close to what I am looking for, let me see if I can explain a little better, the worksheet has 3 columns, A,B and C.

Column A is my item number for that product (i.e. H467), Column B is the number being ordered (i.e. 6), Column C is just the product name.

Certain item numbers require them to order a minimum of either 10, 12 or 24.

I need it to search column A for specific item numbers, if they are found, I need the corresponding number ordered in Column B to round up to the minimum requirement.

I can't do it with a formula in the sheet, because the spreadsheet is generated fresh from the point of sale system every week. I have a macro with a hot key set up to format the sheet to what our warehouse needs for a pick list saved into a personal macro book so no matter what sheet I open, the hot key does the same thing. This is the final step, making sure the minimum order requirements get met. Thanks for your help on this!

This sheet can be as small as 10 line items up to about 2000.
 
Upvote 0
Where is the information held that determines what multiple of an item needs to be ordered?
 
Upvote 0
It is not in the worksheet. There are just a few items that require this. I suppose I could include the numbers in the personal macro workbook couldn't I?
 
Upvote 0
Depends how many a "few" is. I don't use a personal macro workbook, so I can't say for sure.
Or you could build a Select Case structure for your data to determine the multiple.

Code:
Sub test()
Dim c As Range, d As Range, multiple As Long
For Each c In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    Select Case UCase(c)
        Case "H467": multiple = 12
        Case "J432": multiple = 10
        Case Else: multiple = 1
    End Select
    Set d = c.Offset(, 1)
    If IsNumeric(d) And d <> "" Then d = -Int(-d / multiple) * multiple
Next
End Sub
 
Upvote 0
I put that in, but it is not doing anything. I am going to attach my code so you can see what I have. I'm fairly new to VBA so what I have so far is probably a little rough around the edges, but it's doing what I need with the exception of that.

Code:
Sub Create_Pick_List()
'
' Create_Pick_List Macro
' Create pick list from POS generated order
'
' Keyboard Shortcut: Ctrl+e
'
    Cells.Select
    Selection.RowHeight = 15
    Selection.ColumnWidth = 8.43
    Rows("1:6").Select
    Selection.Delete Shift:=xlUp
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Check"
    Columns("F:F").Select
    Selection.Cut
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Ordered"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Pulled"
    Columns("E:E").Select
    ActiveWindow.View = xlPageLayoutView
    Columns("A:A").EntireColumn.AutoFit
    Selection.ColumnWidth = 58
    Range("A1:E10").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Dim myrange As String
    Rows("1:1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("C:C").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    ActiveSheet.Cells(3, 2).Select
    
    

myrange = Cells(Rows.Count, 5).End(xlUp).Address

ActiveSheet.PageSetup.PrintArea = "$A$1:" & myrange
  Dim Rws As Long, Col As Integer, r As Range, fRng As Range

    Set r = Range("A1")

    Rws = Cells.Find(what:="*", After:=r, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    Col = Cells.Find(what:="*", After:=r, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column


    Set fRng = Range(Cells(1, 1), Cells(Rws, Col))    ' range A4 to last cell on sheet

    fRng.Select    'or whatever you want to do with the range

    With Selection.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With


' inserts the same header/footer in all worksheets
Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        Application.StatusBar = "Changing header/footer in " & ws.Name
        With ActiveSheet.PageSetup
        .TopMargin = Application.InchesToPoints(1.25)
        .LeftHeader = "&D"
        .CenterHeader = "&B& &18& Pepper Palace"
        .RightHeader = "Ship                           *" & Chr$(13) & "Carrier                           *" & Chr$(13) & "Pallets                           *" & Chr$(13) & "Napalm                           *"
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = "&P"
        
        End With
    Next ws
    Set ws = Nothing
    Application.StatusBar = False
End Sub
Sub test()
Dim c As Range, d As Range, multiple As Long
For Each c In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
    Select Case UCase(c)
        Case "H48": multiple = 12
        Case "N3125": multiple = 10
        Case Else: multiple = 1
    End Select
    Set d = c.Offset(, 1)
    If IsNumeric(d) And d <> "" Then d = -Int(-d / multiple) * multiple
Next
End Sub

Worksheet is set up like this after running the code:

A B C D
ITEM Ordered Picked Description
H48 4 Microwave Popcorn
N3125 2 Wake Up Coffee


These 2 example items, need to be ordered in the higher quantities. For some reason, I can't get anything to work on it. Thanks again for your help.
 
Last edited:
Upvote 0
Here is a better look at the way the sheet is set up:
Worksheet is set up like this after running the code:

A___________ B________C_____________ D
ITEM_____Ordered____Picked_______ Description
H48_________ 4_______(___)_______ Microwave Popcorn
N3125_______ 2_______(___)_______ Wake Up Coffee


These 2 example items, need to be ordered in the higher quantities. For some reason, I can't get anything to work on it. I need the code to look for these item numbers, then round that ordered number up to the minimum requirement. Thanks again for your help.
 
Upvote 0
Finally got it! For some reason, it wasn't calling the sub. I placed the code into my existing sub and it works great. Thanks again for your help!
 
Upvote 0
Glad it worked out for you. Yes you do need to run the sub. You could of also called it from your original routine.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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