Consulting

Results 1 to 14 of 14

Thread: how to optimise sumproduct function

  1. #1

    how to optimise sumproduct function

    Hi:

    I have a workbook to consolidate expenses incurred for the month. Each type of the expenses is given a code so that I could use the Sumproduct function to sum up the total of that particular category of expenses for the month (example enclosed). However, the Sumproduct function becomes very slow to recalc when there are large number of worksheets involved. Is there any other way to achieve the same results faster?

    The expenses listing worksheets are actually prepared in separate workbooks and are then copy and paste into the said consolidated workbook. Is there a way to automate this manual process for the example enclosed?

    Thanks for your help in advance.

  2. #2
    VBAX Contributor
    Joined
    Nov 2007
    Posts
    144
    Location
    I may have misunderstood your question...

    but i can not find where you have used the SUMPRODUCT function

  3. #3
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    i see no way to optimize these sumproducts, as for the automating the cut and pasting, yes that can be done, but we will need more information. such as the name of the other workbooks. are they all stored in the same place? is it the same place as this file? and how many are there.

  4. #4
    The information is copy and paste from corresponding month worksheets stored in 2 files e.g aab.xls and bbc.xls (enclosed). all these files are stored in the same directory. For example, in Jun, the "Jun" worksheets in aab.xls and bbc.xls are copy and paste in "aab" and "bbc" worksheet in the consolidated workbook. For Jul, the same process repeats. Any help would be appreciated.

  5. #5
    could someone pls help to improve the codes?
    thanks.

    [vba]Sub Macro1()
    '
    Call cp("c:\temp\aab.xls", "aab", "Jan")
    Call cp("c:\temp\bbc.xls", "bbc", "Jan")

    End Sub

    Sub cp(ByVal InputFile As String, ByVal OutputSheet As String, ByVal InputSheet As String)

    Dim wb As Workbook
    Application.ScreenUpdating = False
    Set wb = Workbooks.Open(InputFile, True, True)
    Sheets(InputSheet).Range("A1:AZ300").Copy
    wb.Close False
    Set wb = Nothing


    Sheets(OutputSheet).Activate
    Sheets(OutputSheet).Range("A1:AZ300").Select
    Sheets(OutputSheet).Paste
    Application.ScreenUpdating = True

    End Sub
    [/vba]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Improve in what way, what do you see as wrong with them?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    I'm a newbie. I may be wrong. These codes are "assembled" from various sources. It seems to be a bit too lengthy for the copy and paste purpose between workbooks. It also does not warn if the other workbooks are already opened.

    regards,

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Okay, try this then

    [vba]

    Sub Macro1()
    '
    Call cp("c:\temp\aab.xls", "aab", "Jan")
    Call cp("c:\temp\bbc.xls", "bbc", "Jan")

    End Sub

    Sub cp(ByVal InputFile As String, ByVal OutputSheet As String, ByVal InputSheet As String)
    Dim This As Workbook
    Dim wb As Workbook
    Dim aryPath As Variant

    Application.ScreenUpdating = False

    On Error Resume Next
    aryPath = Split(InputFile, "\")
    Set wb = Workbooks(aryPath(UBound(aryPath)))
    On Error GoTo 0
    If wb Is Nothing Then

    Set wb = Workbooks.Open(InputFile, True, True)
    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    wb.Close False
    Else

    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    End If
    Set wb = Nothing

    This.Sheets(OutputSheet).Range ("A1")

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Thanks for your swift reply.

    However, the macro has a runtime error 9 at
    This.Sheets(OutputSheet).Range ("A1")

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I missed a bit, but it still doesn't work as the aab.xls doesn't have an aab worksheet.

    [vba]

    Sub Macro1()
    '
    Call cp("c:\temp\aab.xls", "aab", "Jan")
    Call cp("c:\temp\bbc.xls", "bbc", "Jan")

    End Sub

    Sub cp(ByVal InputFile As String, ByVal OutputSheet As String, ByVal InputSheet As String)
    Dim This As Workbook
    Dim wb As Workbook
    Dim aryPath As Variant

    Application.ScreenUpdating = False

    Set This = ActiveWorkbook
    On Error Resume Next
    aryPath = Split(InputFile, "\")
    Set wb = Workbooks(aryPath(UBound(aryPath)))
    On Error GoTo 0
    If wb Is Nothing Then

    Set wb = Workbooks.Open(InputFile, True, True)
    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    wb.Close False
    Else

    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    End If
    Set wb = Nothing

    This.Sheets(OutputSheet).Range ("A1")

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    It should copy from the input sheet "Jan" from aab.xls to output sheet "aab" in the template and from "Jan" sheet in bbc.xls to "bbc" sheet in the template.

    by the way, what is the function of these codes?
    [VBA]
    On Error Resume Next
    aryPath = Split(InputFile, "\")
    Set wb = Workbooks(aryPath(UBound(aryPath)))
    On Error Goto 0

    [/VBA]
    Last edited by WINFS; 07-19-2008 at 06:19 PM.

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,068
    Location
    As I read your request WINFS, you require your code to copy the individual monthly worksheets from the workbooks aab.xls & bbc.xls to their respective sheets in the Consolidated workbook, right?

    XLD has rightly pointed out that you don't have any sheets in either of the aab.xls & bbc.xls workbooks named as "aab"

    What happens if you change the code to this

    [VBA]
    Sub Macro1()
    '
    Call cp("c:\temp\aab.xls", "Jan")
    Call cp("c:\temp\bbc.xls", "Jan")

    End Sub
    [/VBA]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    I may be not clear on one point.

    It need not to copy all the worksheets in aab.xls and bbc.xls to the consolidated workbook.

    It should only copy from the input sheet "Jan" (or any other month specified) from aab.xls to output sheet "aab" in the consolidated workbook and from "Jan" sheet in bbc.xls to "bbc" sheet in the consolidated workbook. The consolidated workbook already has "aab" and "bbc" worksheet.

    regrads,

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Maybe thi is what you need, guessing I am afraid

    [vba]

    Sub Macro1()
    '
    Call cp("c:\temp\aab.xls", "aab", "Jan")
    Call cp("c:\temp\bbc.xls", "bbc", "Jan")

    End Sub

    Sub cp(ByVal InputFile As String, ByVal InputSheet As String, ByVal OutputSheet As String)
    Dim This As Workbook
    Dim wb As Workbook
    Dim aryPath As Variant

    Application.ScreenUpdating = False

    Set This = ActiveWorkbook
    On Error Resume Next
    aryPath = Split(InputFile, "\")
    Set wb = Workbooks(aryPath(UBound(aryPath)))
    On Error Goto 0
    If wb Is Nothing Then

    Set wb = Workbooks.Open(InputFile, True, True)
    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    wb.Close False
    Else

    wb.Sheets(InputSheet).Range("A1:AZ300").Copy
    End If
    Set wb = Nothing

    This.Sheets(OutputSheet).Range ("A1") .Paste

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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