error

  • Thread starter Thread starter ranswert
  • Start date Start date
R

ranswert

When I run the following code from a userform I get a 'subscript out of range
error'. What am I doing wrong?

Private Sub CommandButton1_Click()
Dim costitem As String
unprotectsheet
costitem = ComboBox1.Value
If costitem <> "" Then
Range("costitemrng").Find(what:=costitem, lookat:=x1whole,
LookIn:=x1values, searchorder:=x1bycolumns).Activate
'selectcostitem.Hide
'Editcostitem.Show
End If
selectcostitem.Hide
protectsheet
End Sub

Thanks
 
2 possibilities:
- try ComboBox1.Text instead
- have you tested for null? IsNull(ComboBox1.Value)
 
replace:
Range("costitemrng")
with:
Range(costitemrng)

because

costitemrng should already contain a string
 
I tried changing to "costitem=combobox1.text" and I still got the same error.
How do i do the "IsNull(ComboBox1.Value)" code and what does it do?
Thanks
 
I did what you said and I still got the same error. "costitemrng" is a named
range on my spreadsheet.
Any ideas on what else might be wrong? or a better way to look up a string
and select the cell that contains the string?
Thanks
 
Another way is:

Dim rng As Range (at top of Sub)
-
-
For Each rng In Range("costitemrng").Cells
If InStr(rng.Value, costitem) <> 0 Then
rng.Select
Exit For
End If
Next
 
That worked
Thank You

Smallweed said:
Another way is:

Dim rng As Range (at top of Sub)
-
-
For Each rng In Range("costitemrng").Cells
If InStr(rng.Value, costitem) <> 0 Then
rng.Select
Exit For
End If
Next
 

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

Similar Threads

find 6
Instr 2
Listbox problem agaie 3
Resize to last entry in row and copy to sheet 1 3
conso macro 0
Prevent UserForm from displaying 3
Search cells code not working 8
Screen Flashing 6

Back
Top