Hi i created a code in vba. the requirements is that
-input is 3 color bands from a resistor(in order)
-and the output of the resistor in ohms
-make function work for 3 colors.I am using vba. I came up with a code but it has my function highlighted in yellow. whats wrong with it?
'Resistor By: Diamond Wongus
Function Resistor(color1, color2, color3)

'I cannot put the equation until the end
Select Case color1
Case "black", "Black"
color1 = 0
Case "brown", "Brown"
color1 = 1
Case "Red", "red"
color1 = 2
Case "Orange", "orange"
color1 = 3
Case "Yellow", "yellow"
color1 = 4
Case "Green", "green"
color1 = 5
Case "Blue", "blue"
color1 = 6
Case "Violet", "violet", "Purple", "purple"
color1 = 7
Case "Grey", "grey", "Gray", "gray"
color1 = 8
Case "White", "white"
color1 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver""silver"
color1 0.001
'the above information is for color 1
Select Case color2
Case "black", "Black"

color2 = 0
Case "brown", "Brown"
color2 = 1
Case "Red", "red"
color2 = 2
Case "Orange", "orange"
color2 = 3
Case "Yellow", "yellow"
color2 = 4
Case "Green", "green"
color2 = 5
Case "Blue", "blue"
color2 = 6
Case "Violet", "violet", "Purple", "purple"
color2 = 7
Case "Grey", "grey", "Gray", "gray"
color2 = 8
Case "White", "white"
color2 = 9
Case "Gold", "gold"
color2 = 0.1
Case "Silver""silver"
color2 0.001
'the above data is color 2
Select Case color3

Case "black", "Black"
color3 = 0
Case "brown", "Brown"
color3 = 1
Case "Red", "red"
color3 = 2
Case "Orange", "orange"
color3 = 3
Case "Yellow", "yellow"
color3 = 4
Case "Green", "green"
color3 = 5
Case "Blue", "blue"
color3 = 6
Case "Violet", "violet", "Purple", "purple"
color3 = 7
Case "Grey", "grey", "Gray", "gray"
color3 = 8
Case "White", "white"
color3 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver""silver"
color3 0.001
'The above data is color 3
End Select
Resistor = (color1 * 10 + color2) * (10 ^ color3)


End Function

Recommended Answers

All 5 Replies

You have not specified the return type for the function.

Try:

Function Resistor(color1 As String, color2 As String, color3 As String) As Double

You can then call your function like so:

Dim myResistor As Double

myResistor = Resistor("black", "blue", "silver")

Please note that you have several other problems with your code.
1. You have neglected to have "End Select" for the first two "Select Case" statements.
2. You have left out commas in several of the "Case" clauses (look at "Silver""silver").
3. You have left out equals signs in several places (look at "color1 0.001")

Oh, and just as a friendly reminder, please use CODE tags when you post code.

Hope this gets you going. Good luck!

Thank you
I am not sure on how to use CODE tags(im kinda new to this whole computer thing).Can you tell me how to do that?Thanks and i made all the changes and its still not working?/Im so confused and I need this done by monday.This is what i have so far.Can you tell me whats wrong?

'Resistor By: Diamond Wongus
Function Resistor(color1 As String, color2 As String, color3 As String) As Double
'I used As String because each variable (colors) have a set amount of values they can be (0-9 and .1 and .001)
'I cannot put the equation until the end
Select Case color1
Case "black", "Black"
color1 = 0
Case "brown", "Brown"
color1 = 1
Case "Red", "red"
color1 = 2
Case "Orange", "orange"
color1 = 3
Case "Yellow", "yellow"
color1 = 4
Case "Green", "green"
color1 = 5
Case "Blue", "blue"
color1 = 6
Case "Violet", "violet", "Purple", "purple"
color1 = 7
Case "Grey", "grey", "Gray", "gray"
color1 = 8
Case "White", "white"
color1 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver", "silver"
color1 = 0.001
End Select
'the above information is for color 1
Select Case color2
Case "black", "Black"

color2 = 0
Case "brown", "Brown"
color2 = 1
Case "Red", "red"
color2 = 2
Case "Orange", "orange"
color2 = 3
Case "Yellow", "yellow"
color2 = 4
Case "Green", "green"
color2 = 5
Case "Blue", "blue"
color2 = 6
Case "Violet", "violet", "Purple", "purple"
color2 = 7
Case "Grey", "grey", "Gray", "gray"
color2 = 8
Case "White", "white"
color2 = 9
Case "Gold", "gold"
color2 = 0.1
Case "Silver", "silver"
color2 = 0.001
'the above data is color 2
End Select
Select Case color3

Case "black", "Black"
color3 = 0
Case "brown", "Brown"
color3 = 1
Case "Red", "red"
color3 = 2
Case "Orange", "orange"
color3 = 3
Case "Yellow", "yellow"
color3 = 4
Case "Green", "green"
color3 = 5
Case "Blue", "blue"
color3 = 6
Case "Violet", "violet", "Purple", "purple"
color3 = 7
Case "Grey", "grey", "Gray", "gray"
color3 = 8
Case "White", "white"
color3 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver", "silver"
color3 = 0.001
'The above data is color 3
End Select
Resistor = (color1 * 10 + color2) * (10 ^ color3)
Dim myResistor As Double
'i use Dim so that VBA can remember my values for my variables

End Function

You have to put this in general declaration:

Dim myResistor As Double

You have to use myResistor instead of Resistor:

WRONG:
Resistor = (color1 * 10 + color2) * (10 ^ color3)
CORRECT:
myResistor = (color1 * 10 + color2) * (10 ^ color3)

If an error still occur then let me know. Post the exact
error message that was displayed.

Have fun!

Okay, here's the deal. Code tags are the tags you should surround your code with so that it formats correctly on this forum. Has nothing to do with your code per se. See the [ C O D E ] thing in the "Post Reply" editing area? Click that and it will give you your code tags. Paste your code in between the two tags.

Next, I didn't notice this before, but you're passing text into your function as parameters, then turning right around and changing them to numbers. Bad technique. It will work but you should be using separate local variables inside your function for calculations.

Next, (and also for @Capritarius21), in order to return a value from a function in VB6, you set the name of the function equal to the return value. That will pass the value back to wherever it was called from. So, in the interest of complete disclosure, here is some (corrected) sample code.

Private Sub Form_Load()
' Here is where you declare the variable to hold the return value of your function
Dim myResistor As Double

' Here is where you call your function
myResistor = Resistor("black", "blue", "silver")

' Here is where you display the return value
Me.Caption = CStr(myResistor)

End Sub

Function Resistor(color1 As String, color2 As String, color3 As String) As Double
' Here is where you declare the variables you use to do calculations
Dim dblColor1 As Double
Dim dblColor2 As Double
Dim dblColor3 As Double

' Here is where you convert your parameter to a number
Select Case color1
Case "black", "Black"
dblColor1 = 0
Case "brown", "Brown"
dblColor1 = 1
Case "Red", "red"
dblColor1 = 2
Case "Orange", "orange"
dblColor1 = 3
Case "Yellow", "yellow"
dblColor1 = 4
Case "Green", "green"
dblColor1 = 5
Case "Blue", "blue"
dblColor1 = 6
Case "Violet", "violet", "Purple", "purple"
dblColor1 = 7
Case "Grey", "grey", "Gray", "gray"
dblColor1 = 8
Case "White", "white"
dblColor1 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver", "silver"
dblColor1 = 0.001
End Select

' Here is where you convert your next parameter to a number
Select Case color2
Case "black", "Black"
dblColor2 = 0
Case "brown", "Brown"
dblColor2 = 1
Case "Red", "red"
dblColor2 = 2
Case "Orange", "orange"
dblColor2 = 3
Case "Yellow", "yellow"
dblColor2 = 4
Case "Green", "green"
dblColor2 = 5
Case "Blue", "blue"
dblColor2 = 6
Case "Violet", "violet", "Purple", "purple"
dblColor2 = 7
Case "Grey", "grey", "Gray", "gray"
dblColor2 = 8
Case "White", "white"
dblColor2 = 9
Case "Gold", "gold"
dblColor2 = 0.1
Case "Silver", "silver"
dblColor2 = 0.001
End Select

' Here is where you convert your last parameter to a number
Select Case color3
Case "black", "Black"
dblColor3 = 0
Case "brown", "Brown"
dblColor3 = 1
Case "Red", "red"
dblColor3 = 2
Case "Orange", "orange"
dblColor3 = 3
Case "Yellow", "yellow"
dblColor3 = 4
Case "Green", "green"
dblColor3 = 5
Case "Blue", "blue"
dblColor3 = 6
Case "Violet", "violet", "Purple", "purple"
dblColor3 = 7
Case "Grey", "grey", "Gray", "gray"
dblColor3 = 8
Case "White", "white"
dblColor3 = 9
Case "Gold", "gold"
dblColor3 = 0.1
Case "Silver", "silver"
dblColor3 = 0.001
End Select

' Here is where you do your calculation, and set the return value
Resistor = (dblColor1 * 10 + dblColor2) * (10 ^ dblColor3)

End Function

Just an aside, you may also want to experiment with some indenting so that your code is a little more readable.

Thanks for all your help.I finally figured it out last night.I will remember these things for future projects:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.