PDA

View Full Version : Solved: If else doesn't work the way I expected



venson0801
07-16-2010, 11:36 AM
Hey guys, I have created a sub, intend to find the some certain text and move to the bottom of the category. The code works fine if I could find the existing category. I want a messagebox pop out if they didn't find it, but the problem is that my code never goes to the else section of the code. Could someone please tell me what's wrong with the code? Thanks so much!

Here is the code:
Private Sub LookUpType()
Dim datatoFind3, g, currentRow


datatoFind3 = cbotype.Value
g = Cells.Find(What:=datatoFind3, After:=[A1], LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

If g = "True" Then
ActiveCell.Offset(1, 6).Select
Do While ActiveCell.Value = datatoFind3
ActiveCell.Offset(1, 0).Select

Loop

Else
MsgBox ("New Category")


End If
ActiveCell.EntireRow.Insert shift:=xlDown
End Sub

Bob Phillips
07-16-2010, 01:25 PM
Try this



Private Sub LookUpType()
Dim datatoFind3, g As Range, currentRow As Long


datatoFind3 = cbotype.Value
Set g = Cells.Find(What:=datatoFind3, After:=Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)

If Not g Is Nothing Then
g.Offset(1, 6).Select
Do While ActiveCell.Value = datatoFind3
ActiveCell.Offset(1, 0).Select

Loop

Else
MsgBox ("New Category")


End If
ActiveCell.EntireRow.Insert shift:=xlDown
End Sub

venson0801
07-16-2010, 01:32 PM
Thanks so much, the code actual works!