Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Can't get past Line 3 of code

Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
..... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Can't get past Line 3 of code

It works for me - what's the error?

Jim May wrote:
Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Can't get past Line 3 of code

Thanks Andrew;
I was stepping through the code (F8) while my Sheet2 was the
activesheet.
Cause (after your reply..
When I retested it and it worked << Cause My Sheet1 was then active <<
Must keep this in mind in the future

Why does the Activesheet matter?



"Andrew Taylor" wrote in message
oups.com:

It works for me - what's the error?

Jim May wrote:

Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Can't get past Line 3 of code

You can only select on the active sheet.

So maybe just adding

worksheets("Sheet1").select

would help

Jim May wrote:

Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Can't get past Line 3 of code

Thanks Dave for "an important base rule" - unknown to me, till now.
Only how I can remember;
Have a great day !!
Jim

"Dave Peterson" wrote in message
:

You can only select on the active sheet.

So maybe just adding

worksheets("Sheet1").select

would help

Jim May wrote:


Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub



--

Dave Peterson




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Can't get past Line 3 of code

Another useful "rule" to be aware of is that it's almost always
unnecessary to select ranges before using them, and to do
so will usually make your code run much more slowly.

So in your example you could have done somewthing like:
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion
Set srng2 = srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3)

and this doesn't depend on having the right sheet activated,though
it dos assume you're in the right workbook; but you can handle this
by (e.g.) :
Set srng =
Workbooks("data.xls").Worksheets("Sheet1").Range(" A1").CurrentRegion

It's also good to assign the workbooks and sheets you're using to
variables:
e.g. set MyWb = Workbooks("data.xls").
set SourceSheet = MyWb.Worksheets("Sheet1")
set srng = SourceSheet.Range("A1").CurrentRegion

And just one more rule in case you're not aware of it
- ALWAYS use Option Explicit ;)


hth
Andrew


Jim May wrote:
Thanks Dave for "an important base rule" - unknown to me, till now.
Only how I can remember;
Have a great day !!
Jim

"Dave Peterson" wrote in message
:

You can only select on the active sheet.

So maybe just adding

worksheets("Sheet1").select

would help

Jim May wrote:


Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub



--

Dave Peterson


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 477
Default Can't get past Line 3 of code

Andrew,
This is really good stuff to know; thank you
for taking the time to help me (with what
the books are not as clear on..).
Jim May

"Andrew Taylor" wrote:

Another useful "rule" to be aware of is that it's almost always
unnecessary to select ranges before using them, and to do
so will usually make your code run much more slowly.

So in your example you could have done somewthing like:
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion
Set srng2 = srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3)

and this doesn't depend on having the right sheet activated,though
it dos assume you're in the right workbook; but you can handle this
by (e.g.) :
Set srng =
Workbooks("data.xls").Worksheets("Sheet1").Range(" A1").CurrentRegion

It's also good to assign the workbooks and sheets you're using to
variables:
e.g. set MyWb = Workbooks("data.xls").
set SourceSheet = MyWb.Worksheets("Sheet1")
set srng = SourceSheet.Range("A1").CurrentRegion

And just one more rule in case you're not aware of it
- ALWAYS use Option Explicit ;)


hth
Andrew


Jim May wrote:
Thanks Dave for "an important base rule" - unknown to me, till now.
Only how I can remember;
Have a great day !!
Jim

"Dave Peterson" wrote in message
:

You can only select on the active sheet.

So maybe just adding

worksheets("Sheet1").select

would help

Jim May wrote:


Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
.... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


--

Dave Peterson



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Can't get past Line 3 of code

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion
Worksheets("Sheet1").Select
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select
Set drng = Worksheets("Sheet2").Range("A2")
End Sub

would be my guess.

--
Regards,
Tom Ogilvy


"Jim May" wrote:

Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
..... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 477
Default Can't get past Line 3 of code

Thks Tom, Good Guess!!
Jim

"Tom Ogilvy" wrote:

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion
Worksheets("Sheet1").Select
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select
Set drng = Worksheets("Sheet2").Range("A2")
End Sub

would be my guess.

--
Regards,
Tom Ogilvy


"Jim May" wrote:

Can someone point out my fault in line 3?
TIA,

Sub tester()
Set srng = Worksheets("Sheet1").Range("A1").CurrentRegion ' Covers
Range A1:C10
srng.Offset(1, 0).Resize(srng.Rows.Count - 1, 3).Select << error
..... In Progress
Set drng = Worksheets("Sheet2").Range("A2")
End Sub


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
Macro code to put series name next to individual line in line grap Otani Charts and Charting in Excel 3 February 23rd 10 07:24 PM
problem with code. past link instead of copy Paul Excel Programming 0 April 20th 06 08:57 AM
Code to delete a Line in a another code helmekki[_88_] Excel Programming 1 August 8th 05 01:14 AM
Continue line of VBA code on the next line peacelittleone Excel Programming 9 June 24th 05 07:46 PM
Code doesn't go past first IF statement keepITcool Excel Programming 2 July 20th 04 09:48 PM


All times are GMT +1. The time now is 11:48 PM.

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"