|
-
Aug 5th, 2011, 11:15 AM
#1
Thread Starter
New Member
Public Function VBA #VALUE Error
Hi,
I have created a simple Public Function (to be expanded) but want to make sure I can get the call in excel working. Here is the code:
Public Function Wind (Direction as integer) as integer
If Direction > 0 & Direction <=10 Then Wind = 1
Else: Wind = 0
End If
End Public Function
Essentially Direction is a numeric value for wind direction and i am seeing if it is between 0 and 10. When i type the following in excel
=Wind (A1) it returns #VALUE
Any help would be appreciated
-
Aug 5th, 2011, 11:32 AM
#2
Re: Public Function VBA #VALUE Error
Thread moved from the 'VB6' forum to the 'Office Development/VBA' forum (while VBA and VB6 have some similarities, they are not the same thing)
-
Aug 5th, 2011, 12:30 PM
#3
Re: Public Function VBA #VALUE Error
You are in the right track. A couple of changes:
* & is concatenation for string, use "And" instead.
* End Function, not End Public Function
* note: This function does not include the "0", if you want it do >=0
* If you put a statement right after the Then keyword, you cant put Else on the next row.
One cosmetic change:
* Structure your code for readability, saving code rows making it compact at the expense of readability is not a good practice.
vb Code:
Public Function Wind(Direction As Integer) As Integer
If Direction > 0 And Direction <= 10 Then
Wind = 1
Else
Wind = 0
End If
End Function
Last edited by kaliman79912; Aug 5th, 2011 at 12:37 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Aug 5th, 2011, 01:37 PM
#4
Thread Starter
New Member
Re: Public Function VBA #VALUE Error
Thanks for the reply, it worked great. As a follow-up I wanted to see if it would be possible to only use the below code for a wind speed greater than 15km/hr. I added the Speed in the function but do not know how to approach only running the code for Speed > 15
Public Function Wind(Direction As Integer, Speed as integer) As Integer
If Direction > 0 And Direction <= 10 Then
Wind = 1
Else
Wind = 0
End If
End Function
-
Aug 5th, 2011, 05:15 PM
#5
Re: Public Function VBA #VALUE Error
When posting code try to format it as such, with the code tags at the top of the edit window.
vb Code:
Public Function Wind(Direction As Integer, Speed as Integer) As Integer
If Direction > 0 And Direction <= 10 And Speed > 15 Then
Wind = 1
Else
Wind = 0
End If
End Function
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Aug 6th, 2011, 11:41 AM
#6
Re: Public Function VBA #VALUE Error
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|