Inventory transfer through VBA

Zubair

Active Member
Joined
Jul 4, 2009
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
Mr. Excel.xlsx
ABCDEFGHIJ
1Inventory Data Form
2TransactionInventory Transfer
3Document No.5001
4Date22/02/2022
5
6Location FromFactory 1
7Location ToFactory 2
8
9BrandSizeQuantityRateAmount
10ABC2125601,500
11CDE291255660
12
13
14Data base
15TransactionDocument No.DateLocation FromLocation ToBrandSizeQuantityRateAmount
16Opening stockFactory 1ABC21100606,000
17Opening stockFactory 1CDE2950552,750
18
19Inventory Transfer500122/02/2022Factory 1ABC21-2560(1,500)
20Inventory Transfer500122/02/2022Factory 2ABC2125601,500
21Inventory Transfer500122/02/2022Factory 1CDE29-1255(660)
22Inventory Transfer500122/02/2022Factory 2CDE291255660
23
24Closing stockFactory 1ABC2175604,500
25Closing stockFactory 2ABC2125601,500
26Closing stockFactory 1CDE2938552,090
27Closing stockFactory 2CDE291255660
Inventory transfer
Cell Formulas
RangeFormula
J24:J27,J19:J22,J16:J17,E10:E11E10=+C10*D10
D19D19=+B6
E20E20=+B7
D21D21=+B6
E22E22=+B7
H24H24=+H16+H19
H25,H27H25=+H20
H26H26=+H17+H21



Hi,

I need VBA to record transfer stock from Factory 1 to Factory 2 in tab "database" through Inventory Data Form.
please help
 
Hi Zubair, I was a bit busier than expected so some delay...

Read through the comments and set the correct sheet names and starting cells where the comments start with <<<<

VBA Code:
Option Explicit

Sub TransferStock()
'Macro to show stock transfers in database
    Dim vIn As Variant, vOut As Variant, vDet As Variant
    Dim lRo As Long, lCo As Long, lRi As Long, lUBi As Long, lUBo As Long
    Dim rTransf As Range, rDB As Range
    
    Set rTransf = Sheets("Sheet1").Range("A9")      '<<<< Left  cell of heading row (Brand)
    vDet = Sheets("Sheet1").Range("A2:B7").Value    '<<<< address of Transaction table
    Set rDB = Sheets("Database").Range("A1")        '<<<< top Left  cell of database heading row
    
    'read data into arrays for fast processing
    vIn = rTransf.CurrentRegion.Value
    lUBi = UBound(vIn, 1)
    
    'set size of output array to correct number of rows (twice input)
    ReDim vOut(1 To 2 * (lUBi - 1), 1 To 10)
    
    lUBo = UBound(vOut, 1)
    lRi = 1     ' to account for header row
    ' Because each line of input  creates two lines of output, we use 'Step 2' in the loop
    For lRo = 1 To lUBo Step 2
        lRi = lRi + 1
        ' Decide what to do at each column for the two output rows
        For lCo = 1 To 10
            Select Case lCo
                Case 1, 2, 3
                    vOut(lRo, lCo) = vDet(lCo, 2)
                    vOut(lRo + 1, lCo) = vDet(lCo, 2)
                Case 4
                    vOut(lRo, lCo) = vDet(lCo + 1, 2)
                Case 5
                    vOut(lRo + 1, lCo) = vDet(lCo + 1, 2)
                Case 6, 7, 9
                    vOut(lRo, lCo) = vIn(lRi, lCo - 5)
                    vOut(lRo + 1, lCo) = vOut(lRo, lCo)
                Case 8, 10
                    vOut(lRo, lCo) = -vIn(lRi, lCo - 5)
                    vOut(lRo + 1, lCo) = -vOut(lRo, lCo)
            End Select
        Next lCo
    Next lRo
    
    ' Now dump the output array below the existing database
    rDB.Offset(rDB.CurrentRegion.Rows.Count, 0).Resize(lUBo, 10) = vOut
End Sub
 
Upvote 0
Solution

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Zubair, I was a bit busier than expected so some delay...

Read through the comments and set the correct sheet names and starting cells where the comments start with <<<<

VBA Code:
Option Explicit

Sub TransferStock()
'Macro to show stock transfers in database
    Dim vIn As Variant, vOut As Variant, vDet As Variant
    Dim lRo As Long, lCo As Long, lRi As Long, lUBi As Long, lUBo As Long
    Dim rTransf As Range, rDB As Range
   
    Set rTransf = Sheets("Sheet1").Range("A9")      '<<<< Left  cell of heading row (Brand)
    vDet = Sheets("Sheet1").Range("A2:B7").Value    '<<<< address of Transaction table
    Set rDB = Sheets("Database").Range("A1")        '<<<< top Left  cell of database heading row
   
    'read data into arrays for fast processing
    vIn = rTransf.CurrentRegion.Value
    lUBi = UBound(vIn, 1)
   
    'set size of output array to correct number of rows (twice input)
    ReDim vOut(1 To 2 * (lUBi - 1), 1 To 10)
   
    lUBo = UBound(vOut, 1)
    lRi = 1     ' to account for header row
    ' Because each line of input  creates two lines of output, we use 'Step 2' in the loop
    For lRo = 1 To lUBo Step 2
        lRi = lRi + 1
        ' Decide what to do at each column for the two output rows
        For lCo = 1 To 10
            Select Case lCo
                Case 1, 2, 3
                    vOut(lRo, lCo) = vDet(lCo, 2)
                    vOut(lRo + 1, lCo) = vDet(lCo, 2)
                Case 4
                    vOut(lRo, lCo) = vDet(lCo + 1, 2)
                Case 5
                    vOut(lRo + 1, lCo) = vDet(lCo + 1, 2)
                Case 6, 7, 9
                    vOut(lRo, lCo) = vIn(lRi, lCo - 5)
                    vOut(lRo + 1, lCo) = vOut(lRo, lCo)
                Case 8, 10
                    vOut(lRo, lCo) = -vIn(lRi, lCo - 5)
                    vOut(lRo + 1, lCo) = -vOut(lRo, lCo)
            End Select
        Next lCo
    Next lRo
   
    ' Now dump the output array below the existing database
    rDB.Offset(rDB.CurrentRegion.Rows.Count, 0).Resize(lUBo, 10) = vOut
End Sub
Marvelous! Many thanks, sijpie. perfectly working as required.

Can I request you to see the following thread pending an answer long?

 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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