简体   繁体   中英

EXCEL VBA issue returning #VALUE! instead of false through pivot table

I'm new to VBA programming and I've run into a weird issue. So I have a pivot table with 3 types of rows ("users", "Full Name", "Permissions").

I wrote a program to look for a certain name x in the PivotTable , and once it's found the name it looks at the following "permissions" and returns "True" if they 'Have Permissions'. Now the case when it returns "True" works fine, but for some reason every time it fails instead of returning false like I thought it would it returns #VALUE!, does anyone know why?

Public Function Dependency(x As String) As String

Dim rngRow As Range
Dim pt As PivotTable
Dim pf2Value As String
Dim pf3Value As String

pf2Value = "False"
pf3Value = "False"
Set pt = ActiveSheet.PivotTables("PivotTable1")

For Each rngRow In pt.RowRange        
    If pf2Value = "True" And rngRow.PivotField.Name = "Permissions" Then
        If rngRow.Value = "Has Permissions" Then
                pf3Value = "True"
        End If
    End If
    If pf2Value = "True" And pf3Value = "True" And rngRow.PivotField.Name <> "Permissions" Then Exit For
    If rngRow.PivotField.Name = "Full Name" And rngRow.Value = x Then
        pf2Value = "True"
    End If
    If rngRow.PivotField.Name = "Full Name" And rngRow.Value <> x Then
        pf2Value = "False"
    End If
Next rngRow
Dependency = pf3Value

End Function

If I do this it does return false:

Public Function Dependency(x As String) As String

Dim rngRow As Range
Dim pt As PivotTable
Dim pf2Value As String
Dim pf3Value As String

pf2Value = "False"
pf3Value = "False"
Set pt = ActiveSheet.PivotTables("PivotTable1")

For Each rngRow In pt.RowRange
Next rngRow
Dependency = pf3Value

End Function

the issue starts when I add this line:

Public Function Dependency(x As String) As String

Dim rngRow As Range
Dim pt As PivotTable
Dim pf2Value As String
Dim pf3Value As String

pf2Value = "False"
pf3Value = "False"
Set pt = ActiveSheet.PivotTables("PivotTable1")

For Each rngRow In pt.RowRange
    If pf2Value = "True" And rngRow.PivotField.Name = "Permissions" Then
    End If
Next rngRow
Dependency = pf3Value

End Function

Try the code below (explanation inside the code as comments):

Public Function Dependency Code

Option Explicit

Public Function Dependency(x As String) As String

Dim pt          As PivotTable
Dim ptNameFld   As PivotField
Dim ptPermFld   As PivotField
Dim C           As Range

' set the Pivto-Table object, modify "Sheet1" with your sheet's name
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1")

' set the Pivot Field "Full Name"
Set ptNameFld = pt.PivotFields("Full Name")

' set the Pivot Field "Permissions"
Set ptPermFld = pt.PivotFields("Permissions")

' loop through all rows in "Full Name" data range
For Each C In ptNameFld.DataRange.Rows
    If C.Value2 Like x Then ' if the name matches "x"
        ' for DEBUG Only
        'Debug.Print ptPermFld.DataRange(C.Row - ptPermFld.LabelRange.Row)

        Dependency = ptPermFld.DataRange(C.Row - ptPermFld.LabelRange.Row) ' get the value of "Permission", of the same row
        Exit For
    End If
Next C

End Function

Sub TestDependency Code (to test the Function)

Sub TestDependency()

MsgBox Dependency("John Doe")

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM