+ Reply to Thread
Results 1 to 8 of 8

I created a function that runs too slow! Can you help me?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-23-2017
    Location
    MEXICO
    MS-Off Ver
    2013
    Posts
    2

    Exclamation I created a function that runs too slow! Can you help me?

    Hi everyone!

    I just created a FUNCTION to find the column number of a cell in other BOOK depending of two variables. For 100 cells it takes 3 minutes to calculate!

    THIS IS MY CODE:
         
    Option Explicit
    
    
    Function ENCUENTRASEM(takt As Double, wagon As String) As Double
    
    Dim col As Integer
    Dim row As Integer
    Dim res As Integer
    
    Dim NumRows As Integer
    Dim NumCols As Integer
    NumRows = 370
    NumCols = 148
    
    res = -1
    For row = 8 To NumRows
        For col = 5 To NumCols
        
            If Application.Workbooks("SHM_170720_0300_TSH_CHU_Takt-Schedule_V049").Worksheets("3.TAKT SCHEDULE").Cells(row, col).Value = wagon And Application.Workbooks("SHM_170720_0300_TSH_CHU_Takt-Schedule_V049").Worksheets("3.TAKT SCHEDULE").Cells(row, 1).Value = takt Then
                res = col
                Exit For
            End If
            
        Next col
        
            If res <> -1 Then
                Exit For
            End If
    Next row
    
    ENCUENTRASEM = res
    
    End Function
    Last edited by LuisEngineer; 08-23-2017 at 07:39 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,526

    Re: I created a function that runs too slow! Can you help me?

    Deleted while OP complied to Forum Rules.
    Last edited by bakerman2; 08-24-2017 at 11:48 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: I created a function that runs too slow! Can you help me?

    I would suggest that you come up with a better searching algorithm. As programmed, it looks like a very inefficient "start in the upper left corner and go across and down until it finds the cell that matches the two criteria". I think a better search algorithm could improve the performance of this.

    First thing I notice, it appears that you are only going to return a value from the row which has takt in the leftmost column. As presently programmed, your search algorithm wastes a lot of time/effort searching across rows that do not have takt in the first column of that row. I would structure the algorithm so that it first searches for takt in the first column. Once the algorithm knows which row takt is in, then look for the column in that row that contains wagon.

    Does that make sense? It appears that you already know how to code a basic linear search algorithm, so can I assume you can rearrange your loops to change the search order?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    08-23-2017
    Location
    MEXICO
    MS-Off Ver
    2013
    Posts
    2

    Re: I created a function that runs too slow! Can you help me?

    Hey! Thanks for your help, I've been trying to do what you said but I can't get it. Can you help me showing how would you do the arrange please?

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: I created a function that runs too slow! Can you help me?

    I'd be curious what you tried. You seem to know For...Next loops and Block If's and such, so it seems like it should have been fairly straightforward. I would have expected like this code snippet:
    For row=8 to numrows
    If rangereference.cells(row,1).value=takt then exit For
    next row
    For col=5 to numcols
    if rangereference.cells(row,col).value=wagon then exit for
    next col
    'process result

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,526

    Re: I created a function that runs too slow! Can you help me?

    I would even lose the column search.

    Function ENCUENTRASEM(takt As Double, wagon As String) As Double
        Dim NumRows As Long, NumCols As Long
        Dim col As Long, row As Long, x
    
        NumRows = 370
        NumCols = 148
        col = -1
    
        For row = 8 To NumRows
            If Workbooks("SHM_170720_0300_TSH_CHU_Takt-Schedule_V049").Worksheets("3.TAKT SCHEDULE").Cells(row, 1).Value = takt Then
                x = Application.Match(wagon, Workbooks("SHM_170720_0300_TSH_CHU_Takt-Schedule_V049").Worksheets("3.TAKT SCHEDULE").Cells(row, 5).Resize(, 148), 0)
                If Not IsError(x) Then col = x + 4: Exit For
            End If
        Next row
    
        ENCUENTRASEM = col
    End Function
    Last edited by bakerman2; 08-24-2017 at 02:08 PM.

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

    Re: I created a function that runs too slow! Can you help me?

    I am sorry guys but why would anyone search through a whole lot of cells looking for specific text?

    Surely you would use the Find function?

    What am I missing?

    
    Function ENCUENTRASEM(takt As Double, wagon As String) As Double
    
    res = -1
    
        With Application.Workbooks("SHM_170720_0300_TSH_CHU_Takt-Schedule_V049").Worksheets("3.TAKT SCHEDULE").Range(Cells(8, 5), Cells(370, 148))
        
            Set rngFind = .Find(wagon, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Do
                    If Cells(rngFind.row, 1) = takt Then res = rngFind.row
                    
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress And res = -1
            End If
        End With
    
    ENCUENTRASEM = res
    
    End Function
    Last edited by mehmetcik; 08-24-2017 at 02:44 PM.
    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.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: I created a function that runs too slow! Can you help me?

    mehmetcik: I did not suggest the .Find method because I was not sure how well it would work for this kind of 2D lookup, especially if there are multiple instances of wagon in the table and the goal of the lookup is to find which instance of wagon is in the takt row. The OP really hasn't given enough information for me to suggest how to use .Find in this situation (though that could easily be because I don't use .Find enough to see how to use it here).

    For that matter, why use VBA at all. I expect there is a way to combine INDEX() and MATCH() functions to perform this lookup without VBA. That could also help, just because VBA requires a certain overhead that can contribute to poor performance. I guess we are just working from the OP's starting place.

+ 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. VBA code runs slow
    By jamfz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2016, 12:25 PM
  2. VBA runs slow locally, runs fine when connected remotely
    By jbzy324 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-01-2015, 10:05 PM
  3. VBA Code runs too slow
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2012, 07:19 AM
  4. Replies: 1
    Last Post: 07-15-2011, 01:24 AM
  5. Spreadsheet runs slow
    By jmoffett in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2008, 02:47 PM
  6. [SOLVED] Macro runs slow
    By Sandy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-20-2005, 12:05 PM
  7. macro runs slow
    By nanomiter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2005, 01:06 PM

Tags for this Thread

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