VBA to find a value in a pivot table

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hello! I have a UserValue input through an InputBox. I would like to check a little one-column pivot table to see if that value exists in the table. If it does, I will pop up a message. If not, continue with the macro. This is what I wrote, but it's (obviously) not working. At the bolded line, I get the error "Object doesn't support this property or method." I was thinking of turning the pivot table into a range and checking to see if the value exists in the range. Would that be a better way?? Any suggestions are appreciated!

UserValue = InputBox("Enter the username you'd like to add here:")

If ThisWorkbook.Sheets("Inputs").ListObject(1).Value = UserValue Then
ans = MsgBox("The user " & UserValue & " was already added and will be added as a duplicate. Would you like to continue adding?", vbYesNoCancel)
Select Case ans
Case vbYes
ThisWorkbook.Sheets("Inputs").Range("M2").Value = UserValue
Case vbNo
Exit Sub
Case vbCancel
Exit Sub
End Select
End If
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Nevermind with this - I've solved it myself. The code I'm using is:

For Each Row In ThisWorkbook.Sheets("Inputs").PivotTables(1).DataBodyRange.Rows
SearchList = SearchList & Row.Cells(Row.Count, 1) & "|"
Next Row

If 0 = InStr(1, SearchList, UserValue) Then
'left blank to "do nothing" in this case
Else: ans = MsgBox("This username is already included and adding it will create duplicate lines throughout. Would you still like to add this username?", vbYesNo)
End If
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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