Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 3
Default Using text in VB Code

Hi, very new to VB code, I just found a code online and was trying to convert it to work as I want. Pretty much, as you can see in the code below, I want the cell to say Hi, if the referenced cell has a 1 in it. But when I try it in excel, i get a #VALUE! error. if I put in a 2, I do get the correct return, and putting in anything else gets a 0, as it supposed to. But how do I get text to return in the cell? I tried changing cell format to text, but that didnt work.

And I need to use VB because I will end up having more than 7 IF statements, which I found out, is the most excel can handle.

Thanks.

Function CalcValue(pVal As String) As Long
Select Case pVal
Case "1"
CalcValue = "hi"
Case "2"
CalcValue = 64
Case Else
CalcValue = 0
End Select
End Function
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 621
Default Using text in VB Code

Either of these modifications works.

I would go with second one personally so Case numbers are numbers, not
text.

Function CalcValue(pVal As String)
Select Case pVal
Case "1"
CalcValue = "hi"
Case "2"
CalcValue = 64
Case Else
CalcValue = 0
End Select
End Function


Function CalcValue(pVal As Long)
Select Case pVal
Case 1
CalcValue = "hi"
Case 2
CalcValue = 64
Case Else
CalcValue = 0
End Select
End Function


Gord


On Tue, 28 Feb 2012 14:17:43 +0000, nappyjim
wrote:


Hi, very new to VB code, I just found a code online and was trying to
convert it to work as I want. Pretty much, as you can see in the code
below, I want the cell to say Hi, if the referenced cell has a 1 in it.
But when I try it in excel, i get a #VALUE! error. if I put in a 2, I
do get the correct return, and putting in anything else gets a 0, as it
supposed to. But how do I get text to return in the cell? I tried
changing cell format to text, but that didnt work.

And I need to use VB because I will end up having more than 7 IF
statements, which I found out, is the most excel can handle.

Thanks.

Function CalcValue(pVal As String) As Long
Select Case pVal
Case "1"
CalcValue = "hi"
Case "2"
CalcValue = 64
Case Else
CalcValue = 0
End Select
End Function

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
FxM FxM is offline
external usenet poster
 
Posts: 9
Default Using text in VB Code

Hi nappyjim,

With choice "1", you try to return a text ("hi") into a "long" format
(Function ... as Long).

Try with the following :
Function CalcValue(...)
(rest is the same).

@+
FxM



Le 28/02/2012 15:17, nappyjim a écrit :
Hi, very new to VB code, I just found a code online and was trying to
convert it to work as I want. Pretty much, as you can see in the code
below, I want the cell to say Hi, if the referenced cell has a 1 in it.
But when I try it in excel, i get a #VALUE! error. if I put in a 2, I
do get the correct return, and putting in anything else gets a 0, as it
supposed to. But how do I get text to return in the cell? I tried
changing cell format to text, but that didnt work.

And I need to use VB because I will end up having more than 7 IF
statements, which I found out, is the most excel can handle.

Thanks.

Function CalcValue(pVal As String) As Long
Select Case pVal
Case "1"
CalcValue = "hi"
Case "2"
CalcValue = 64
Case Else
CalcValue = 0
End Select
End Function





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
Text format - setting text colour with code NDBC Excel Discussion (Misc queries) 6 July 1st 09 10:15 PM
Need to Add Text to Control Text Box via Command Button code BJ Excel Programming 1 September 19th 08 06:16 PM
Macro - Fixed text code needs replacing with variable text steven.holloway Excel Discussion (Misc queries) 3 July 22nd 08 03:57 PM
Code for searching & copying Text from 1 text file to another J@Y Excel Programming 2 June 27th 07 04:16 PM
Convert a Number Code to a Text Code Traye Excel Discussion (Misc queries) 3 April 6th 07 09:54 PM


All times are GMT +1. The time now is 08:58 AM.

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

About Us

"It's about Microsoft Excel"