Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Need some syntax help, please

Hello!

I have 5 workbooks which contain data: GROUP1.xls ...
GROUP5.xls. {NOTE: These names are not cast in stone; I
can change them if needed to solve this problem. Read
on.} Each workbook contains 5 worksheets. The books are
identical in structure, i.e. the names of the 5
worksheets are the same in all 5 workbooks, only the data
on the sheets is different.

I want to use a 6th workbook with VBA code to extract and
combine data from the 5 workbooks (at times only two, but
other times, 3, 4 or 5), but I can't figure out how to do
it with a loop because I can't get the syntax correct.

For example, assume cells A1 of Sheet1 of the 2,3,4 or 5
workbooks contain a number. I want Cell A1 of the 6th
book to contain the sum of those numbers. So I want to
loop through each of the five, add the numbers together,
and display the total in Cell A1 of the 6th book.

I need something which will allow me to VARY the name of
the workbook I'm extracting from but still have VBA
recognize it. Here is what I mean:

NumberBooks = {2, 3, 4 or 5}
Dim MYSHEET, SixthBook as Object
Set SixthBook = {active workbook sheet}
For N = 1 to NumberBooks
Set MYSHEET&N = Workbooks(Group&N.xls).Sheets("SheetName")
SixthBook.Cells(#,#) = Workbooks(Group&N.xls).Sheets_
("SheetName")
{addition operation}
Next N
{display operation}

I know this isn't right, but hopefully you understand
what I mean. I want to go from book to book using a for-
next loop with the book name as a variable.

Anybody know of a slick way to do this? Help is
appreciated.

OBTW: I currently have a hard-coded version of this
operation which I had to complete to meet a deadline. It
took 1547 lines of code. I have another project coming
which will be similar, but different enough where I'll
have to start from scratch. I'd like to avoid hardcoding
again, hence the question. Thanks for your time.

MARTY

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need some syntax help, please

The only thing I see "wrong" is that the Dim statement should be:

Dim SixthBook as Worksheet

The way that the code is written, all of the workbooks need to be ope
when you start, right?

If all of the books are open when you start, then you might not want
For ... Next loop. Maybe you want to use For Each.

For Each Window In Application.Windows
Window.Activate
Next Windo

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Need some syntax help, please

Yes, all of the workbooks will have to be open.

If I use a For Each, it will also try to activate the 6th
book (which is also open), and access its cell A1 also,
which I don't want it to do. Also, I need to use a For
Next for other purposes in the routine.

To boil it down, I guess my REAL question is this: if N
is an integer, is this syntax (&N) allowed in workbook
names, objects, etc. Example:

Set MYSHEET&N = Workbooks(Group&N.xls).Sheets("SheetName")

If N = 1, will VBA interpret this as:

Set MYSHEET1 = Workbooks(Group1.xls).Sheets("SheetName")

?????

Thanks,
MARTY
-----Original Message-----
The only thing I see "wrong" is that the Dim statement

should be:

Dim SixthBook as Worksheet

The way that the code is written, all of the workbooks

need to be open
when you start, right?

If all of the books are open when you start, then you

might not want a
For ... Next loop. Maybe you want to use For Each.

For Each Window In Application.Windows
Window.Activate
Next Window


---
Message posted from http://www.ExcelForum.com/

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Need some syntax help, please

Marty,

The first sub below calls the second, which sums the value from the sheet,
cell, and array of "GROUP?.xls" depending on what is passed to it. The
example will sum Sheet1 A1 from GROUP2, GROUP3, GROUP4, and GROUP5.xls, and
places the sum into cell A1 (the same cell as that being summed) of the
active sheet of the workbook where the code is located.

The GetSum macro can be called as many times as you want. To call it to sum
cells A1:A10 from Sheet1 of Group2, 3, 4 and 5, you could do this:

Dim Books As Variant

Dim myCell As Range

Books = Array(2, 3, 4, 5)

For Each myCell In Range("A1:A10")
'Call the summing macro
GetSum "Sheet1", myCell.Address, Books
Next myCell

HTH,
Bernie
MS Excel MVP

Sub PutSum()
Dim Books As Variant
Books = Array(2, 3, 4, 5)

'Call the summing macro
GetSum "Sheet1", "A1", Books

End Sub

Sub GetSum(SheetName As String, _
CellAdd As String, _
BookNums As Variant)
Dim N As Integer
Dim mySum As Double

For N = LBound(BookNums) To UBound(BookNums)
mySum = mySum + Workbooks("GROUP" & BookNums(N) & ".xls"). _
Worksheets(SheetName).Range(CellAdd).Value
Next N
ThisWorkbook.ActiveSheet.Range(CellAdd).Value = mySum
End Sub


"MARTY" wrote in message
...
Hello!

I have 5 workbooks which contain data: GROUP1.xls ...
GROUP5.xls. {NOTE: These names are not cast in stone; I
can change them if needed to solve this problem. Read
on.} Each workbook contains 5 worksheets. The books are
identical in structure, i.e. the names of the 5
worksheets are the same in all 5 workbooks, only the data
on the sheets is different.

I want to use a 6th workbook with VBA code to extract and
combine data from the 5 workbooks (at times only two, but
other times, 3, 4 or 5), but I can't figure out how to do
it with a loop because I can't get the syntax correct.

For example, assume cells A1 of Sheet1 of the 2,3,4 or 5
workbooks contain a number. I want Cell A1 of the 6th
book to contain the sum of those numbers. So I want to
loop through each of the five, add the numbers together,
and display the total in Cell A1 of the 6th book.

I need something which will allow me to VARY the name of
the workbook I'm extracting from but still have VBA
recognize it. Here is what I mean:

NumberBooks = {2, 3, 4 or 5}
Dim MYSHEET, SixthBook as Object
Set SixthBook = {active workbook sheet}
For N = 1 to NumberBooks
Set MYSHEET&N = Workbooks(Group&N.xls).Sheets("SheetName")
SixthBook.Cells(#,#) = Workbooks(Group&N.xls).Sheets_
("SheetName")
{addition operation}
Next N
{display operation}

I know this isn't right, but hopefully you understand
what I mean. I want to go from book to book using a for-
next loop with the book name as a variable.

Anybody know of a slick way to do this? Help is
appreciated.

OBTW: I currently have a hard-coded version of this
operation which I had to complete to meet a deadline. It
took 1547 lines of code. I have another project coming
which will be similar, but different enough where I'll
have to start from scratch. I'd like to avoid hardcoding
again, hence the question. Thanks for your time.

MARTY



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Syntax dhstein Excel Discussion (Misc queries) 6 November 8th 08 09:13 PM
VBA syntax Sunantoro Excel Discussion (Misc queries) 1 September 21st 05 03:19 AM
Syntax Help Dmorri254 Excel Worksheet Functions 2 March 2nd 05 02:51 PM
Help with VBA syntax jacqui[_2_] Excel Programming 3 January 13th 04 02:29 PM
Syntax Michael[_23_] Excel Programming 1 December 30th 03 10:08 PM


All times are GMT +1. The time now is 05:55 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"