+ Reply to Thread
Results 1 to 5 of 5

Loop for calculating the sales for a given area and a commodity

  1. #1
    Registered User
    Join Date
    11-13-2013
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    7

    Loop for calculating the sales for a given area and a commodity

    Hi All,

    I was trying to use the following VB Script to calculate the Sales figure for a particular commodity and of a particular region and the end result being displayed in a MSGBOX. Finally, I get the message box but with no values in it.

    ------------------------------------------------------------------------------------
    Private Sub Sales()

    Dim Example As Worksheet
    Set Example = ThisWorkbook.Sheets("Example")

    LastRow = Example.Cells(Rows.Count, 1).End(xlUp).Row
    SelRow = Selection.Row
    Category = Example.Cells(SelRow, 9)
    City = Example.Cells(SelRow, 5)

    For x = 2 To LastRow
    If Example.Cells(x, 9) = Category And Example.Cells(x, 5) = City Then
    Tsale = Tsale + Example.Cells(x, 11)
    End If
    Next x
    Stop

    MsgBox "The total sale for" & Category & " and " & City & " is " & Tsale, vbOKOnly, "Msg1"

    End Sub
    -----------------------------------------------------------------------------------------------
    Please help me out folks..

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Loop for calculating the sales for a given area and a commodity

    Your code syntax looks functional.

    When you run the code and it "Stops, what are the LastRow, SelRow, Category, and City values?
    What is the Active cell?

    This line is case sensitive for exact matches e.g. Category does not equal category
    If Example.Cells(x, 9) = Category And Example.Cells(x, 5) = City Then

    Alternatively, you could do this with the SUMIFS function instead of a For-Next loop.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    11-13-2013
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Loop for calculating the sales for a given area and a commodity

    Can u throw some light on to the "SUMIFS".. I am a bit struggling in using it..!!

    All I want to do is calculate the Sum of the sales of a particular city and of a particular commodity.. just like using SUMIF( range, criteria, [sum_range] )

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Loop for calculating the sales for a given area and a commodity

    Quote Originally Posted by crr.potluri View Post
    Can u throw some light on to the "SUMIFS".. I am a bit struggling in using it..!!

    All I want to do is calculate the Sum of the sales of a particular city and of a particular commodity.. just like using SUMIF( range, criteria, [sum_range] )
    This is how it might be done with SUMIFS (not tested) if I interpreted your original code correctly.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-13-2013
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Loop for calculating the sales for a given area and a commodity

    Thank you AlphaFrog.. It worked.. I was able to execute the script..!!

    Thanks again..

+ 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. Calculating a Sales Bonus When the Bonus Rate Changes as Sales Change
    By daydreamz in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-04-2012, 11:52 AM
  2. Replies: 2
    Last Post: 07-21-2010, 12:54 PM
  3. Edit PivotTable by removing Sales Button from ROW area?
    By eholt1941 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-13-2006, 09:45 PM
  4. Converting postcode to sales area
    By LarsF in forum Excel General
    Replies: 2
    Last Post: 08-18-2005, 06:05 AM
  5. Commodity Trading? Solver maybe....
    By bonjella in forum Excel General
    Replies: 3
    Last Post: 06-08-2005, 01:05 PM

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