Consulting

Results 1 to 4 of 4

Thread: How can I speed this up?

  1. #1

    Question How can I speed this up?

    Hello, I have a closed workbook that I am treating as a database. I run sql queries for searches and return results to my searching workbook. I used the code found here: http://www.erlandsendata.no/english/...nvbadacwbdbado

    It works great! and is very fast. However the next step is to look at the results, then based on criteria, look in a folder to see if a file exists. If it does, then create a hyperlink to that file. This part works as well, but is quite slow. Is there a way for me to speed this part up. Any help would be greatly appreciated. My hyperlink code is below.

    P.S. I search for files with any extension ".*", then I pull the extension in my function (MyExt),to place in the hyperlink. This allows me to link to .doc,.xls,.pdf etc. Being new at VBA in general, I thought that was a pretty good solution. But if it is slowing me down I'll change it.

    [vba]Public MyExt As String

    Sub Links()
    Dim i As Integer
    Dim MyFile As String
    Dim MyFolder As String
    Dim MyLink As String
    Dim MyDir As String

    MyDir = ("\\server\shr\")

    For i = 8 To 10000

    If Cells(i, 1) <> "" Then
    MyFile = Cells(i, 1).Value
    MyFolder = "procedures\"
    Else: Exit Sub
    End If

    If FileThere(MyDir & MyFolder & MyFile & ".*") Then
    MyLink = MyDir & MyFolder & MyFile & MyExt
    Cells(i, 1).Hyperlinks.Add Anchor:=Cells(i, 1), _
    Address:=MyLink, TextToDisplay:=MyFile
    Application.EnableEvents = True
    End If

    If Cells(i, 5) <> "" Then
    MyFile = Cells(i, 5).Value
    MyFolder = "worksheets\"

    If FileThere(MyDir & MyFolder & "\" & MyFile & ".*") Then
    MyLink = MyDir & MyFolder & MyFile & MyExt
    Cells(i, 5).Hyperlinks.Add Anchor:=Cells(i, 5), _
    Address:=MyLink, TextToDisplay:=MyFile
    Application.EnableEvents = True
    End If

    End If

    Next i

    End Sub


    Public Function FileThere(FileName As String) As Boolean
    FileThere = (Dir(FileName) > "")
    MyExt = Right((Dir(FileName)), 4)
    End Function[/vba]
    Last edited by mvidas; 12-29-2005 at 12:13 PM. Reason: Added vba tags [vba] before the code and [/vba] after

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    Your speed will be due to using a loop (For each) to check your data. Some tricks to consider:
    -Apply an autofilter to the column so that only valid data is presented, then loop through the visible rows (use the specialcells method). This will allow you to avoid checking rows for this kind of thing: If Cells(i, 1) <> "" Then

    -Make your loop a little more dynamic. Instead of hard coding 10000 rows, many of which may be empty and just consume time, try to make your loop go from the first row of data to the last row. Something like:
    [vba]For i = 8 to Activesheet.Range("A" & Activesheet.Rows.Count).End(xlup).Row[/vba]

    Let us know if you need any help applying it.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Using an index variable (i) will also slow it down, try using something like this (untested)[vba]Option Explicit

    Public MyExt As String

    Sub Links()
    Dim Target As Range
    Dim MyFile As String
    Dim MyFolder As String
    Dim MyLink As String
    Dim MyDir As String

    MyDir = ("\\server\shr\")

    For Each Target In Range("A1", Range("A" & Rows.Count).End(xlUp))

    If Target = Empty Then
    Exit Sub
    Else
    MyFile = Target
    MyFolder = "procedures\"
    End If

    If FileThere(MyDir & MyFolder & MyFile & ".*") Then
    MyLink = MyDir & MyFolder & MyFile & MyExt
    Target.Hyperlinks.Add Anchor:=Target, _
    Address:=MyLink, TextToDisplay:=MyFile
    Application.EnableEvents = True
    End If

    If Target.Offset(0, 4) <> "" Then
    MyFile = Target.Offset(0, 4)
    MyFolder = "worksheets\"

    If FileThere(MyDir & MyFolder & "\" & MyFile & ".*") Then
    MyLink = MyDir & MyFolder & MyFile & MyExt
    Target.Offset(0, 4).Hyperlinks.Add Anchor:=Target.Offset(0, 4), _
    Address:=MyLink, TextToDisplay:=MyFile
    Application.EnableEvents = True
    End If

    End If

    Next

    End Sub


    Public Function FileThere(FileName As String) As Boolean
    FileThere = (Dir(FileName) > "")
    MyExt = Right((Dir(FileName)), 4)
    End Function[/vba]

    EDIT: Also, try using Application.ScreenUpdating = False near the top of your code and Application.ScreenUpdating = True as the penultimate line.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4

    Thumbs up Solved:

    Thank you both for your help. It runs about 25% faster now. Excellent.

Posting Permissions

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