Instr

R

ranswert

I have the following code:

Private Sub CommandButton1_Click()
Dim a2 As String
Dim b2 As String
Dim costitem As String
Dim rng As Range
Dim codeno As String
Dim ctr As Integer
Dim b1 As String
Dim a1 As String
a1 = Editcostitem.Label2.Caption
b1 = Editcostitem.Label6.Caption
a2 = Editcostitem.TextBox1.Value
b2 = Editcostitem.TextBox2.Value
ctr = 0
If a1 = a2 And b1 = b2 Then
MsgBox ("No changes selected for '" & b1 & " " & a1 & "' cost item")
Else
If b1 = b2 Then
For Each rng In Range("costitemrng").Cells
If InStr(rng.Value, a2) <> 0 Then
ctr = ctr + 1
End If
Next
If ctr = 0 Then
ActiveCell = a2
Else
MsgBox ("Cost Item '" & a2 & "' already exists")
End If
End If
If a1 = a2 Then

For Each rng In Range("costcoderng").Cells
If InStr(rng.Value, b2) <> 0 Then
ctr = ctr + 1
End If
Next
MsgBox (ctr)
If ctr = 0 Then
ActiveCell.Offset(0, -1) = b2
Else
MsgBox ("Cost Code '" & b2 & "' already exists")
End If
End If
If a1 <> a2 And b1 <> b2 Then
For Each rng In Range("costitemrng").Cells
If InStr(rng.Value, a2) <> 0 Then
ctr = ctr + 1
End If
Next
If ctr = 0 Then
ActiveCell = a2
Else
MsgBox ("Cost Item '" & a2 & "' already exists")
End If
For Each rng In Range("costcoderng").Cells
If InStr(rng.Value, b2) <> 0 Then
ctr = ctr + 1
End If
Next
If ctr = 0 Then
ActiveCell.Offset(0, -1) = b2
Else
MsgBox ("Cost Code '" & b2 & "' already exists")
End If
End If
End If



Editcostitem.Hide

End Sub

I have this code to not allow values in a range of cells to match. I think
what I wrote it counts any matching characters in the string. What do I need
to do different?
Thanks
 
D

Dave Peterson

Instead of looping through the cells, you could use .find() or if the
costitemrng/costcoderng are a single column, you could use application.match().

Dim FoundCell as range
with worksheets("Somesheetnamehere").range("costitemrng")
set foundcell = .cells.find(what:=a2, _
after:=.Cells(.Cells.Count), _
lookat:=xlWhole, _
LookIn:=xlValues, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
end with

if foundcell is nothing then
msgbox "not found in costitemrng"
'activecell.value = a2 'how do you know you're in the right cell??
else
'adjust something on that same row???
foundcell.offset(0,-1).value = "x"
end if
 
R

ranswert

Thanks I'll give that a try

Dave Peterson said:
Instead of looping through the cells, you could use .find() or if the
costitemrng/costcoderng are a single column, you could use application.match().

Dim FoundCell as range
with worksheets("Somesheetnamehere").range("costitemrng")
set foundcell = .cells.find(what:=a2, _
after:=.Cells(.Cells.Count), _
lookat:=xlWhole, _
LookIn:=xlValues, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
end with

if foundcell is nothing then
msgbox "not found in costitemrng"
'activecell.value = a2 'how do you know you're in the right cell??
else
'adjust something on that same row???
foundcell.offset(0,-1).value = "x"
end if
 

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