+ Reply to Thread
Results 1 to 2 of 2

Paste from one to many

Hybrid View

  1. #1
    Registered User
    Join Date
    02-25-2009
    Location
    bfn,rsa
    MS-Off Ver
    Excel 2003
    Posts
    17

    Paste from one to many

    Dear Sir/Madam

    I need some help.

    I’ve got a folder on my C: drive named Test. This folder contains a sub folder “TestSubFolder” and a workbook “Primary Workbook.xls”. “TestSubFolder” contains all my customer’s individual accounts. The total customer accounts are unspecified.

    Every day I enter customer payments into “Primary workbook.xls”. At the end of the day I will have a list of all customer payments received.

    I want a macro that when a button is pressed, updates all the individual customer accounts with the payments made and then clear the list in “Primary Workbook” for the same procedure the next day.

    It sounds tricky. I hope someone can help me with this.
    Find attached a sample of the “Primary Workbook.xls” and a few customer account samples.

    Thank you very much for all the help.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Paste from one to many

    See attached file where I added below macro.

    Macro will automatically insert formula in column 'E' of customer sheets. Initially you need only to put first theree rows (titles and row with Opening Balance).

    Private Sub CommandButton1_Click()
       Dim myPath As String
       Dim r As Long, lastRow As Long
       Dim custId As String, elem As Variant
       Dim myRow As Variant
       Dim destRow As Long, nr As Integer
       
       Dim otherWb As Workbook
       Dim otherSh As Worksheet
       Dim sh1 As Worksheet
       Dim myDic As Object
       
       myPath = "C:\test\TestSubFolder\"
       
       Set sh1 = ThisWorkbook.Sheets(1)
       Set myDic = CreateObject("scripting.dictionary")
       
       lastRow = sh1.Range("a2").CurrentRegion.Rows.Count + sh1.Range("a2") _
                .CurrentRegion.Row - 1
       For r = 2 To lastRow
          custId = sh1.Cells(r, "b")
          If Not myDic.exists(custId) Then
             myDic.Add Item:=r, key:=custId
          Else
             myDic(custId) = myDic(custId) & "," & r
          End If
       Next r
       
       'store rows for each customer so macro opens only one time customer workbook
       For Each elem In myDic.keys
          'open customer workbook
          Set otherWb = Workbooks.Open(myPath & elem & ".xls")
          Set otherSh = otherWb.ActiveSheet
          
          lastRow = otherSh.Cells(Rows.Count, "a").End(xlUp).Row
          destRow = lastRow
          nr = UBound(Split(myDic(elem), ",")) + 1
          
          'macro copies data for each row customer from primary wb to customer sheet
          For Each myRow In Split(myDic(elem), ",")
             destRow = destRow + 1
             otherSh.Cells(destRow, "a") = sh1.Cells(myRow, "a")
             otherSh.Cells(destRow, "b") = sh1.Cells(myRow, "c")
             otherSh.Cells(destRow, "d") = sh1.Cells(myRow, "d")
          Next myRow
          
          'apply formula on 'E' column
          otherSh.Cells(lastRow + 1, "e").Resize(nr, 1).Formula = "=e" & lastRow _
                      & "+c" & lastRow + 1 & "-d" & lastRow + 1
          
          otherSh.Range("a:a").NumberFormat = "d-mmm-yy"
          
          'close and save customer workbook.Close True
       Next elem
       
       'clean data for next day
       sh1.Range("a2:d2").Resize(sh1.Range("a2").CurrentRegion.Rows.Count).ClearContents
    End Sub
    Regards,
    Antonio
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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