Error 91 but only when sheet is protected

L

Limey1971

I'm new to VBA programming so please go easy on me.

I have a spreadsheet I've been developing that has multiple comman
buttons, userforms, etc. The problem I have is with one particula
userform that contains two combo boxes and three command buttons
ComboBox1 links to a list of locations, ComboBox2 links to a list o
suppliers. The idea is that you select a location, select the curren
supplier for that location and then click on either "Cancel", "Update
Exit" or "Update & Next". Cancel will close the userform without makin
any changes, Update & Exit will update the sheet and close the use
form, etc.

The whole thing works fine up until the point when I protect th
worksheet, then I start getting run-time error 91 messages when I clic
on either of the update button, all linked cells are formatted to b
unlocked, obviously I'm missing something but I need help figuring ou
what that something is.

The code used in the Update & Exit button is as follows:

Private Sub UpdateSourceAndExit_Click()
Dim Entry1 As String, Entry2 As String
Entry1 = SourceByLocation.LocationComboBox
Entry2 = SourceByLocation.SourceComboBox
Range("N2:N26").Select
Selection.Find(What:=Entry1).Activate
ActiveCell.Offset(0, 1).Select
ActiveCell = Entry2
Sheets("Sheet1").Cells(1, 1).Select
SourceByPlant.Hide
End Sub

Where:
SourceByLocation = UserForm
LocationComboBox = List of available locations
SourceComboBOx = List of available sources
N2:N26 = List of available locations

Basically the above code looks up the value in LocationComboBox i
range N2:N26, and pastes the value in SourceComboBox into the cel
immediately to the right.

The run-time error occurs in the line that starts with Selection.Find

I think I understand why it's doing it (the combobox is a variable?
but I don't know how to fix it!

Please help if you can.

I'm sure my code writing is primative and messy but as I said, I'm ne
to this and besides, it works great so long as the sheet isn'
protected.

TI
 
P

Paul Robinson

Hi
Before a Select, you normally need to activate the sheet that contains
the range you are selecting from. So you need to put in

worksheets("Whatever").Activate

before the select. A better idea is not to select at all and replace
this

worksheets("Whatever").Activate
Range("N2:N26").Select
Selection.Find(What:=Entry1).Activate
ActiveCell.Offset(0, 1).Select
ActiveCell = Entry2

with this

worksheets("Whatever").Range("N2:N26").Find(What:=Entry1).Activate
ActiveCell.Offset(0, 1).Value = Entry2

I can't see why a password protected sheet would cause a problem. This
may be a coincidence?

regards
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top