Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default adding arrays from multiple workbooks

Hi,

I have a number of workbooks in a folder (number varies) which I have to
open and add the values of the same range ("F15:M34") in each workbook into
a summary workbook.

The code below works, but to me appears long winded, is there an easier more
efficient way of writing this code. This is the first time I have had a go
at using arrays.

thanks in advance
Neil

Dim myValues(160) As Long
Dim MyPath As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
MyPath = ActiveWorkbook.Path
On Error Resume Next
Set wbCodeBook = ThisWorkbook

With Application.FileSearch
.NewSearch
.LookIn = MyPath
.FileType = msoFileTypeExcelWorkbooks
If .Execute 0 Then
For lCount = 2 To .FoundFiles.Count
Set wbResults =
Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)

For A = 1 To 20
myValues(A) = myValues(A) +
ActiveSheet.Range("F" & 14 + A).Value
Next A
For B = 21 To 40
myValues(B) = myValues(B) +
ActiveSheet.Range("G" & -6 + B).Value
Next B
For c = 41 To 60
myValues(c) = myValues(c) +
ActiveSheet.Range("H" & -26 + c).Value
Next c
For d = 61 To 80
myValues(d) = myValues(d) +
ActiveSheet.Range("I" & -46 + d).Value
Next d
For e = 81 To 100
myValues(e) = myValues(e) +
ActiveSheet.Range("J" & -66 + e).Value
Next e
For f = 101 To 120
myValues(f) = myValues(f) +
ActiveSheet.Range("K" & -86 + f).Value
Next f
For g = 121 To 140
myValues(g) = myValues(g) +
ActiveSheet.Range("L" & -106 + g).Value
Next g
For h = 141 To 160
myValues(h) = myValues(h) +
ActiveSheet.Range("M" & -126 + h).Value
Next h

'Close the workbook you opened
wbResults.Close SaveChanges:=True

Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
For i = 1 To 20
ActiveSheet.Range("F" & 14 + i).Value = myValues(i)
Next i
For j = 21 To 40
ActiveSheet.Range("g" & -6 + j).Value = myValues(j)
Next j
For k = 41 To 60
ActiveSheet.Range("h" & -26 + k).Value = myValues(k)
Next k
For l = 61 To 80
ActiveSheet.Range("i" & -46 + l).Value = myValues(l)
Next l
For m = 81 To 100
ActiveSheet.Range("j" & -66 + m).Value = myValues(m)
Next m
For n = 101 To 120
ActiveSheet.Range("k" & -86 + n).Value = myValues(n)
Next n
For o = 121 To 140
ActiveSheet.Range("l" & -106 + o).Value = myValues(o)
Next o
For p = 141 To 160
ActiveSheet.Range("m" & -126 + p).Value = myValues(p)
Next p

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default adding arrays from multiple workbooks

no worries, I have worked it out.

Set chkRange = ActiveSheet.Range("F15:m34")
For A = 1 To 160
myValues(A) = myValues(A) + chkRange(A).Value
Next A

"Neil Eves" wrote in message
...
Hi,

I have a number of workbooks in a folder (number varies) which I have to
open and add the values of the same range ("F15:M34") in each workbook
into a summary workbook.

The code below works, but to me appears long winded, is there an easier
more efficient way of writing this code. This is the first time I have had
a go at using arrays.

thanks in advance
Neil

Dim myValues(160) As Long
Dim MyPath As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
MyPath = ActiveWorkbook.Path
On Error Resume Next
Set wbCodeBook = ThisWorkbook

With Application.FileSearch
.NewSearch
.LookIn = MyPath
.FileType = msoFileTypeExcelWorkbooks
If .Execute 0 Then
For lCount = 2 To .FoundFiles.Count
Set wbResults =
Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)

For A = 1 To 20
myValues(A) = myValues(A) +
ActiveSheet.Range("F" & 14 + A).Value
Next A
For B = 21 To 40
myValues(B) = myValues(B) +
ActiveSheet.Range("G" & -6 + B).Value
Next B
For c = 41 To 60
myValues(c) = myValues(c) +
ActiveSheet.Range("H" & -26 + c).Value
Next c
For d = 61 To 80
myValues(d) = myValues(d) +
ActiveSheet.Range("I" & -46 + d).Value
Next d
For e = 81 To 100
myValues(e) = myValues(e) +
ActiveSheet.Range("J" & -66 + e).Value
Next e
For f = 101 To 120
myValues(f) = myValues(f) +
ActiveSheet.Range("K" & -86 + f).Value
Next f
For g = 121 To 140
myValues(g) = myValues(g) +
ActiveSheet.Range("L" & -106 + g).Value
Next g
For h = 141 To 160
myValues(h) = myValues(h) +
ActiveSheet.Range("M" & -126 + h).Value
Next h

'Close the workbook you opened
wbResults.Close SaveChanges:=True

Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
For i = 1 To 20
ActiveSheet.Range("F" & 14 + i).Value = myValues(i)
Next i
For j = 21 To 40
ActiveSheet.Range("g" & -6 + j).Value = myValues(j)
Next j
For k = 41 To 60
ActiveSheet.Range("h" & -26 + k).Value = myValues(k)
Next k
For l = 61 To 80
ActiveSheet.Range("i" & -46 + l).Value = myValues(l)
Next l
For m = 81 To 100
ActiveSheet.Range("j" & -66 + m).Value = myValues(m)
Next m
For n = 101 To 120
ActiveSheet.Range("k" & -86 + n).Value = myValues(n)
Next n
For o = 121 To 140
ActiveSheet.Range("l" & -106 + o).Value = myValues(o)
Next o
For p = 141 To 160
ActiveSheet.Range("m" & -126 + p).Value = myValues(p)
Next p



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
Adding a Row to Multiple Workbooks LPS Excel Discussion (Misc queries) 0 May 31st 07 05:04 PM
Arrays - declaration, adding values to arrays and calculation Maxi[_2_] Excel Programming 1 August 17th 06 04:13 PM
Adding multiple workbooks together Keith Excel Discussion (Misc queries) 1 August 8th 05 09:39 PM
adding certain cells in multiple worksheets in multiple workbooks Stephen via OfficeKB.com Excel Worksheet Functions 1 February 4th 05 08:31 PM
Adding cells from multiple workbooks Blackcat Excel Discussion (Misc queries) 6 January 19th 05 09:50 AM


All times are GMT +1. The time now is 01:20 AM.

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"