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 December 15th, 2005, 02:28 AM
Registered User
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vicky.dalmia Send a message via Yahoo to vicky.dalmia
Default Need Help For A Macro Code

Hello EveryBody

This is Vikash Here and ,this is my first time I am Posting my Problem, Kindly Look into it and help me out


I am writing a macro for an Excel Sheet. The Excel Sheet Look Like This:

Ord No Trd Qty Ord Qty

200512135273 800 3100
200512135273 600 2300
200512135273 700 1700
200512135273 1000 1000
200512135275 500 700
200512135275 200 200
200512136450 100 300
200512136450 200 200

Want I want is According to order No i.e. 200512135273 which is having 4 Nos. of trade of diff. qty to sum it up and get a single row which showing as

Ord No Trd Qty Ord Qty

200512135273 3100 3100
200512135275 700 700
200512136450 300 300

Plz help me to solve the prob

Vikash Dalmia
Old December 16th, 2005, 07:37 AM
Authorized User
Join Date: Oct 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered

Ok, first up, I wouldn't usually do this with code, I'd either use Excels built in subtotals or a pivot table - you might want to consider that..

Anyway, that aside, here is some code that will do what you need :

Sub Collate_Data(sSheet As String, i1 As Integer, i2 As Integer)
    ' Check how many different elements there are
    Dim dNum() As Double
    ReDim dNum(i1 To i2)
    dNum(i1) = Sheets(sSheet).Cells(i1, 2).Value
    iInc = i1
    For iLoop = i1 To i2
        bMatch = False
        For iLoop2 = i1 To i2
            If dNum(iLoop2) = Sheets(sSheet).Cells(iLoop, 2).Value Then bMatch = True
        If bMatch = False Then
            iInc = iInc + 1
            dNum(iInc) = Sheets(sSheet).Cells(iLoop, 2).Value
        End If
    ReDim Preserve dNum(i1 To iInc)
    ' Create New table entries
    For iLoop = LBound(dNum) To UBound(dNum)
        Sheets(sSheet).Cells(iLoop, 8).Value = dNum(iLoop)
        For iLoop1 = i1 To i2
            If Sheets(sSheet).Cells(iLoop1, 2).Value = dNum(iLoop) Then
                Sheets(sSheet).Cells(iLoop, 9).Value = Sheets(sSheet).Cells(iLoop, 9).Value + Sheets(sSheet).Cells(iLoop1, 3).Value
                Sheets(sSheet).Cells(iLoop, 10).Value = Sheets(sSheet).Cells(iLoop, 10).Value + Sheets(sSheet).Cells(iLoop1, 4).Value
            End If
        Next iLoop1
End Sub

This code assumes that you are passing it the sheet name, first row that the data is in and the last row that the data is in
The column number has been assumed as 2 for the Ord No.
The result data is written to the right in columns 8 9 and 10

Additional parameters can easily be added if you want this to change often..

Similar Threads
Thread Thread Starter Forum Replies Last Post
calling to xlam macro from macro inside xlsb SteveB Excel VBA 0 June 30th, 2008 06:43 PM
Run code in Macro Corey Access VBA 4 February 5th, 2007 10:54 AM
How to protect macro code? kotaiah Excel VBA 1 September 25th, 2006 02:57 PM
Hide VBA code when forwarding macro sh333384 Excel VBA 7 September 2nd, 2004 10:23 AM
Macro or Code? lryckman Access 7 August 24th, 2004 01:00 AM

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