Consulting

Results 1 to 8 of 8

Thread: Sum not correct

  1. #1

    Sum not correct

    Hi

    I have delveloped some codes to copy data with certain criterias but i am not getting correct sum in mentioned below codes Kindly see my attached sheet with complete codes if you people think that it needs improvement then please mention and correct it where necessory.

    HTML Code:
     LastRow = Range("J" & Rows.Count).End(xlUp).Row
         Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
         Range("J" & LastRow + 1) = Header2
         Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
         Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
         Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
         Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
         Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P" & LastRow))
    thanks in advance

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Maybe it should be

    [vba]

    LastRow = Range("J" & Rows.Count).End(xlUp).Row
    Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
    Range("J" & LastRow + 1) = Header2
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
    Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P1:P" & LastRow))
    [/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

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Here's Ayazgreats full code from http://www.mrexcel.com/forum/showthread.php?t=323607
    [VBA]
    Sub ayazgreat()
    Dim Header
    Dim Header2
    Dim LastRow As Long
    Dim Rng As Range
    Header = Array("Region", "Item Issued", "Invoice Date", "Cheque #", "Amount", "Chq Date")
    Header2 = "Total"
    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    Sheets("Payment").Copy
    ActiveSheet.Shapes("Button 1").Delete
    ActiveSheet.Shapes("Button 2").Delete
    Rows("1:2").Delete Shift:=xlUp
    ActiveSheet.Range("C3:H3").Value = Header
    ActiveSheet.Columns("E:H").AutoFit

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Set Rng = Range("A3:H" & Range("A" & Rows.Count).End(xlUp).Row)

    Range("A" & LastRow, "H" & LastRow).Delete
    Rng.Sort Key1:=Range("C4"), Order1:=xlAscending
    Rng.AutoFilter Field:=3, Criteria1:="S-II"
    Rng.SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Sheets("Payment").Range("J3")
    Rng.AutoFilter

    LastRow = Range("J" & Rows.Count).End(xlUp).Row
    Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
    Range("J" & LastRow + 1) = Header2
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
    Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P4:P" & LastRow))



    Rng.AutoFilter Field:=3, Criteria1:="S-III"
    Rng.SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Sheets("Payment").Range("S3")
    Rng.AutoFilter

    LastRow = Range("S" & Rows.Count).End(xlUp).Row
    Range("S" & LastRow + 1, "X" & LastRow + 1).Merge
    Range("S" & LastRow + 1) = Header2
    Range("S" & LastRow + 1, "Z" & LastRow + 1).Interior.Color = 0
    Range("S" & LastRow + 1, "Z" & LastRow + 1).Font.Bold = True
    Range("S" & LastRow + 1, "Z" & LastRow + 1).Font.ColorIndex = 2
    Range("S" & LastRow + 1, "X" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("Y" & LastRow + 1).Value = WorksheetFunction.Sum(Range("Y4:Y" & LastRow))
    Range("S3:Z2000").Copy
    Cells(Rows.Count, "J").End(xlUp).Offset(3).PasteSpecial xlAll
    Columns("S:Z").Delete
    Columns("A:I").Delete

    Range("A4").Select
    .ScreenUpdating = True
    .DisplayAlerts = True
    End With
    End Sub
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    Simon please first see my mentioned blow codes and the these codes someshow are different at this point please check and compare codes your provided codes

    [vba]
    Sub TEST()
    Dim Header2
    Dim LastRow As Long
    Dim Rng As Range
    With Application
    .ScreenUpdating = False

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Set Rng = Range("A3:H" & Range("A" & Rows.Count).End(xlUp).Row)


    Rng.Sort Key1:=Range("C4"), Order1:=xlAscending

    Rng.AutoFilter Field:=3, Criteria1:="S-II"
    Rng.SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Sheets("DATA").Range("J3")
    Rng.AutoFilter

    LastRow = Range("J" & Rows.Count).End(xlUp).Row
    Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
    Range("J" & LastRow + 1) = Header2
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
    Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P4:P" & LastRow))



    Rng.AutoFilter Field:=3, Criteria1:="S-III"
    Rng.SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Sheets("DATA").Range("J" & LastRow).Offset(4, 0)
    Rng.AutoFilter


    LastRow = Range("J" & Rows.Count).End(xlUp).Row
    Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
    Range("J" & LastRow + 1) = Header2
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
    Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P" & LastRow))
    Range("A4").Select


    .ScreenUpdating = True
    .DisplayAlerts = True

    End With
    End Sub
    [/vba]

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Yes they are but posters often change their code when posting, it always helps to give the full picture!. When posting code please highlight your code and then click the green VBA button to add the code tags.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #6
    Thanks Simon for your advice and what about the correction I want in mentioned below codes with the help of my complete provided codes please?

    [VBA] LastRow = Range("J" & Rows.Count).End(xlUp).Row
    Range("J" & LastRow + 1, "O" & LastRow + 1).Merge
    Range("J" & LastRow + 1) = Header2
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Interior.Color = 0
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.Bold = True
    Range("J" & LastRow + 1, "Q" & LastRow + 1).Font.ColorIndex = 2
    Range("J" & LastRow + 1, "O" & LastRow + 1).HorizontalAlignment = xlCenter
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P" & LastRow))
    [/VBA]

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    You have had the answer form xld! your code only sums one cell:
    [VBA]
    Range("P" & LastRow + 1).Value = WorksheetFunction.Sum(Range("P" & LastRow))
    [/VBA]is the same as typing in A2 on the worksheet =A1 it will only ever show the value of A1 similarly if you entered =Sum(A1) which effectively thats what your doing then it will only ever show the value of A1
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Actually to be honest you haven't developed these codes yourself nor have you bothered to read the code you have as the answer is conatined in the full code you posted and the full code of yours that i posted from MrExcel.

    Thread Closed be being annoying!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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