+ Reply to Thread
Results 1 to 5 of 5

Basic For/Each question

  1. #1
    Registered User
    Join Date
    10-09-2012
    Location
    Missouri
    MS-Off Ver
    Excel 2010
    Posts
    3

    Basic For/Each question

    Hello community,

    I am just beginning to learn how to implement loops, so bare with me here in the thread.

    I have a dynamic range on Sheet 1 that uses the following offset formula to return a range from A2--that's where the records actually start--and the last used row in the column. For the purposes of this thread, I'll call it "MyRange".

    In Sheet 2, I have a form that is intended to loop through "MyRange", and plug each cell in "MyRange", one-by-one, into Sheet 2, Cell A1.

    After it drops that name, the form will gather the rest of that respective entry's data from Sheet 1.

    Here is the code I was working with:

    Please Login or Register  to view this content.
    Any suggestions?


    Zack

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    A few things not quite right.

    The first thing is that you need to find the last row before you set the range Alpha.

    Also, with the code as it is that range will only be one cell.

    Finally, no need to use select.

    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-09-2012
    Location
    Missouri
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Basic For/Each question

    Thanks for the suggestion, Norie!

    Unfortunately, it is returning a Run-time error 1004: Application-defined or object-defined error.

    After I used the F8 debugger to see what was going on, it says the following:

    ws.Range("$A$2:A" & LastRow) = <Method 'Range of object '_Worksheet' failed>

    Any suggestions?


    Zack

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic For/Each question

    My bad, finding last row should come after setting the worksheet reference.
    Please Login or Register  to view this content.
    PS Do the sheet names definitely have a space after 'Sheet'? Might be worth checking.

  5. #5
    Registered User
    Join Date
    10-09-2012
    Location
    Missouri
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Basic For/Each question

    Thanks Norie! Works beautifully!

    Is there a way to return another portion of that record in the next cell in Sheet2?

    Let's say that Sheet 2, $A$1 is your name, and that Sheet 2, $A$2 is the location for that record.

    Is there a way to do that in for each loop?

+ 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.6.0 RC 1