Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Custom Function If..Else and reading text strings

I'm trying to write a function to read a string and depending on the
contents of that string assign a type to it. The code below is just an
example. I would use the IF function built-in to Excel but I have 15+
types and couple different ways to determine some of those types. This
is my first function.

When I use the following function, all I get is a '0' in the cell, not
even my error message. That makes me think that I'm using the StrComp
function incorrectly.

For sample data I'm using:
< 5KOLRAWAL1BI
< 2USSTVRVS1GI
< 5KOLTVWAL1BI

Function ReadAdCode(strAdCode As String)
Dim strAdType As String

If StrComp(strAdCode, "*5KOL*", vbTextCompare) = 0 Then
strAdType = "OTHER MEDIA"
ElseIf StrComp(strAdCode, "*TV*", vbTextCompare) = 0 Then
strAdType = "TVA"
Else
strAdType = "This is Not Working"
End If
End Function

Any help is greatly appreciated!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Custom Function If..Else and reading text strings

You need to return the value, not just assign it to a variable

Function ReadAdCode(strAdCode As String)
Dim strAdType As String

If StrComp(strAdCode, "*5KOL*", vbTextCompare) = 0 Then
ReadAdCode = "OTHER MEDIA"
ElseIf StrComp(strAdCode, "*TV*", vbTextCompare) = 0 Then
ReadAdCode = "TVA"
Else
ReadAdCode = "This is Not Working"
End If
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan" wrote in message
oups.com...
I'm trying to write a function to read a string and depending on the
contents of that string assign a type to it. The code below is just an
example. I would use the IF function built-in to Excel but I have 15+
types and couple different ways to determine some of those types. This
is my first function.

When I use the following function, all I get is a '0' in the cell, not
even my error message. That makes me think that I'm using the StrComp
function incorrectly.

For sample data I'm using:
< 5KOLRAWAL1BI
< 2USSTVRVS1GI
< 5KOLTVWAL1BI

Function ReadAdCode(strAdCode As String)
Dim strAdType As String

If StrComp(strAdCode, "*5KOL*", vbTextCompare) = 0 Then
strAdType = "OTHER MEDIA"
ElseIf StrComp(strAdCode, "*TV*", vbTextCompare) = 0 Then
strAdType = "TVA"
Else
strAdType = "This is Not Working"
End If
End Function

Any help is greatly appreciated!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Custom Function If..Else and reading text strings

You have to assign the result to ReadAdCode, e.g....

Public Function ReadAdCode(strAdCode As String) As String
Dim strAdType As String
If StrComp(strAdCode, "*5KOL*", vbTextCompare) = 0 Then
strAdType = "OTHER MEDIA"
ElseIf StrComp(strAdCode, "*TV*", vbTextCompare) = 0 Then
strAdType = "TVA"
Else
strAdType = "This is Not Working"
End If
ReadAdCode = strAdType
End Function

Note that StrComp compares strings ASCII values - so * is not a
wildcard. I think you're really after something like

Public Function ReadAdCode(strAdCode As String) As String
Dim strAdType As String
If InStr(1, strAdCode, "5KOL", vbTextCompare) Then
strAdType = "OTHER MEDIA"
ElseIf InStr(1, strAdCode, "TV", vbTextCompare) Then
strAdType = "TVA"
Else
strAdType = "This is Not Working"
End If
ReadAdCode = strAdType
End Function

In article .com,
"Dan" wrote:

I'm trying to write a function to read a string and depending on the
contents of that string assign a type to it. The code below is just an
example. I would use the IF function built-in to Excel but I have 15+
types and couple different ways to determine some of those types. This
is my first function.

When I use the following function, all I get is a '0' in the cell, not
even my error message. That makes me think that I'm using the StrComp
function incorrectly.

For sample data I'm using:
< 5KOLRAWAL1BI
< 2USSTVRVS1GI
< 5KOLTVWAL1BI

Function ReadAdCode(strAdCode As String)
Dim strAdType As String

If StrComp(strAdCode, "*5KOL*", vbTextCompare) = 0 Then
strAdType = "OTHER MEDIA"
ElseIf StrComp(strAdCode, "*TV*", vbTextCompare) = 0 Then
strAdType = "TVA"
Else
strAdType = "This is Not Working"
End If
End Function

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Custom Function If..Else and reading text strings

I added the comparison " 0" to the if staement and it works. This
should save me about 2.5 hrs/ week. Thanks!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Custom Function If..Else and reading text strings

Not strictly necessary, since any non-zero result of InStr will be
interpreted as True, and any zero result as False. But it certainly
doesn't hurt.

In article .com,
"Dan" wrote:

I added the comparison " 0" to the if staement and it works.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
=IF function, reference problem to "text strings" in Data Validati Jim D.[_2_] Excel Discussion (Misc queries) 2 July 17th 09 08:56 AM
How to refer to a text piece in a VBA custom function? FARAZ QURESHI Excel Discussion (Misc queries) 2 January 27th 08 03:11 PM
Custom number formats in TEXT function MatthewB Excel Worksheet Functions 4 June 28th 06 09:05 PM
Reading long strings (with jdbc) from Excel Aaron Fude Excel Programming 4 November 15th 04 12:56 PM


All times are GMT +1. The time now is 07:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"