Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro not working properly

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi, i have the following code in one of my macros which is not working correctly. Because "_new" will appear within both elseif statments the "_newreb" part is being ignored.

Hope this make sense!

ElseIf InStr(ActiveCell.Text, "_NEW") Then
Range("D" & X).Value = "_NEW"
Range("D" & X).Interior.ColorIndex = 3
Range("D" & X).Font.Bold = True
Range("E" & X).Value = "PC146"


ElseIf InStr(ActiveCell.Text, "_NEWREB") Then
Range("D" & X).Value = "_NEWREB"
Range("D" & X).Interior.ColorIndex = 44
Range("D" & X).Font.Bold = True
Range("E" & X).Value = "PC145"

Please can anyone help!!!

Kindest regards,

Brian
 
What about reversing the order of the statements?

Code:
ElseIf InStr(ActiveCell.Text, "_NEWREB") Then
                Range("D" & X).Value = "_NEWREB"
                Range("D" & X).Interior.ColorIndex = 44
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC145" 

ElseIf InStr(ActiveCell.Text, "_NEW") Then
                Range("D" & X).Value = "_NEW"
                Range("D" & X).Interior.ColorIndex = 3
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC146"

 
Hi thanks for replying, i have done this but it still does not work ;-(

it seams that it just detects "_new" and doesn't go as far as "_newreb"

Regards,

Brian
 
You are not understanding how Instr works. From help file:
Helpfile said:
Return Values

If InStr returns
string1 is zero-length 0
string1 is Null Null
string2 is zero-length start
string2 is Null Null
string2 is not found 0
string2 is found within string1 Position at which match is found
start > string2 0

You therefore need to test for the RESULT of instr - you can only use the syntax you have if the result of the function is true or false:
Code:
ElseIf InStr(ActiveCell.Text, "_NEWREB") [b]<> 0[/b] Then
                Range("D" & X).Value = "_NEWREB"
                Range("D" & X).Interior.ColorIndex = 44
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC145" 

ElseIf InStr(ActiveCell.Text, "_NEW") [b]<> 0[/b] Then
                Range("D" & X).Value = "_NEW"
                Range("D" & X).Interior.ColorIndex = 3
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC146"

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 





Hi,

"... Because "_new" will appear within both elseif statments the "_newreb" part is being ignored."

THEN don't say Else.

Rather
Code:
             If ActiveCell.Text Like "*_NEW*" Then
                Range("D" & X).Value = "_NEW"
                Range("D" & X).Interior.ColorIndex = 3
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC146"
             End If
              
             If ActiveCell.Text Like "*_NEWREB*" Then
                Range("D" & X).Value = "_NEWREB"
                Range("D" & X).Interior.ColorIndex = 44
                Range("D" & X).Font.Bold = True
                Range("E" & X).Value = "PC145"
             End If

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for all your help - all working now!

Kindest regards,

Brian Free
 
for future ref, please post vba questions in the VBA forum: Forum707

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top