Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass sheet to a function (excel vba)

Tags:

excel

vba

I've been trying to pass a sheet in my workbook to a function, but it doesn't seem to be working. The function is

Private Sub passToSheet(theData As Variant, Optional mySheet As Worksheet)

I have tried doing

Dim mySheet as Worksheet
Set mySheet = "Results"

then to call the function

passToSheet theData mySheet

but it doesn't seem to work. I'm not really sure what to google to get the right way to reference the worksheet though! Any help is much appreciated!

like image 453
Matt Facer Avatar asked Jan 17 '10 21:01

Matt Facer


People also ask

How do you pass a value to a function in VBA?

You can pass an argument by value if you include the ByVal keyword in the procedure's declaration. Arguments passed by value consume from 2–16 bytes within the procedure, depending on the argument's data type. Larger data types take slightly longer to pass by value than smaller ones.

How do you pass a range in a function in VBA Excel?

To allow for a variable number of ranges to be used in the function, you need to declare a ParamArray variant array in your argument list. Then, you can process each of the ranges in the array in turn. This function could then be used in the worksheet to add multiple ranges.


1 Answers

Use

'Set mySheet to worksheet
Set mySheet = Worksheets("Results")

to get a reference to the Worksheet.

like image 67
Vincent Ramdhanie Avatar answered Oct 09 '22 17:10

Vincent Ramdhanie