+ Reply to Thread
Results 1 to 4 of 4

Transaction to Inventory excel VBA using macro

Hybrid View

mohi021 Transaction to Inventory... 01-17-2021, 05:33 AM
Trebor76 Re: Transaction to Inventory... 01-17-2021, 08:26 AM
mohi021 Re: Transaction to Inventory... 01-17-2021, 10:10 AM
Trebor76 Re: Transaction to Inventory... 01-17-2021, 06:21 PM
  1. #1
    Registered User
    Join Date
    01-17-2021
    Location
    Tehran
    MS-Off Ver
    2019
    Posts
    2

    Lightbulb Transaction to Inventory excel VBA using macro

    ​Hello guys!

    I want to write a macro to receive data from the 'Transaction' sheet and gives me an 'Inventory' sheet but I don't know how to do it

    (the 'Transaction' sheet may contain many shares so the macro should count them all and if we sell a share that we don't have in the first place,(short it) it should warn us)

    could you please help me??
    Attached Files Attached Files
    Last edited by mohi021; 01-17-2021 at 04:23 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Transaction to Inventory excel VBA using macro

    Hi mohi021,

    Welcome to the forum.

    I'm sure about "if we sell a share that we don't have in the first place,(short it) it should warn us" but this will do everything else:

    Option Explicit
    Sub Macro1()
    
        Dim lngLastRow As Long, lngMyRow As Long
        Dim wsTrans As Worksheet, wsInventory As Worksheet
        Dim clnTickers As New Collection
        Dim varTicker As Variant
        
        Application.ScreenUpdating = False
        
        Set wsTrans = ThisWorkbook.Sheets("Transaction")
        lngLastRow = wsTrans.Cells(Rows.Count, "A").End(xlUp).Row
        
        'Create unique list of ticker codes
        On Error Resume Next
            wsTrans.ShowAllData
            For lngMyRow = 2 To lngLastRow
                clnTickers.Add wsTrans.Range("A" & lngMyRow), CStr(wsTrans.Range("A" & lngMyRow))
            Next lngMyRow
        On Error GoTo 0
        
        'Clear existing contents and output unique ticker codes with associated formulas
        Set wsInventory = ThisWorkbook.Sheets("Inventory")
        lngLastRow = wsInventory.Range("A:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        wsInventory.Range("A2:I" & lngLastRow).ClearContents
        lngMyRow = 2
        For Each varTicker In clnTickers
            wsInventory.Range("A" & lngMyRow).Value = varTicker
            wsInventory.Range("B" & lngMyRow).Formula = "=IFERROR(SUMIFS('" & wsTrans.Name & "'!C:C,'" & wsTrans.Name & "'!B:B,""Buy"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("C" & lngMyRow).Formula = "=IFERROR(B" & lngMyRow & "*D" & lngMyRow & ",0)"
            wsInventory.Range("D" & lngMyRow).Formula = "=IFERROR(AVERAGEIFS('" & wsTrans.Name & "'!D:D,'" & wsTrans.Name & "'!B:B,""Buy"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("E" & lngMyRow).Formula = "=IFERROR(SUMIFS('" & wsTrans.Name & "'!C:C,'" & wsTrans.Name & "'!B:B,""Sell"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("F" & lngMyRow).Formula = "=IFERROR(E" & lngMyRow & "*G" & lngMyRow & ",0)"
            wsInventory.Range("G" & lngMyRow).Formula = "=IFERROR(AVERAGEIFS('" & wsTrans.Name & "'!D:D,'" & wsTrans.Name & "'!B:B,""Sell"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("H" & lngMyRow).Formula = "=IFERROR(B" & lngMyRow & "-E" & lngMyRow & ",0)"
            wsInventory.Range("I" & lngMyRow).Formula = "=IFERROR(E" & lngMyRow & "*(G" & lngMyRow & "-D" & lngMyRow & "),0)"
            lngMyRow = lngMyRow + 1
        Next varTicker
        
        'Total Cost formula
        lngLastRow = wsInventory.Cells(Rows.Count, "C").End(xlUp).Row
        wsInventory.Range("C" & lngLastRow + 2).Formula = "=IFERROR(SUM(C2:C" & lngLastRow & "),0)"
        
        Application.ScreenUpdating = True
        
        MsgBox "Inventory data has now been prepared.", vbInformation
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    01-17-2021
    Location
    Tehran
    MS-Off Ver
    2019
    Posts
    2

    Re: Transaction to Inventory excel VBA using macro

    Thanks a lot, man! you are a true savior!! <3
    I have a few questions if you bear me,

    1)when I delete the data on the 'Inventory' sheet(just Second row onwards) and for example add a new set of data for 'Transaction's
    it will delete the first row on the 'Inventory' sheet, and when I want to set another batch of data for 'Transaction's and run the Macro
    It says 'Object variable or with block variable not set'...so the macro deletes the Header Row for the 'inventory' sheet. I want the first row
    on the 'inventory' sheet to remain.
    .
    2)our language is Persian (Arabic letters) so, if I change the cells' data into Persian, would it destroy the code?
    .
    Thank you very much

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Transaction to Inventory excel VBA using macro

    Hi mohi021,

    you are a true savior!!
    Hardly. Just another Excel user

    1 - The code clears and reimports the data to the Inventory tab so you don't have to do that manually. The revised code does however only delete from Row 2 onwards.
    2 - No, it shouldn't matter. You will have to test that yourself though.

    Thanks,

    Robert

    Option Explicit
    Sub Macro1()
    
        Dim lngLastRow As Long, lngMyRow As Long
        Dim wsTrans As Worksheet, wsInventory As Worksheet
        Dim clnTickers As New Collection
        Dim varTicker As Variant
        
        Application.ScreenUpdating = False
        
        Set wsTrans = ThisWorkbook.Sheets("Transaction")
        lngLastRow = wsTrans.Cells(Rows.Count, "A").End(xlUp).Row
        
        'Create unique list of ticker codes
        On Error Resume Next
            wsTrans.ShowAllData
            For lngMyRow = 2 To lngLastRow
                clnTickers.Add wsTrans.Range("A" & lngMyRow), CStr(wsTrans.Range("A" & lngMyRow))
            Next lngMyRow
        On Error GoTo 0
        
        'Clear existing contents (only if there's data from Row 2 onwards) and output unique ticker codes with associated formulas
        Set wsInventory = ThisWorkbook.Sheets("Inventory")
        On Error Resume Next
            lngLastRow = wsInventory.Range("A:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If lngLastRow >= 2 Then
                wsInventory.Range("A2:I" & lngLastRow).ClearContents
            End If
        On Error GoTo 0
        lngMyRow = 2
        For Each varTicker In clnTickers
            wsInventory.Range("A" & lngMyRow).Value = varTicker
            wsInventory.Range("B" & lngMyRow).Formula = "=IFERROR(SUMIFS('" & wsTrans.Name & "'!C:C,'" & wsTrans.Name & "'!B:B,""Buy"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("C" & lngMyRow).Formula = "=IFERROR(B" & lngMyRow & "*D" & lngMyRow & ",0)"
            wsInventory.Range("D" & lngMyRow).Formula = "=IFERROR(AVERAGEIFS('" & wsTrans.Name & "'!D:D,'" & wsTrans.Name & "'!B:B,""Buy"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("E" & lngMyRow).Formula = "=IFERROR(SUMIFS('" & wsTrans.Name & "'!C:C,'" & wsTrans.Name & "'!B:B,""Sell"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("F" & lngMyRow).Formula = "=IFERROR(E" & lngMyRow & "*G" & lngMyRow & ",0)"
            wsInventory.Range("G" & lngMyRow).Formula = "=IFERROR(AVERAGEIFS('" & wsTrans.Name & "'!D:D,'" & wsTrans.Name & "'!B:B,""Sell"",'" & wsTrans.Name & "'!A:A,A" & lngMyRow & "),0)"
            wsInventory.Range("H" & lngMyRow).Formula = "=IFERROR(B" & lngMyRow & "-E" & lngMyRow & ",0)"
            wsInventory.Range("I" & lngMyRow).Formula = "=IFERROR(E" & lngMyRow & "*(G" & lngMyRow & "-D" & lngMyRow & "),0)"
            lngMyRow = lngMyRow + 1
        Next varTicker
        
        'Total Cost formula
        lngLastRow = wsInventory.Cells(Rows.Count, "C").End(xlUp).Row
        wsInventory.Range("C" & lngLastRow + 2).Formula = "=IFERROR(SUM(C2:C" & lngLastRow & "),0)"
        
        Application.ScreenUpdating = True
        
        MsgBox "Inventory data has now been prepared.", vbInformation
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-05-2020, 12:08 PM
  2. [SOLVED] Inventory management macro subtracting too much inventory
    By Keegan1116 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-13-2020, 02:57 PM
  3. [SOLVED] Macro for finding Last day of transaction
    By srinivasan1965 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-10-2018, 02:14 AM
  4. [SOLVED] macro to record transaction
    By chubbychub in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-16-2016, 04:00 PM
  5. Complicated set of calculations based on transaction IDs, transaction value, etc.
    By BeeZeRCoX in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2011, 11:35 AM
  6. Lastest transaction date for each inventory item
    By seeya in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2011, 05:56 AM
  7. Excel Inventory Macro
    By junkscratch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2007, 05:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1