Writing Excel VBA Code

Wetaka

New Member
Joined
Sep 1, 2018
Messages
3
Hullo,

I am Wetaka of Uganda, East Africa.

I Have started learning and using Excel VBA and I find your videos very Educative.

In my Data entry sheet invetory management i have been using the following formula and I would like write it in Excel VBA

How do we write the VBA code for the Formula: =MIN(SUMIF(I:I,I3,L:L)-SUMIF(I$1:I2,I3,O$1:O2),K3)

It finds the first items out of Stock.

Your help will be of great use

Thank you

Wetaka
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the board. Try:
Code:
Sub M1()

    Dim s   As String
    Dim x   As Long
    
    x = Cells(Rows.Count, 9).End(xlUp).Row - 2
    s = "=MIN(SUMIF($I$1:$I$LR,I3,$L$1:$L$LR)-SUMIF(I$1:I2,I4,O$1:O2),K3)"
    s = Replace(s, "LR", x)
    
   Cells(3, Cells(3, Columns.Count).End(xlToLeft).Offset(, 1).Column).Resize(x).Formula = s
   
End Sub
 
Last edited:
Upvote 0
Hullo JackDanIce,

Thank you for your help.

I tried to use the code exactly as I received it ie,
Sub M1()

Dim s As String
Dim x As Long

x = Cells(Rows.Count, 9).End(xlUp).Row - 2
s = "=MIN(SUMIF($I$1:$I$LR,I3,$L$1:$L$LR)-SUMIF(I$1:I2,I4,O$1:O2),K3)"
s = Replace(s, "LR", x)

Cells(3, Cells(3, Columns.Count).End(xlToLeft).Offset(, 1).Column).Resize(x).Formula = s

End Sub

The results was the following formula in column K(Value Out)as in the table below




=MIN(SUMIF($I$1:$I$24,I3,$L$1:$L$24)-SUMIF(I$1:I2,I4,O$1:O2),K3)
every time I make an entry and submit the formula is inserted in the next column ie K, L M etc
When I made a few adjustments of the line
s = "=MIN(SUMIF($I$1:$I$LR,I3,$L$1:$L$LR)-SUMIF(I$1:I2,I4,O$1:O2),K3)" to s = "=MIN(SUMIF($D$1:$D$LR,D3,$F$1:$F$LR)-SUMIF(D$1:D2,D4,I$1:I2),E3)"
The following formula resulted on running the code in excel column K
=MIN(SUMIF($I$1:$I$24,I3,$L$1:$L$24)-SUMIF(I$1:I2,I4,O$1:O2),K3)

I expected to see the result similar to the fomula =MIN(SUMIF(D:D,D3,F:F)-SUMIF(D$1:D2,D3,I$1:I2),E3) when inserted in column I named FIFO Qty Out in the Table below.



<colgroup><col style="mso-width-source:userset;mso-width-alt:4681;width:96pt" width="128"> <col style="width:48pt" span="10" width="64"> </colgroup><tbody>
[TD="class: xl65, width: 128"] Date
[/TD]
[TD="class: xl66, width: 64"] Request_Invoice Number
[/TD]
[TD="class: xl66, width: 64"] From_To
[/TD]
[TD="class: xl66, width: 64"] Item_Name [/TD]
[TD="class: xl66, width: 64"] Qty_In [/TD]
[TD="class: xl66, width: 64"] Qty_Out [/TD]
[TD="class: xl66, width: 64"] Unit_Cost
[/TD]
[TD="class: xl66, width: 64"] Value_Received
[/TD]
[TD="class: xl66, width: 64"] FIFOQtyOut
[/TD]
[TD="class: xl66, width: 64"] Balance InStore [/TD]
[TD="class: xl66, width: 64"] ValueOut [/TD]

[TD="class: xl71, width: 128, align: right"]01/09/2018[/TD]
[TD="class: xl72, width: 64, align: right"]7889[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"]Books[/TD]
[TD="class: xl73, width: 64"]10[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"]
[/TD]
[TD="class: xl73, width: 64"]10
[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]02/09/2018[/TD]
[TD="class: xl72, width: 64"]H788727[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"]Pens[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]02/09/2018[/TD]
[TD="class: xl72, width: 64"]H83737[/TD]
[TD="class: xl72, width: 64"]abc Ltd[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl73, width: 64"]400[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl73, width: 64"]315[/TD]
[TD="class: xl67, align: right"]85[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]09/02/2018[/TD]
[TD="class: xl73, width: 64"]76662[/TD]
[TD="class: xl72, width: 64"]XYZ Ltd[/TD]
[TD="class: xl72, width: 64"]Books[/TD]
[TD="class: xl73, width: 64"]100[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]100[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]09/02/2018[/TD]
[TD="class: xl73, width: 64"]76662[/TD]
[TD="class: xl72, width: 64"]Abc Ltd[/TD]
[TD="class: xl72, width: 64"]Ruled Papers[/TD]
[TD="class: xl73, width: 64"]100[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]30[/TD]
[TD="class: xl73, width: 64"]3000[/TD]
[TD="class: xl73, width: 64"]15[/TD]
[TD="class: xl67, align: right"]85[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]09/04/2018[/TD]
[TD="class: xl72, width: 64"]gyuu766[/TD]
[TD="class: xl72, width: 64"]Admin[/TD]
[TD="class: xl72, width: 64"]Books[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]90[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]0
[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]09/04/2018
[/TD]
[TD="class: xl72, width: 64"]gyuu766[/TD]
[TD="class: xl72, width: 64"]Sales[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]90[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl72, width: 128"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"]pens[/TD]
[TD="class: xl73, width: 64"]100[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl73, width: 64"]4[/TD]
[TD="class: xl67, align: right"]96[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl71, width: 128, align: right"]09/09/2018[/TD]
[TD="class: xl73, width: 64"]3029[/TD]
[TD="class: xl72, width: 64"]abc Ltd[/TD]
[TD="class: xl72, width: 64"]Chalk[/TD]
[TD="class: xl73, width: 64"]200[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl72, width: 64"] [/TD]
[TD="class: xl73, width: 64"]30[/TD]
[TD="class: xl67, align: right"]170[/TD]
[TD="class: xl67, align: right"]0
[/TD]

[TD="class: xl71, width: 128, align: right"]08/10/2018[/TD]
[TD="class: xl73, width: 64"]4399[/TD]
[TD="class: xl72, width: 64"]Dpt QW[/TD]
[TD="class: xl72, width: 64"]Books[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]4[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl69"]09/09/2018[/TD]
[TD="class: xl67, align: right"]3029[/TD]
[TD="class: xl67"]dpt ed[/TD]
[TD="class: xl67"]Paracetamol 500mg Tabs[/TD]
[TD="class: xl74, align: right"]200[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl70"] [/TD]
[TD="class: xl67"] [/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]200[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl69"]08/10/2018[/TD]
[TD="class: xl67, align: right"]4399[/TD]
[TD="class: xl67"]sales[/TD]
[TD="class: xl67"]pens[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]4[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl69"]09/03/2018[/TD]
[TD="class: xl67"]32443r[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]40[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]31/08/2018[/TD]
[TD="class: xl67"]fdg4521[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]20[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]30/08/2018[/TD]
[TD="class: xl67, align: right"]46732[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]50[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]25/08/2018[/TD]
[TD="class: xl67"]2443w[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl74, align: right"]0[/TD]
[TD="class: xl67, align: right"]20[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]23/08/2018[/TD]
[TD="class: xl67"]F6557[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]10[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]24/08/2018[/TD]
[TD="class: xl67"]wtt56788[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl72, width: 64"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]25[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]29/08/2018[/TD]
[TD="class: xl67, align: right"]7766712[/TD]
[TD="class: xl67"]XYZ Ltd[/TD]
[TD="class: xl67"]Books[/TD]
[TD="class: xl67, align: right"]1000[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]50[/TD]
[TD="class: xl67, align: right"]50000[/TD]
[TD="class: xl73, width: 64"]154[/TD]
[TD="class: xl67, align: right"]846[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]16/08/2018[/TD]
[TD="class: xl67"]88y77h[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl67"]Books[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]150[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]15/08/2018[/TD]
[TD="class: xl67"]783h[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl67"]Books[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]20[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]17/08/2018[/TD]
[TD="class: xl67, align: right"]34[/TD]
[TD="class: xl67"]Sales[/TD]
[TD="class: xl67"]Pencils[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]60[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]25/08/2018[/TD]
[TD="class: xl67, align: right"]43321[/TD]
[TD="class: xl67"]XYZ Ltd[/TD]
[TD="class: xl67"]Ruled Papers[/TD]
[TD="class: xl67, align: right"]890[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]50[/TD]
[TD="class: xl67, align: right"]44500[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]890[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]14/08/2018[/TD]
[TD="class: xl67"]245ew[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl67"]Chalk[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]30[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]

[TD="class: xl68"]20/08/2018[/TD]
[TD="class: xl67, align: right"]4533[/TD]
[TD="class: xl67"]Admin[/TD]
[TD="class: xl67"]Ruled Papers[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]15[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl73, width: 64"]0[/TD]
[TD="class: xl67, align: right"]0[/TD]
[TD="class: xl67"] [/TD]

</tbody>

I Appreciate every effort to help me.

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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