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
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