+ Reply to Thread
Results 1 to 9 of 9

Thread: Can't get past Line 3 of code

  1. #1
    Jim May
    Guest

    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. #2
    Dave Peterson
    Guest

    Re: 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

  3. #3
    Andrew Taylor
    Guest

    Re: 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



  4. #4
    Tom Ogilvy
    Guest

    RE: 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
    >
    >


  5. #5
    Jim May
    Guest

    Re: 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" <andrew.taylor@cantab.net> wrote in message
    news:1152275119.743911.267280@m73g2000cwd.googlegroups.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



  6. #6
    Jim May
    Guest

    Re: 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" <petersod@verizonXSPAM.net> wrote in message
    news:44AE52D1.5F6453B1@verizonXSPAM.net:

    > 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. #7
    Andrew Taylor
    Guest

    Re: 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" <petersod@verizonXSPAM.net> wrote in message
    > news:44AE52D1.5F6453B1@verizonXSPAM.net:
    >
    > > 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. #8
    Jim May
    Guest

    Re: 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" <petersod@verizonXSPAM.net> wrote in message
    > > news:44AE52D1.5F6453B1@verizonXSPAM.net:
    > >
    > > > 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

    >
    >


  9. #9
    Jim May
    Guest

    RE: 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0