+ Reply to Thread
Results 1 to 10 of 10

Trouble with For Loop

  1. #1
    Registered User
    Join Date
    04-03-2014
    Location
    Woodbury, GA
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    37

    Trouble with For Loop

    I'm having some trouble with this code. I don't know what the trouble could be. When I tried it out with a smaller sheet (200) rows it worked just fine, but now the loop portion no longer does what I designed it to do.

    I basically need to put together separate spreadsheets for each of the city names (Griffin being one of them). I have A LOT of data to sift through. I need the program to copy and paste all info pertinent to the city name ("Sheet1") to a new spreadsheet ("Sheet2").

    Could someone give some direction on this. I thought this would be a pretty simple code to write, didn't try to do anything fancy, but it seems like it's giving me fits.

    Any help would be much obliged.

    -Tyler.


    Please Login or Register  to view this content.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Trouble with For Loop

    Probably easier to use Advanced Filter | Copy.

    Post a sample workbook, so I can show you an example on your data structure...
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Trouble with For Loop

    You will actually need a variable for the city name and then your code needs to dynamically create a new sheet if it does not exist yet and paste the record onto that sheet. After the macro has run, you should end up with a new sheet for each city in your list.
    Your code is only copying two columns, yet your columns are up to the "O" column. Is this intended?

    If you can post a sample file with some dummy data representing your data structure, we can help you with a macro.
    Regards,
    Rudi

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trouble with For Loop

    See if this helps?

    Please Login or Register  to view this content.
    Or better can you attach a sample file with some dummy data to test on?

  5. #5
    Registered User
    Join Date
    04-03-2014
    Location
    Woodbury, GA
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    37

    Re: Trouble with For Loop

    ok, let me know if this helps.Book1.xlsx

  6. #6
    Registered User
    Join Date
    04-03-2014
    Location
    Woodbury, GA
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    37

    Re: Trouble with For Loop

    By the way I modified my code, but now it only works on the first name (griffin #4) and only pastes the first column on all others.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Trouble with For Loop

    Doesn't help much - it bears no resemblance to the requirement you posted...

    Try again: Show us your actual data structure, and what your required outcome looks like. Mock it up manually if necessary. Include header row(s), include the relevant source AND target worksheets. By all means, sanitise any sensitive data, but make sure what is left is enough to work with, and matches what you are describing. There's no 'Griffin' in your attachment, there's no "Telemetry" sheet... etc.

    You know in your head what you mean. We only have your words and your workbook to go on...

  8. #8
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Trouble with For Loop

    Hi,

    Try this code...
    Workbook with code attached below.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-03-2014
    Location
    Woodbury, GA
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    37

    Re: Trouble with For Loop

    It worked great the first time I used it, but now I can't get it to function properly. It keeps giving an error message sating that the user - defined type is not defined.

  10. #10
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Trouble with For Loop

    Hi EverGreen,

    I forgot to mention that for this to run effectively you need to activate the Microsoft Scripting Runtime Library under the VBA Reference Library list.

    See the attached image and follow these steps to have the macro run properly again.

    1. From the workbook where you run the macro, press ALT+F11
    2. In the VBA editor which activated, click on the Tools Menu
    3. Select References
    4. In the dialog, scroll down to Microsoft Scripting Runtime, and tick the check box
    5. Select OK

    The macro will now run properly again.
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Trouble with loop
    By Jietoh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2013, 04:25 PM
  2. [SOLVED] I am having trouble accessing information with an if loop.
    By Spreadlover in forum Excel Formulas & Functions
    Replies: 32
    Last Post: 07-17-2013, 04:39 PM
  3. VBA Find Copy Loop Trouble
    By antora in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-14-2013, 04:04 AM
  4. Trouble with Formula (in loop)
    By dugong in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2008, 05:27 AM
  5. trouble with counter in range loop
    By brya6347 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-22-2005, 06:25 PM

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