+ Reply to Thread
Results 1 to 11 of 11

InStr macro takes forever.

  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    InStr macro takes forever.

    Is there any options better than InStr. It takes forever to do. All It is doing is looking in E for the phrase and then copying the rows and pasting.

    Please Login or Register  to view this content.
    Sub TopErrorACSI()
    Dim i As Long, c As Range
    Dim rng As Range

    Set rng = Worksheets("PasteValues").Range("E:E")

    For Each c In rng
    If InStr(c, "ACSI") > 0 Then
    c.Offset(1, 1).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("B8")
    c.Offset(3, 1).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("B9")
    c.Offset(4, 1).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("B10")

    c.Offset.Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("A7")
    c.Offset(1, 0).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("A8")
    c.Offset(2, 0).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("A9")
    c.Offset(3, 0).Copy
    c.Worksheet.Paste Destination:=Worksheets("Top Errors").Range("A10")


    End If
    Next c
    End Sub

  2. #2
    Registered User
    Join Date
    07-26-2013
    Location
    BL, Bosnia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: InStr macro takes forever.

    Try this:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,695

    Re: InStr macro takes forever.

    Could you explain what "c.Worksheet" means in
    Please Login or Register  to view this content.
    and what is this suppose to copy?
    Please Login or Register  to view this content.
    Is the code meant to overwrite everytime it finds "ASCI"? All your paste ranges are hard coded.
    Maybe explain what you want to achieve.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: InStr macro takes forever.

    You are looking in every cell looking for a specific text.

    So if you have 2000 cells, actually because you have specified a complete column that is millions of cells ie 1048576 cells.

    So your logic is

    Look in cell
    Does it contain my search test
    If so then do something
    Go back and look at the next cell

    So if you have one cell that contains your text you have to loop 1048576 times.

    '*****************************************************

    Another way is:-

    Find the last used cell in my target row
    My Starting Position is row 1

    Find the first appearance of my search text between my starting position and the last used cell
    If found then
    a. Starting position is the row number of the cell plus 1
    b. Do something
    c. Go back and look again

    Otherwise Quit.

    So using my logic with only one occurrence of the Search Text
    You only go through the loop once.

    So which method is faster?

    '******************************************************


    Please Login or Register  to view this content.
    Last edited by mehmetcik; 12-02-2017 at 12:19 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: InStr macro takes forever.

    Thanks! When I have a moment I will try out your code in full, but you said something that really helped in the short term. Since I know the approx range of the search, I can really narrow it down and not make it look through the entire column. So instead of "M:N", i did "M1:N50" and that cut a lot of time. Thank you again

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: InStr macro takes forever.

    If there should only be one match- as I assume since you overwrite the same cells each time- you might add Exit For before the End If line.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: InStr macro takes forever.

    overwrite? No, I copy from one sheet to another. Then repeat but the locating i am pasting too isn't the same.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: InStr macro takes forever.

    Quote Originally Posted by taylorsm View Post
    overwrite? No, I copy from one sheet to another. Then repeat but the locating i am pasting too isn't the same.
    The destination cells are fixed in the code you posted, so they get overwritten if there are multiple matches.

  9. #9
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: InStr macro takes forever.

    Oh ok. Yeah there is only ever one match.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: InStr macro takes forever.

    Then you should add an exit for- no point carrying on the search after you found it.

  11. #11
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: InStr macro takes forever.

    Gotcha, Thanks!

+ 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. Macro takes forever after upgrading from excel 20007 to 2010
    By Yigal in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-05-2014, 10:45 AM
  2. Macro Takes forever To Execute
    By daveyc18 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-16-2014, 11:14 AM
  3. Replies: 8
    Last Post: 04-15-2014, 04:35 AM
  4. Copying column w/ VBA takes forever
    By patatvs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2010, 01:42 PM
  5. Replies: 7
    Last Post: 12-19-2008, 10:57 PM
  6. hiding a few rows with a macro takes forever
    By Conor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2007, 06:34 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