PC Review


Reply
Thread Tools Rate Thread

Complex macro - Any ideas would be great

 
 
Gareth.Evans
Guest
Posts: n/a
 
      25th Jun 2009
HI all,

I am strugling to produce the bellow functionallity for a reporting
system.

I have a main workbook ('Invoice') which contains invoice numbers, i
needs to look up data in several other workbooks
('January','Febuary','March'), consolidate the data corrosponding to a
certain invoice, then import it into a sheet in the main workbook.

EXAMPLE:

Workbook 'Invoice', sheet 1:

Invoice Number | Lookup Workbook
2 January
3 Febuary
4 March
5 Febuary
6 January


Workbook 'Febuary', sheet 1:

Invoice Number | Amount
3 10
3 30
3 15
5 50
5 30

FINAL Desired Result in Workbook 'Invoice', sheet 2

Invoice Number | Amount
3 55
5 80

Hope that makes sense. This is obvoicesly a simple example, in real i
need to import many other fields for thousands of records.

Realy hope someone can help me.

Kind Regards,



 
Reply With Quote
 
 
 
 
Patrick Molloy
Guest
Posts: n/a
 
      12th Jul 2009
several possible solutions come to mind - "there's more than one way to skin
a cat" - sorry cat people

however, it depends on how many records you have

also, in your Invoice sheet, will an invoice number only have the one month
as per your example?

my first suggestion would be to bring all the data from the January -
December worksheets into one sheet in the invoice workbook, and then you can
just use the SUMIF function.

the following code should be pasted into a code module

Option Explicit
Sub RefreshData()
Dim mnth As Long ' index for monthly workbooks
Dim sfile As String ' "root" file name - January, February etc
Dim sfilename As String ' full filename if found eg January.xls
Dim wb As Workbook ' object for the monthly workbook
Dim source As Range ' data from monthly book
Dim target As Range ' target range in Invoice book
'clear target table
Sheet2.Cells.ClearContents

For mnth = 1 To 12
sfile = Format(DateSerial(Year(Date), mnth, 1), "MMMM")
sfilename = Dir(sfile & ".xls")
If sfilename <> "" Then
Set wb = Workbooks.Open(sfilename)
Set source = wb.Worksheets("sheet1").Range("A1").CurrentRegion
With ThisWorkbook.Worksheets("sheet2")
If .Range("A1") = "" Then
Set target = .Range("A1")
Else
Set target = .Range("A1").End(xlDown).Offset(1)
End If
End With
With source
target.Resize(.Rows.Count, .Columns.Count).Value = .Value
target.Offset(, 2).Resize(.Rows.Count, 1) = sfile
End With

wb.Close False
Set wb = Nothing
End If
Next
End Sub

After running this sub, you'll have in sheet2 a table with three columns
A is the Invoice
B is the Amount
C is the Month

all you need to do now is an array formula in c,
eg C2
{==SUM((Sheet2!A2:A9=Sheet1!A2)*(Sheet2!C2:C9=Sheet1!B2)*(Sheet2!B2:B9)) }










"smartin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Gareth.Evans wrote:
>> HI all,
>>
>> I am strugling to produce the bellow functionallity for a reporting
>> system.
>>
>> I have a main workbook ('Invoice') which contains invoice numbers, i
>> needs to look up data in several other workbooks
>> ('January','Febuary','March'), consolidate the data corrosponding to a
>> certain invoice, then import it into a sheet in the main workbook.
>>
>> EXAMPLE:
>>
>> Workbook 'Invoice', sheet 1:
>>
>> Invoice Number | Lookup Workbook
>> 2 January
>> 3 Febuary
>> 4 March
>> 5 Febuary
>> 6 January
>>
>>
>> Workbook 'Febuary', sheet 1:
>>
>> Invoice Number | Amount
>> 3 10
>> 3 30
>> 3 15
>> 5 50
>> 5 30
>>
>> FINAL Desired Result in Workbook 'Invoice', sheet 2
>>
>> Invoice Number | Amount
>> 3 55
>> 5 80
>>
>> Hope that makes sense. This is obvoicesly a simple example, in real i
>> need to import many other fields for thousands of records.
>>
>> Realy hope someone can help me.
>>
>> Kind Regards,
>>
>>
>>

>
> Have a look at Debra Dalgleish's explanation of the technique here:
>
> http://www.contextures.com/xlFunctions05.html#RefWkbk


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converstion of Complex Word VBA macro to Excel Macro Michael Microsoft Excel Programming 1 18th Jan 2008 12:21 AM
Great Macro for Outlook!!!! =?Utf-8?B?UGFzc2lvbmF0ZSBPdXRsb29rIHVzZXI=?= Microsoft Outlook Discussion 0 10th Feb 2007 07:15 PM
Have this great Macro, but!! =?Utf-8?B?SmltIE1heQ==?= Microsoft Excel Programming 1 19th May 2006 06:07 PM
I need a great virtual planner.... ideas?? =?Utf-8?B?R2FsZW4=?= Windows XP General 8 3rd Feb 2005 07:19 PM
Any Great Ideas to Make Old Boring Excel Charts to be EXCITING & ENGAGING? hce Microsoft Excel Misc 3 26th Mar 2004 01:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:16 PM.