Solved

Returning multiple values on vba with a function

Posted on 2011-09-14
3
300 Views
Last Modified: 2012-09-17
I have a function, which is a web query. It gets the table from internet and pastes it on Cell E111.
The information I need is on Cells F111 and F114.

so after the webquery I tried something like this and didnt work
myfunction = b(Sheets("LiveSheet").Range("F111").Value, Sheets("LiveSheet").Range("F114").Value)

I need to put the values, i return from the function into two different cells on excel.

Also the function is used in a vba subroutine.
0
Comment
Question by:awesomejohn19
3 Comments
 
LVL 5

Expert Comment

by:slycoder
Comment Utility
Break your function into two explicit parts and try something like:

Range("A1").Value = myfunction1()
Range("B1").Value = myfunction2()
0
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
Return an array from your function.

I *think* something like this might work in VBA...
Function DoIt(parm As Long) As Variant
    Doit = Array(parm,9,15)
End Function

Dim x as Variant

x = Doit(4)

Msgbox x(0)
Msgbox x(1)
Msgbox x(2)

Open in new window

0
 
LVL 4

Accepted Solution

by:
AgeOfEmpires earned 500 total points
Comment Utility
Another option, if for some reason you are dealing with two different datatypes is to use a user defined type.

Type A
   val1 as Integer
   val2 as String
End Type

Sub test()
   Dim y as A

   y = GetVal
   Debug.Print y.val1
   Debug.Print y.val2

End Sub

Function GetVal() as A
   GetVal.val1 = 1
   GetVal.val2 = "test"
End Function

Open in new window


This will call GetVal, return an integer and a string, and print both to the immediate window.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now