Data movement in 2 sheets

Zubair

Active Member
Joined
Jul 4, 2009
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
Hi,

How can I move Purchase data in 2 sheets i.e. Datawarehouse and AP ?



Purchase entry tab
Mr. Excel.xlsx
ABCDE
1Document No.20003
2Date4/3/2022
3Supplier No.ProductQtyRateAmount
458F42100858,500
558F90105818,505
658F991258911,125
7
8
9
10Subtotal28,130
11Cartage500
12Grand Total28,630
Purchase
Cell Formulas
RangeFormula
E4:E6E4=+C4*D4
E10E10=SUM(E4:E9)
E12E12=+E10+E11



Data warehouse tab - All information
Mr. Excel.xlsx
ABCDEFGHI
1Document No.DateSupplier No.ProductQtyRate Amount Cartage Grand Total
2200014/2/202221F4250653,25020010,913
321F9051693,519
421F9958683,944
5200024/3/202240F433003510,5005,00044,380
640F503703814,060
740F683803914,820
8200034/3/202258F42100858,50050028,630
958F90105818,505
1058F991258911,125
Data warehouse
Cell Formulas
RangeFormula
G5:G7G5=+E5*F5


Accounts Payable tab (AP) - selected information with a negative sign
Mr. Excel.xlsx
ABCDEF
1Document No.DateSupplier No.SubtotalCartageGrand Total
2200014/2/202221(10,713)(200)(10,913)
3200024/3/202240(39,380)(5,000)(44,380)
4200034/3/202258(28,130)(500)(28,630)
AP
Cell Formulas
RangeFormula
F2:F4F2=+D2+E2


after data transfer input data to clear.
 
Ah, I hadn't taken account of you using all six rows.

This new code should fix it
VBA Code:
Option Explicit

Sub PurchaseTransfer()
    Dim vIn As Variant, vWH As Variant, vAP As Variant, vCost As Variant
    Dim lRi As Long, lCi As Long, lRw As Long, lRa As Long, lItems As Long, UB As Long
    Dim rST As Range
    
    'get entry data into arrays for fast processing
    
    Set rST = Sheets("Purchase").Range("A:A").Find("Subtotal")
    vCost = rST.Offset(0, 4).Resize(3, 1)
    
    lRi = Sheets("Purchase").Range("A3").CurrentRegion.Rows.Count
    lRi = Application.WorksheetFunction.Min(lRi, rST.Row - 1)
    
    With Sheets("Purchase").Range("A1")
        vIn = .Resize(lRi, 5).Value
    End With
    UB = UBound(vIn, 1)
        
    lItems = UB - 3
    
    'resize output arrays
    ReDim vWH(1 To lItems, 1 To 9)
    ReDim vAP(1 To 1, 1 To 6)
    
    'fill out warehouse array
    vWH(1, 1) = vIn(1, 5) 'Doc No
    vWH(1, 2) = vIn(2, 5) 'Date
    vWH(1, 8) = vCost(2, 1) 'Cartage
    vWH(1, 9) = vCost(3, 1) 'Grand Tot
    
    For lRi = 4 To UB
        lRw = lRw + 1
        For lCi = 1 To 5
            vWH(lRw, lCi + 2) = vIn(lRi, lCi)
        Next lCi
    Next lRi
    
    'fill out Accounts Payable array
    vAP(1, 1) = vIn(1, 5) 'Doc No
    vAP(1, 2) = vIn(2, 5) 'Date
    vAP(1, 3) = vIn(4, 1) 'Suppl No
    vAP(1, 4) = -vCost(1, 1) 'Subtot
    vAP(1, 5) = -vCost(2, 1) 'Cart
    vAP(1, 6) = -vCost(3, 1) 'Grndtot
    
    'Write arrays to sheets
    With Sheets("Data Warehouse")
        lRw = .Range("A1").CurrentRegion.Rows.Count
        .Range("A" & lRw + 1).Resize(lItems, 9) = vWH
    End With
    With Sheets("AP")
        lRa = .Range("A1").CurrentRegion.Rows.Count
        .Range("A" & lRa + 1).Resize(1, 6) = vAP
    End With
    
    'clear input data
    With Sheets("Purchase")
        .Range("A4").Resize(lItems, 5).ClearContents
        .Range("E1:E2").ClearContents
        .Range("E11").ClearContents
    End With
End Sub
 
Upvote 0
Solution

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Ah, I hadn't taken account of you using all six rows.

This new code should fix it
VBA Code:
Option Explicit

Sub PurchaseTransfer()
    Dim vIn As Variant, vWH As Variant, vAP As Variant, vCost As Variant
    Dim lRi As Long, lCi As Long, lRw As Long, lRa As Long, lItems As Long, UB As Long
    Dim rST As Range
   
    'get entry data into arrays for fast processing
   
    Set rST = Sheets("Purchase").Range("A:A").Find("Subtotal")
    vCost = rST.Offset(0, 4).Resize(3, 1)
   
    lRi = Sheets("Purchase").Range("A3").CurrentRegion.Rows.Count
    lRi = Application.WorksheetFunction.Min(lRi, rST.Row - 1)
   
    With Sheets("Purchase").Range("A1")
        vIn = .Resize(lRi, 5).Value
    End With
    UB = UBound(vIn, 1)
       
    lItems = UB - 3
   
    'resize output arrays
    ReDim vWH(1 To lItems, 1 To 9)
    ReDim vAP(1 To 1, 1 To 6)
   
    'fill out warehouse array
    vWH(1, 1) = vIn(1, 5) 'Doc No
    vWH(1, 2) = vIn(2, 5) 'Date
    vWH(1, 8) = vCost(2, 1) 'Cartage
    vWH(1, 9) = vCost(3, 1) 'Grand Tot
   
    For lRi = 4 To UB
        lRw = lRw + 1
        For lCi = 1 To 5
            vWH(lRw, lCi + 2) = vIn(lRi, lCi)
        Next lCi
    Next lRi
   
    'fill out Accounts Payable array
    vAP(1, 1) = vIn(1, 5) 'Doc No
    vAP(1, 2) = vIn(2, 5) 'Date
    vAP(1, 3) = vIn(4, 1) 'Suppl No
    vAP(1, 4) = -vCost(1, 1) 'Subtot
    vAP(1, 5) = -vCost(2, 1) 'Cart
    vAP(1, 6) = -vCost(3, 1) 'Grndtot
   
    'Write arrays to sheets
    With Sheets("Data Warehouse")
        lRw = .Range("A1").CurrentRegion.Rows.Count
        .Range("A" & lRw + 1).Resize(lItems, 9) = vWH
    End With
    With Sheets("AP")
        lRa = .Range("A1").CurrentRegion.Rows.Count
        .Range("A" & lRa + 1).Resize(1, 6) = vAP
    End With
   
    'clear input data
    With Sheets("Purchase")
        .Range("A4").Resize(lItems, 5).ClearContents
        .Range("E1:E2").ClearContents
        .Range("E11").ClearContents
    End With
End Sub
Yes, perfect all moving accurately, many thanks.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

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