Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 25th, 2005, 09:12 AM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding the highest amount

My problem :

In Coloumn D i am going to have 10 sets of numbers

12.37
14.63
2.54
4.79
19.21
15.84
25.91
2.49
0.95
1.28

These numbers added up equal 100.1. I have to right a macro that will sum up the numbers and if it is > 100 than it will minux X amount(over 100) from the first highest number? Alternatively, if it is < 100 than i will have to add x amount(under 100) to the highest number.

So far i've got the easy parts down, Summeing the number but i am really stuck in how to figure out how to scan the block of numbers for the highest amount and than add/sub the over/under. Unfortunately, The programmer who used to do these has left so it goes to the next qualified person. The Network admin :( because of course he knows programming :( /rant off

Thanks in advance for any help
Keith

 
Old May 25th, 2005, 09:53 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sub myMacro()
Dim myColDSum
Dim myColDMax
Dim myColDNewMax
    myColDSum = WorksheetFunction.Sum(Range("D:D"))
    myColDMax = WorksheetFunction.Max(Range("D:D"))
    If myColDSum <> 100 Then
        myColDNewMax = (100 - myColDSum) + myColDMax
    End If
    Range("D:D").Select
    Selection.Find(What:=myColDMax, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Activate
    ActiveCell.Value = myColDNewMax
    Range("D1").Select
End Sub

cheers

Matt
 
Old May 25th, 2005, 01:44 PM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mjppaba
 Sub myMacro()
Dim myColDSum
Dim myColDMax
Dim myColDNewMax
    myColDSum = WorksheetFunction.Sum(Range("D:D"))
    myColDMax = WorksheetFunction.Max(Range("D:D"))
    If myColDSum <> 100 Then
        myColDNewMax = (100 - myColDSum) + myColDMax
    End If
    Range("D:D").Select
    Selection.Find(What:=myColDMax, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Activate
    ActiveCell.Value = myColDNewMax
    Range("D1").Select
End Sub

Thank you very much, I'm grateful. One thing popped up though if the sum of the numbers equaled 100 it would than put a blank in the cell so all i did was move the End If a little further down in order to skip the search process as it wasn't needed.

 Sub myMacro()
Dim myColDSum
Dim myColDMax
Dim myColDNewMax
    myColDSum = WorksheetFunction.Sum(Range("D:D"))
    myColDMax = WorksheetFunction.Max(Range("D:D"))
    If myColDSum <> 100 Then
        myColDNewMax = (100 - myColDSum) + myColDMax
    Range("D:D").Select
    Selection.Find(What:=myColDMax, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Activate
    ActiveCell.Value = myColDNewMax
    End If
    Range("D1").Select
End Sub

Thanks again for the help. I've been learning a lot from these code snippets.

Thanks,
Keith






Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting highest count stolte XSLT 2 November 19th, 2008 04:00 PM
finding highest value Aprile Access VBA 3 October 3rd, 2007 07:13 AM
How to open only the record with highest ID ?! SKE Classic ASP Databases 2 May 16th, 2005 06:05 AM
Finding the highest value of a query ry Oracle 1 December 18th, 2004 12:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.