+ Reply to Thread
Results 1 to 5 of 5

How-to use function stored in another workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    05-16-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    How-to use function stored in another workbook

    I have a worksheet that has cells coloured via conditional formatting. I need to count the various cells by colour - after much searching & experimenting I've got the two functions below to work with the following formula :-
    =CountCellsByColor(range, color code)

    Problem is that I need to do this in many different workbooks. All my other macros are housed in a central workbook so that I don't have to do much more than open the "storage" workbook, the file to be worked on & then run the appropriate macro.

    My question is how do I use the two functions below via that central workbook ??
    What changes do I need to make to the functions (if any) & how would I "execute" the formula via the macro or would it be as simple as Range("A1").formula="=CountCellsByColor(range, color code)" ??

    Many thanks


    Function GetCellColor(xlRange As Range)
        Dim indRow, indColumn As Long
        Dim arResults()
     
        Application.Volatile
     
        If xlRange Is Nothing Then
            Set xlRange = Application.ThisCell
        End If
     
        If xlRange.Count > 1 Then
          ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
           For indRow = 1 To xlRange.Rows.Count
             For indColumn = 1 To xlRange.Columns.Count
               arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color
             Next
           Next
         GetCellColor = arResults
        Else
         GetCellColor = xlRange.Interior.Color
        End If
    End Function
     
     
    Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
        Dim indRefColor As Long
        Dim cellCurrent As Range
        Dim cntRes As Long
     
        Application.Volatile
        cntRes = 0
        indRefColor = cellRefColor.Cells(1, 1).Interior.Color
        For Each cellCurrent In rData
            If indRefColor = cellCurrent.Interior.Color Then
                cntRes = cntRes + 1
            End If
        Next cellCurrent
     
        CountCellsByColor = cntRes
    End Function

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,945

    Re: How-to use function stored in another workbook

    create a personal.xlsb file and put all UDFs in that file. Here is a link for a complete explanation of Personal.xlsb.

    https://www.rondebruin.nl/win/personal.htm
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    05-16-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    Re: How-to use function stored in another workbook

    Thanks Alan - didn't know about those files.

    Issue is that, unfortunately, I'm not always the person running the routines hence looking at ways so that anyone can run the macros which incorporate the functions.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,945

    Re: How-to use function stored in another workbook

    Need to create Personal.xlsb on PC of each user. It is PC centric not network centric.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,858

    Re: How-to use function stored in another workbook

    I like to store my frequently used UDFs in add-ins. As with a personal.xls file, it still needs to be stored by each user/on each PC. Installing add-ins makes UDFs available to all open workbooks without the need to preface the function call with the file name (though the file/path are implicitly prefaced by Excel, so things get broken if the add-in is moved). http://www.ozgrid.com/VBA/excel-add-in-create.htm
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ 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. [SOLVED] Insert Image into Mail from Folder in which Workbook is stored
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2016, 02:44 PM
  2. Correct syntax to add a workbook name that is stored in a variable into a formula
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-25-2014, 03:46 PM
  3. [SOLVED] Workbook Protection - password stored in named range
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-07-2013, 04:52 AM
  4. Replies: 0
    Last Post: 04-16-2013, 10:27 AM
  5. Replies: 1
    Last Post: 09-15-2011, 04:52 AM
  6. Locating the directory a workbook is stored in
    By Graham Whitehead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2006, 04:50 AM
  7. [SOLVED] Refer to picture stored in workbook
    By JakeyC in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-12-2006, 06:30 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