+ Reply to Thread
Results 1 to 12 of 12

User Defined Function Fails when I run a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    User Defined Function Fails when I run a Macro

    I have borrowed a UDF from another site. It has the simple function of finding the text color in a cell. It works perfectly when I use it as part of a formula. Here is the UDF:

    Function txtColor(rng As Range)
    txtColor = rng.Font.ColorIndex
    End Function
    I have a macro that filters cells based on the a formula using this UDF. When I filter using this formula by hand, the filter process works fine. But when I use my macro -- which filters, then copies, then pastes, the formula no longer works. All cells containing that formula transform into #VALUE!.

    Thank you in advance for any suggestions.

    Scott
    Last edited by scott altman; 09-11-2017 at 02:20 PM. Reason: Missing Code Tag

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    You can autofilter filter by color without using any functions.

    Please take a few minutes to read the forum rules and then edit your post to add CODE tags. While you're there, you might want to read the part about "How to get your questions answered quickly"

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    Re: User Defined Function Fails when I run a Macro

    I apologize about the code tags, which I think I have now corrected.

    I appreciate you pointing out that I can autofilter by text color. Unfortunately, Excel's autofilter treats any cell that contains black text as being black even if it also contains red text as well. My sheet needs to treat cells with all black text as black and cells with some black text, but also some red or blue text, as not black. This is why I turned to the txtcolor function above. It identifies mixed color cells as not black.

    This is my first post to the forum. So if there are other steps I can take to help get my question answered, I will be grateful for your guidance.

    Scott

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    Yes -- as the rules suggest, post a workbook that illustrates the problem.

  5. #5
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    Re: User Defined Function Fails when I run a Macro

    Thank you for helping me. I believe that I have now uploaded the file. The macro is in module 1. The UDF is in module 3.
    Attached Files Attached Files

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    Try this code instead:

    Sub Macro2()
      Dim wksScr        As Worksheet
      Dim i             As Long
    
      Set wksScr = Worksheets("Script")
    
      Application.Calculation = xlCalculationManual
      
      With wksScr
        For i = 2 To Worksheets.Count
          Worksheets(i).Range("A2:F2000").ClearContents
    
          With .Range("A9", .Cells(.Rows.Count, "E"))
            .AutoFilter Field:=2, Criteria1:=Worksheets(i).Name & "."
            .AutoFilter Field:=5, Criteria1:="Yes"
          End With
    
          .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Worksheets(i).Range("A2")
        Next i
        .AutoFilterMode = False
      End With
      
      Application.Calculation = xlCalculationAutomatic
    End Sub

  7. #7
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    Re: User Defined Function Fails when I run a Macro

    Thank you Shg for your kindness. I am fairly new to VBA (as I am sure is obvious). So I may have incorporated your code into my macro incorrectly. It seems to run most of the way. But near the end, I get a message: "Run Time Error 91: Object variable with Block variable not set"

    When I press debug, it takes me to this line

    .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Worksheets(i).Range("A2")
    In case it helps, I have attached the worksheet showing how I tried to incorporate your code.

    I apologize that my limits are making it hard for you to help me.

    Scott
    Attached Files Attached Files
    Last edited by scott altman; 09-11-2017 at 06:21 PM. Reason: Atttachment Missing

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    There's no "incorporating" to do. Replace all of your code with my code.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    See attached.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    Re: User Defined Function Fails when I run a Macro

    Thank you. This is wonderful -- magic to me. But your fix has introduced a new problem that I am not sure how to diagnose.

    When I change the font in a cell from black to red (or any other color), my UDF recognizes the change. But now when I change any font to black, my UDF categorizes the color as -4105 (rather than as color 1, which it had been recognizing as black).

    Thank for any advice.

  11. #11
    Registered User
    Join Date
    03-29-2016
    Location
    Los Angeles
    MS-Off Ver
    2016
    Posts
    12

    Re: User Defined Function Fails when I run a Macro

    I have solved this last problem. When I used the default font -- which appears to be black -- my UDF did not recognize it as black. So long as I only use the color black, it seems to function,

    Thank you for your terrific help and for your kindness.

    I think my issues are now solved.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User Defined Function Fails when I run a Macro

    You're welcome.

+ 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] User-defined type not defined on Copy to Clipboard Macro
    By vamosj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2015, 11:31 AM
  2. A Macro to Apply the PROPER Function, whislt Excluding User Defined Words
    By HangMan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2015, 12:40 PM
  3. Macro will not allow conditional formatting with a user-defined function
    By Med_MV in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-16-2013, 05:28 PM
  4. user defined macro to replace certain characters in a string based on user input
    By whatappears in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2012, 06:25 PM
  5. Access connection works in Macro, not in user-defined Function
    By Steve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-27-2005, 10:05 AM
  6. How to: User Form to assign a user defined range to a macro variab
    By TrevTrav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2005, 04:06 PM
  7. [SOLVED] Macro user defined function row information
    By bj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2005, 11:06 AM

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