VBA run time error 91 - Object varialble or With block variable not set

JanetW

New Member
Joined
May 12, 2016
Messages
21
Hi

I am a relatively new VBA user and am having some trouble with the following code.

I am getting runtime error 91 and the cursor goes to ** when i click ok

What i am basically trying to do is:
I have a file which stores passwords used to save files by team members for tests (for privacy from other team members)
The manager wants to be able to acces these files for review by accessing the relevant pwd for the file

Private Sub CommandButton1_Click()
Dim Filepath As String
Dim PWDSearch As Range
Dim SearchRange As Range
Dim PWD As String


Filepath = Worksheets("Admin").Range("I3")
Set SearchRange = Worksheets("PWDs").Range("E2", Worksheets("PWDs").Range("E1").End(xlDown))
** Set PWDSearch = SearchRange.Find(what:=Filepath, MatchCase:=True, lookat:=xlWhole).Select
PWD = ActiveCell.Offset(0, -2)

If PWDSearch Is Nothing Then
MsgBox "File does not exist"
Else
Workbooks.Open (Filepath), Password:=PWD
End If


End Sub

If anybody can shed some light on what I am doing wrong I would be really grateful!!

Thanks

Janet
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this.
Code:
Private Sub CommandButton1_Click()
Dim PWDSearch As Range
Dim SearchRange As Range
Dim Filepath As String
Dim PWD As String

    Filepath = Worksheets("Admin").Range("I3")
    Set SearchRange = Worksheets("PWDs").Range("E2", Worksheets("PWDs").Range("E" & Rows.Count).End(xlUp))
    Set PWDSearch = SearchRange.Find(what:=Filepath, MatchCase:=True, lookat:=xlWhole)

    If PWDSearch Is Nothing Then
        MsgBox "File does not exist"
    Else
        PWD = PWDSearch.Offset(0, -2).Value

        Workbooks.Open (Filepath), Password:=PWD
    End If

End Sub
 
Upvote 0
Hi

Thanks for your response -I have tried this and it is not locating the filename in the search even though it is in cell e2 and therefore returns the message box saying file does not exist


???

Janet
 
Upvote 0
Janet

Do you mean the value in I3 on Admin matched the value in E2 on the PWDs?
 
Upvote 0
Hi

Yes, however, is was a concatenation so i have added LookIn:=xlValues

to the PWDSearch line and it now works - thank you for your help :)
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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