User Defined Functions returns #VALUE after a macro is run

Agent_V

New Member
Joined
Aug 24, 2007
Messages
19
Hi everyone,

I am using the following User Defined Function to count the number of indents in a cell.

Code:
Public Function IndentCount(rCell As Range) As Long
    IndentCount = rCell.IndentLevel
End Function

However, I am having a problem with the function returning #VALUE after I run the macro code below which is used to hide and unhide certain rows and columns on the worksheet. Only the functions in the rows that have been hidden or unhidden are affected.

Code:
Sub toggleAdminView()

    If Application.UserName = "Vincent Afnan-Murray" Then
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect Password:="Lotus491"
        
        Selection.SpecialCells(xlCellTypeLastCell).Select
        lastRow = Selection.Row
        lastCol = Selection.Column
        Dim adminView As Boolean
        
        Debug.Print "Before rows are hidden " & Range("AI3").Value
        
        Dim r As Integer
        For r = 1 To lastRow
            If Cells(r, 1).Value = "h" Or Cells(r, 1).Value = "sv" Then
                If Cells(r, 1).EntireRow.Hidden = False Then
                    Cells(r, 1).EntireRow.Hidden = True
                Else
                    Cells(r, 1).EntireRow.Hidden = False
                End If
            End If
        Next
        
        Dim c As Integer
        For c = 1 To lastCol
            If Cells(1, c).Value = "h" Or Cells(1, c).Value = "sv" Then
                If Cells(1, c).EntireColumn.Hidden = False Then
                    Cells(1, c).EntireColumn.Hidden = True
                Else
                    Cells(1, c).EntireColumn.Hidden = False
                    adminView = True
                End If
            End If
        Next
            
        Application.ScreenUpdating = True
        
        Debug.Print "Before sheet is protected " & Range("AI3").Value
        If adminView = True Then
            Range("A1").Select
        Else
            With ActiveSheet
                .Protect Password:="Lotus491", UserInterfaceOnly:=True, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True, AllowFiltering:=True
                .EnableOutlining = True
            End With
        End If
    Else
        MsgBox "You are not authorised to access Admin View"
    End If

End Sub

I put a debug into the first loop of this code to print the value in one of the cells containing the function in a hidden row, and it errored out on the first iteration of the loop even though row 1 would not be hidden. I don't understand the correlation between my macro and the udf. If I manually hide and unhide rows it has no effect.

Any help is much appreciated!

Thanks,

V
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top