+ Reply to Thread
Results 1 to 16 of 16

VBA sum problem

  1. #1
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    VBA sum problem

    As attachment shows, the result should be 321.3, however the result of VBA code is 310, why the diff exsits? Many thanks for help.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: VBA sum problem

    Hi,
    Didnt download your sample, but first of all check type of your variable that contains your sum. Looks like you collect result to Integer

  3. #3
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Quote Originally Posted by kasan View Post
    Hi,
    Didnt download your sample, but first of all check type of your variable that contains your sum. Looks like you collect result to Integer
    Thanks, can you pls help to check the code below?

    PHP Code: 
    Dim cnn As ObjectSQL$, s$, p$, f$
    Set cnn CreateObject("ADODB.Connection")
    ThisWorkbook.Path "\"
    f = Dir(p & "
    *.CSV")
    cnn.Open "
    Provider=Microsoft.ace.OLEDB.16.0;Extended Properties='text;FMT=Delimited(,);hdr=YES';Data Source=" & p
    Do While f <> ""
       SQL = SQL & " 
    SELECT INT(Date) AS TAmount FROM [" & f & "UNION ALL "
        f = Dir()
    Loop
    SQL = Left(SQL, Len(SQL) - 11)
    SQL = "
    SELECT T,SUM(AmountFROM (" & SQL & "GROUP BY T"
    [A2:E1048576] = ""
    [a2].CopyFromRecordset cnn.Execute(SQL)
    cnn.Close
    Set cnn = Nothing
    Dim R%
    R = [A1048576].End(3).Row
    [a1].Offset(R, 0) = "
    Sum"
    [b1].Offset(R, 0).Resize(1, 4) = "
    =sum(b2:b" & R & ")
    Last edited by czl103; 09-26-2018 at 11:29 PM.

  4. #4
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Waitfor for help...

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,541

    Re: VBA sum problem

    While waiting for help, read the Forum Rules (at the top). You should be interested in #2.

  6. #6
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Thanks, updated... Can you pls help to check?many thanks...
    PHP Code: 
    Dim cnn As ObjectSQL$, s$, p$, f$
    Set cnn CreateObject("ADODB.Connection")
    ThisWorkbook.Path "\"
    f = Dir(p & "
    *.CSV")
    cnn.Open "
    Provider=Microsoft.ace.OLEDB.16.0;Extended Properties='text;FMT=Delimited(,);hdr=YES';Data Source=" & p
    Do While f <> ""
       SQL = SQL & " 
    SELECT INT(Date) AS TAmount FROM [" & f & "UNION ALL "
        f = Dir()
    Loop
    SQL = Left(SQL, Len(SQL) - 11)
    SQL = "
    SELECT T,SUM(AmountFROM (" & SQL & "GROUP BY T"
    [A2:E1048576] = ""
    [a2].CopyFromRecordset cnn.Execute(SQL)
    cnn.Close
    Set cnn = Nothing
    Dim R%
    R = [A1048576].End(3).Row
    [a1].Offset(R, 0) = "
    Sum"
    [b1].Offset(R, 0).Resize(1, 4) = "
    =sum(b2:b" & R & ")
    Last edited by czl103; 09-26-2018 at 11:41 PM.

  7. #7
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Pls help, masters...

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA sum problem

    Different method
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    thanks, it does work...
    If there are more item to caculate other than "amount", how to add another item? thanks in advance...

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA sum problem

    OK,
    If it works, goes to next step to make it flexible.

    I'm out at the moment, so I'll post the code when I get back.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA sum problem

    Try
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Thanks a lot.... I will try later...

  13. #13
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Quote Originally Posted by jindon View Post
    Try
    Please Login or Register  to view this content.
    Tried, but this doesn't work. Error:
    "run error "424" object required"
    Pls help to check why this happen, thanks...

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA sum problem

    1) Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.

    2) You should not delete 2 rows on the top of the module.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    It does work, thanks a lot...

  16. #16
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: VBA sum problem

    Million thanks for your kindly help. Here is one question, for large amount of csv files, the running time for this code is much longer than the old one, Is there any way to shorten the running time?

+ 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: 6
    Last Post: 07-01-2018, 01:58 PM
  2. [SOLVED] Excel Macro - Copy & Paste (Font) problem & Delete Last Added Rows problem
    By LennartB in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2015, 06:58 AM
  3. Update Values problem and a Value Selection Problem----Please Help.
    By TrivialT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2013, 02:43 PM
  4. Replies: 6
    Last Post: 10-20-2013, 07:16 PM
  5. input box problem, cell filtering problem, result display while locking sheet
    By croozin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2013, 08:52 AM
  6. Replies: 6
    Last Post: 05-28-2013, 05:08 PM
  7. Replies: 2
    Last Post: 01-22-2013, 07:09 AM

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