ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dynamic Range Offset causing problem with this code (https://www.excelbanter.com/excel-programming/336269-dynamic-range-offset-causing-problem-code.html)

Arishy[_2_]

Dynamic Range Offset causing problem with this code
 
I have a wb with several sheets, each has a dynamic range with a name
CODExx.

The following code - partly from the posts here - works if I have NONE
dynamic range (=offset(sheets......etc)

Public Sub moveplist()
Dim rcnt As Integer
Dim nme As Variant
For Each nme In ActiveWorkbook.Names
If Left(nme.Name, 4) = "CODE" Then
'Debug.Print nme.Name
'Debug.Print Range("nme.Name")
Range(nme.Name).Copy Destination:= _
Worksheets("PTable").Cells(1 + rcnt,
1).Range(Range(nme.Name).Address)
End If
rcnt = rcnt + Range(Range(nme.Name).Address).Rows.Count
Next nme

End Sub

If I use dynamic range I get an error msg
Rune Time error '1004'
Method 'Range' of Object 'Global' failed

Can you help


Bob Phillips[_6_]

Dynamic Range Offset causing problem with this code
 
Arishy,

I couldn't reproduce your error, but I got an error, caused I think by
incrementing the counter outside the If test.

Reversing them solved my error

End If
rcnt = rcnt + Range(Range(nme.Name).Address).Rows.Count

to

rcnt = rcnt + Range(Range(nme.Name).Address).Rows.Count
End If


--
HTH

Bob Phillips

"Arishy" wrote in message
oups.com...
I have a wb with several sheets, each has a dynamic range with a name
CODExx.

The following code - partly from the posts here - works if I have NONE
dynamic range (=offset(sheets......etc)

Public Sub moveplist()
Dim rcnt As Integer
Dim nme As Variant
For Each nme In ActiveWorkbook.Names
If Left(nme.Name, 4) = "CODE" Then
'Debug.Print nme.Name
'Debug.Print Range("nme.Name")
Range(nme.Name).Copy Destination:= _
Worksheets("PTable").Cells(1 + rcnt,
1).Range(Range(nme.Name).Address)
End If
rcnt = rcnt + Range(Range(nme.Name).Address).Rows.Count
Next nme

End Sub

If I use dynamic range I get an error msg
Rune Time error '1004'
Method 'Range' of Object 'Global' failed

Can you help





All times are GMT +1. The time now is 01:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com