add up certain items then multiply by a constant value and display the result on another row

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need my macro to calculate the quantity of item "evergrip", the calculation goes like the following:
It calculates the total number of "Base", "Riser", "Cone", "Top slab" with the keyword "Sanitary" in column O.
the qty of these items are always in column C, the description of these items are in column K.

the macro then run this equation: (total # of these items - 1) * 14 and display the qty on a new row that is added after the last row with any data

see my code below, it looks like this is not counting the items correctly, my result always returns as -14

test file link below

VBA Code:
Public sr As Long, n As Long, lr10 As Long, lr As Long, lr6 As Long
Sub SealantEvergrip()
    lr = ws1.Cells(Rows.Count, "K").End(xlUp).Row
    sr = 2 'Starting Row
    n = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*Base**Riser**Cone**Top Slab*", Range("O" & sr & ":O" & lr), "*Sanitary*")
    lr10 = lr6 + 1 'this is the new row at the bottom that we are going to write the data to.

    Cells(lr10, "A") = Cells(lr, "A")
    Cells(lr10, "B") = "."
    Cells(lr10, "C") = (n - 1) * 14
    Cells(lr10, "D") = "F09510A"
    Cells(lr10, "I") = "Purchased"
    Cells(lr10, "K") = "Evergrip"

    If Cells(lr10, "C").Value Like "*0*" Then
     Cells(lr10, 4) = ","
    End If
End Sub
 
This prints 59 and writes 812 in the target cell
VBA Code:
Sub SealantEvergrip()

'lr = ws1.Cells(Rows.Count, "K").End(xlUp).Row
lr = Cells(Rows.Count, "K").End(xlUp).Row '<< ws1 is not declared or set
sr = 2 'Starting Row
n = Application.Sum(Application.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), _
Array("*Base*", "*Riser*", "*Cone*", "*Top Slab*"), Range("O" & sr & ":O" & lr), "*Sanitary*"))

Debug.Print n

''lr10 = lr6 + 1 'this is the new row at the bottom that we are going to write the data to.
lr10 = lr + 1 '<< lr10, lr6 is zero, so you're writing in the top row, not the last. Don't see a need for lr10 or lr6
Cells(lr10, "A") = Cells(lr, "A")
Cells(lr10, "B") = "."
Cells(lr10, "C") = (n - 1) * 14
Cells(lr10, "D") = "F09510A"
Cells(lr10, "I") = "Purchased"
Cells(lr10, "K") = "Evergrip"

If Cells(lr10, "C").Value Like "*0*" Then
  Cells(lr10, 4) = ","
End If
That only took about 4 hours of playing around and researching. :rolleyes:
this works great !
a little note though, this is part of a bigger macro, there are lr1, 2, 3, 4, 5, 6, 7, 8, 9 being used to serve other purposes.
lr6 is used for adding the last new row right before this evergrip new row, thats why I set lr10 for this subroutine, and set it to lr6 + 1
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
OK; you'll need to modify what I wrote just a bit I guess because for me, some variable values were zero so it was editing the first row. If you've got a solution, maybe mark this one as solved?
 
Upvote 0
OK; you'll need to modify what I wrote just a bit I guess because for me, some variable values were zero so it was editing the first row. If you've got a solution, maybe mark this one as solved?
I already marked your code above as the solution . thanks a bunch !
 
Upvote 0
You're welcome. Good thing I'm not getting paid by the hour - I would have lost money on that one!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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