Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Arrays and Loops

Thanks to Ron de Bruin, I've got code that copies data from a named range to
a "database" worksheet. I want to loop through all named ranges (which
represent a dynamic range in each worksheet) and copy them to the "database"
worksheet.

Here's what I've got:

Sub Consolidate()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
Sheets("Consolidated").Range("2:2", Selection.End(xlDown)).Delete
Shift:=xlUp

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)

Set SourceRange = Range(rName)

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the destination
cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

When running this, I get a method Range of object _Global failed.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Arrays and Loops

It might be as simple as:

Set SourceRange = Range(rName(i))

But I think I'd be more explicit. I'd specify the workbook where those names
are living:

Option Explicit
Sub Consolidate()
Dim SourceWkbk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim DestSheet As Worksheet
Dim Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set SourceWkbk = ActiveWorkbook 'or something else???

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
With Worksheets("Consolidated")
.Rows("2:" & .Rows.Count).Delete
End With

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)
Set SourceRange = SourceWkbk.Names(rName(i)).RefersToRange

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the dest cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub



Kirk P. wrote:

Thanks to Ron de Bruin, I've got code that copies data from a named range to
a "database" worksheet. I want to loop through all named ranges (which
represent a dynamic range in each worksheet) and copy them to the "database"
worksheet.

Here's what I've got:

Sub Consolidate()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
Sheets("Consolidated").Range("2:2", Selection.End(xlDown)).Delete
Shift:=xlUp

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)

Set SourceRange = Range(rName)

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the destination
cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

When running this, I get a method Range of object _Global failed.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Arrays and Loops

Hi Kirk

Try it with this

Set SourceRange = Range(rName(i))

You can move this line outside the loop
Set DestSheet = Sheets("Consolidated")

Change your names
FBU1 is a cell in Excel 2007

Do all names exist in the array?


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Kirk P." wrote in message ...
Thanks to Ron de Bruin, I've got code that copies data from a named range to
a "database" worksheet. I want to loop through all named ranges (which
represent a dynamic range in each worksheet) and copy them to the "database"
worksheet.

Here's what I've got:

Sub Consolidate()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
Sheets("Consolidated").Range("2:2", Selection.End(xlDown)).Delete
Shift:=xlUp

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)

Set SourceRange = Range(rName)

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the destination
cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

When running this, I get a method Range of object _Global failed.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Arrays and Loops

Thanks Ron and Dave. I got it working with your help!

"Dave Peterson" wrote:

It might be as simple as:

Set SourceRange = Range(rName(i))

But I think I'd be more explicit. I'd specify the workbook where those names
are living:

Option Explicit
Sub Consolidate()
Dim SourceWkbk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim DestSheet As Worksheet
Dim Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set SourceWkbk = ActiveWorkbook 'or something else???

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
With Worksheets("Consolidated")
.Rows("2:" & .Rows.Count).Delete
End With

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)
Set SourceRange = SourceWkbk.Names(rName(i)).RefersToRange

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the dest cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub



Kirk P. wrote:

Thanks to Ron de Bruin, I've got code that copies data from a named range to
a "database" worksheet. I want to loop through all named ranges (which
represent a dynamic range in each worksheet) and copy them to the "database"
worksheet.

Here's what I've got:

Sub Consolidate()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim rName As Variant
Dim i As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Define the named range array
rName = Array("FBU1", "FBU2", "FBU3", "FBU4", "FBU5", "FBU6", "STAT1")

'Clear the Destination sheet
Sheets("Consolidated").Range("2:2", Selection.End(xlDown)).Delete
Shift:=xlUp

'fill in the Source Sheet and range
For i = LBound(rName) To UBound(rName)

Set SourceRange = Range(rName)

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Consolidated")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can create a
'destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)

'Copy the source range and use PasteSpecial to paste in the destination
cell
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

When running this, I get a method Range of object _Global failed.


--

Dave Peterson

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
Arrays and "For" "Next" loops [email protected] Excel Programming 8 March 30th 07 03:45 PM
slow for loops ... better way using arrays or something? wdeleo[_2_] Excel Programming 4 July 28th 06 03:29 PM
VBA excel using arrays and loops [email protected] Excel Programming 1 July 7th 06 01:48 AM
Scope of the arrays in Loops itsmaheshp[_9_] Excel Programming 1 November 15th 04 12:51 PM
Arrays to replace very slow loops ? vbastarter Excel Programming 5 August 10th 04 07:15 PM


All times are GMT +1. The time now is 05:19 AM.

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

About Us

"It's about Microsoft Excel"