+ Reply to Thread
Results 1 to 4 of 4

Run Time Error 13 Type Mismatch

  1. #1
    ExcelMonkey
    Guest

    Run Time Error 13 Type Mismatch

    I am wrapping a function around a public variable called Cell. When I run
    the macro, the code breaks with a Run Time Error 13. However when I hit F8
    it allows me to step through it. I can't figure out why its breaking in the
    first place. Do I have to further dimenion it within the function?


    Public Cell As Range
    Sub Main()
    CellIsHidden(Cell)
    End Sub


    Public Function CellIsHidden()
    If Cell.Parent.Protect = True Then
    If Cell.FormulaHidden = True Then
    CellIsHidden = True
    End If
    End If
    End Function


    Thanks


  2. #2
    Jan Karel Pieterse
    Guest

    Re: Run Time Error 13 Type Mismatch

    Hi ExcelMonkey,

    > I am wrapping a function around a public variable called Cell. When I run
    > the macro, the code breaks with a Run Time Error 13. However when I hit F8
    > it allows me to step through it. I can't figure out why its breaking in the
    > first place. Do I have to further dimenion it within the function?


    1. Don't use Cell for a variable name, better to use some name that describes
    what it stands for: rActiveCell as Range
    2. You don't assign Cell to any object in your code, e.g.
    Set rActiveCell=ActiveCell

    Regards,

    Jan Karel Pieterse
    Excel MVP
    http://www.jkp-ads.com


  3. #3
    ExcelMonkey
    Guest

    Re: Run Time Error 13 Type Mismatch

    Sorry. I did not post all code. Its quite extensive. I am effecitvely
    looping through cells in a sheet. The code will work if - upon passing it to
    the function - I change all references to "Cell" in the Function as "rng" and
    put "rng As Range" in the brackets of the function (i.e. Public Function
    CellIsHidden(rng as Range)). This is how I originally had it.

    Public Function CellIsHidden(rng As Range)
    If rng.Parent.Protect = True Then
    If rng.FormulaHidden = True Then
    CellIsHidden = True
    End If
    End If
    End Function


    But after reveiwing I thought that since "Cell" is a public variable, I
    could probably refer to it using its public name instead assigning it a new
    variable name within the Function. But this seems to cause problems and
    breaks the code.


    Public Cell As Range
    Sub Main()

    For Each Cell in sh.UsedRange
    CellIsHidden(Cell)
    NExt
    End Sub


    Public Function CellIsHidden()
    If Cell.Parent.Protect = True Then
    If Cell.FormulaHidden = True Then
    CellIsHidden = True
    End If
    End If
    End Function





    "Jan Karel Pieterse" wrote:

    > Hi ExcelMonkey,
    >
    > > I am wrapping a function around a public variable called Cell. When I run
    > > the macro, the code breaks with a Run Time Error 13. However when I hit F8
    > > it allows me to step through it. I can't figure out why its breaking in the
    > > first place. Do I have to further dimenion it within the function?

    >
    > 1. Don't use Cell for a variable name, better to use some name that describes
    > what it stands for: rActiveCell as Range
    > 2. You don't assign Cell to any object in your code, e.g.
    > Set rActiveCell=ActiveCell
    >
    > Regards,
    >
    > Jan Karel Pieterse
    > Excel MVP
    > http://www.jkp-ads.com
    >
    >


  4. #4
    Jan Karel Pieterse
    Guest

    Re: Run Time Error 13 Type Mismatch

    Hi ExcelMonkey,

    > But after reveiwing I thought that since "Cell" is a public variable, I
    > could probably refer to it using its public name instead assigning it a new
    > variable name within the Function. But this seems to cause problems and
    > breaks the code.


    Why not like this (no need for a public variable: pass as argument):

    Sub Main()
    Dim rCell As Range

    For Each rCell in sh.UsedRange
    CellIsHidden rCell
    Next
    End Sub

    Public Function CellIsHidden(rCell As Range)
    If rCell.Parent.Protect = True Then
    If rCell.FormulaHidden = True Then
    CellIsHidden = True
    End If
    End If
    End Function

    Regards,

    Jan Karel Pieterse
    Excel MVP
    http://www.jkp-ads.com


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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