+ Reply to Thread
Results 1 to 7 of 7

How to speed up VBA custom function?

Hybrid View

ChuxiLiu How to speed up VBA custom... 11-30-2021, 03:46 PM
6StringJazzer Re: How to speed up VBA... 11-30-2021, 04:05 PM
ChuxiLiu Re: How to speed up VBA... 11-30-2021, 04:54 PM
MrShorty Re: How to speed up VBA... 11-30-2021, 06:40 PM
ChuxiLiu Re: How to speed up VBA... 11-30-2021, 07:59 PM
6StringJazzer Re: How to speed up VBA... 11-30-2021, 10:53 PM
ChuxiLiu Re: How to speed up VBA... 11-30-2021, 11:31 PM
  1. #1
    Registered User
    Join Date
    11-12-2019
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Post How to speed up VBA custom function?

    Hello there,

    I am struggling with a custom function I wrote in the VBA. The problem is that this function create new Excel.Application instance inside it that will open excel workbook from another directory (to extract some values). Although it works fine, it takes quite some seconds to extract these values. I am wondering, is there a method that can drastically speed up this function? The excel has many formula that calls this function, and it takes roughly 1 hr to save the workbook (due to formula auto-calculate and value updating). I am attaching this function below for reference. Thank you very much!

    Public Function Ext(Ref As String) As String
    
        Dim excelApp As New Excel.Application
        excelApp.Visible = False
        Dim book As Workbook
        Dim xSht As Worksheet
        Dim Val As String
        
        openPos = InStr(Ref, "'")
        closePos = InStr(Ref, "]")
        bookName = Mid(Ref, openPos + 1, closePos - openPos - 1)
        bookName = Replace(bookName, "'", "")
        bookName = Replace(bookName, "[", "")
        bookName = Replace(bookName, "]", "")
        openPos = InStr(Ref, "]")
        closePos = InStr(Ref, "!")
        sheetName = Mid(Ref, openPos + 1, closePos - openPos - 1)
        sheetName = Replace(sheetName, "'", "")
        charCt = Len(Ref)
        cellName = Right(Ref, charCt - closePos)
        Set book = excelApp.Workbooks.Open(bookName)
        Set xSht = book.Worksheets(sheetName)
        Val = xSht.Range(cellName).Value
        book.Close savechanges:=False
        excelApp.Quit
        Ext = Val
    
    End Function

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: How to speed up VBA custom function?

    This will run once for every formula it is used in, so that is going to take a lot of time.

    First, it is not necessary to create a new instance of Excel to do this. That adds a lot of overhead.

    Second, it is probably not necessary to actually open the Excel file. You can build a formula that references the cell in the closed workbook, instead of opening the file to retrieve the value.

    Please give examples of the formulas where you use this function in your worksheet and I can be more specific about a solution.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-12-2019
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: How to speed up VBA custom function?

    Thank you 6StringJazzer for the help! Below I have attached an example where I used this function (some sensitive information are blurred out, they are workbook/sheet name in the formula). In addition, note that all highlighted cyan cells also use this Ext function. How can I implement the 2 methods you mentioned through this example? I really appreciate your assistance!

    Attachment 757518

    Attachment 757519
    Attached Images Attached Images
    Last edited by ChuxiLiu; 11-30-2021 at 07:57 PM.

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

    Re: How to speed up VBA custom function?

    Your attachments seem to have failed to attach correctly. Be sure to follow the instructions in the yellow banner at the top of the page when attaching files (including pictures).

    I'm going to agree with 6StringJazzer -- creating Excel instances and opening and closing the files are major "slow" steps. I expect that a big part of speeding this up will be figuring out ways to accomplish the same thing without opening and closing files. In addition to getting your uploads to work correctly, I think it might be important to understand the overall goal of your spreadsheet(s), how you intend to interact with them, and what limitations and restrictions (and any other programming requirements) for the project. Things that come to my mind include -- is it necessary for the files to be closed? Is it necessary to keep the different files separate, or could the be compiled into a single, good database? Are you required to use VBA, or would you entertain a Power Query type of solution (or some other solution that involves completely revamping and restructuring your data storage and data querying operations)?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    11-12-2019
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: How to speed up VBA custom function?

    Hi MrShorty,

    Due to this project management, I am afraid I can only use the VBA option. The other options you mentioned are not available.
    I am also thinking if there is a workaround without opening the workbook, but I searched this before and couldn't find any useful information.

    BTW, I attached the screenshot pictures in the previous reply, see if you can view them now.

    Deeply appreciate the help!

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: How to speed up VBA custom function?

    There is not enough information to provide a completed solution. In particular I need to know how you determined that in the formula you showed, the file name was located in cell A2. You did not show column A in your picture. If you have a formula like this in every cyan cell you need to explain how the applicable file name is determined for each cell.

    Similarly you have B6 and a sheet name hardcoded into the string. Is this different for different cells? If so, what determines them?

    Here is the strategy but I can't give a complete solution without more information. This uses the same file name, sheet name, and cell range for every formula, just as an example:

    Public Sub PopulateFormulas()
    
        Dim BookName As String
        Dim CellName As String
        Dim Cell As Range
        
        BookName = [A2]
        SheetName = "Blurred Out Sheet" ' as shown in example
        CellName = "B6" ' as shown in example
        
        For Each Cell In Range("B6,B8:M9,B10:M11,B12,D13:E13,G13:H13,J13:K13,M13,B14,E14,H14,K14")
           Cell.Formula = "=" & "'C:\Users\cxliu\Desktop\HARRY\[" & BookName & ".xlsx]" & SheetName & "'!" & CellName
        Next Cell
        
    End Sub
    This creates formulas that refer directly to the external file, which are effective even if the file is closed. This will be 100 times faster than what you are doing now.
    Last edited by 6StringJazzer; 11-30-2021 at 11:43 PM. Reason: removed leftover line of code for CellName

  7. #7
    Registered User
    Join Date
    11-12-2019
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: How to speed up VBA custom function?

    Thank you 6StringJazzer!

    I think your solution is a perfect workaround and it inspired me to modify my code! I will try it.

+ 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. How to avoid cell SELECTION but still format cells to speed up the macro running speed
    By BeefyBerts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2018, 08:18 AM
  2. [SOLVED] How to speed up this index match function
    By fluffyvampirekitten in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-27-2015, 03:50 AM
  3. Speed issues with VBA function
    By zemigsan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2013, 10:24 PM
  4. Help Me Speed up this User Defined Function
    By Excelcb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2012, 04:26 PM
  5. Speed up a macro which uses instr function
    By drcheaud in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-17-2011, 12:07 PM
  6. Query-Style Custom Function needs speed boost!
    By cyber553 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-24-2009, 03:19 PM
  7. Custom function slowing workbook speed
    By PIPPIBOOKS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-04-2008, 05:30 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