Calling excel VBA subroutine with arguments

Felipe

I want to create a subroutine that accepts a range as an argument. Something like this:

Sub Test(dataRange As Range)

    Sheet2.Range("A1").Cells.value = dataRange.Cells(1, 1)

End Sub

However when I try to select the range on Excel and run it, I get a "Reference is not valid" error.

Note: I am trying to run this by going into the Developer tab and clicking on the "Macros" button. In there I type in Test($A$2:$B$4), and then I get the error.

Is there a way to call a custom VBA subroutine from an Excel spreadsheet and pass arguments to it?

Thanks

David Zemens

Although this is not a generalized answer, it should work for your purposes:

Sub Test(Optional dataRange As Range = Nothing)
    If dataRange is Nothing then Set dataRange = Range(Selection.Address)
    Sheet2.Range("A1").Cells.value = dataRange.Cells(1, 1)

End Sub

From the macro menu, simply type in Test and hit the "Run" button.

This will use the current Selection as the data range.

Alternatively, use an InputBox to capture the range argument:

Sub Test2()
    Dim dataRange as Range
    Set dataRange = Application.InputBox("Select range", Type:=8)
    If dataRange is Nothing then Exit Sub
    Sheet2.Range("A1").Cells.value = dataRange.Cells(1, 1)

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA EXCEL: How to call a subroutine in another subroutine?

From Dev

how to get a subroutine in excel vba to return something?

From Dev

Excel, VBA: Scripting.dictionary in subroutine not working

From Dev

Excel VBA module subroutine not being picked up with parameter

From Dev

Calling subroutine in parallel environment

From Dev

R calling Fortran subroutine

From Dev

Perl subroutine arguments

From Dev

Calling Excel macros from PowerShell with arguments

From Dev

Passing an array to subroutine VBA

From Dev

Calling a subroutine in a module from C

From Dev

Perl subroutine arguments like a hash

From Dev

batch script subroutine: Passing arguments

From Dev

batch script subroutine: Passing arguments

From Dev

Not enough arguments when redefining a subroutine

From Dev

Calling Access VBA function from Excel

From Dev

calling subset of an array to calculate autocorrelations in excel vba

From Dev

Excel VBA error 424 calling sub

From Dev

Excel VBA Hide Calling Cell's Row

From Dev

Change event not calling macro Excel Vba

From Dev

Excel VBA - Passing Correct Arguments to Procedures

From Dev

VBA importing COM-registered dll and calling constructor with arguments

From Dev

VBA include argument to subroutine in OnAction

From Dev

Calling an internal subroutine inside OpenMP region

From Dev

Calling a subroutine in FORTRAN without blocking the main program

From Dev

Perl threads - calling subroutine from module (pm)

From Dev

Calling a FORTRAN subroutine from C#

From Dev

What is the difference when calling subroutine with and without & in Perl?

From Dev

Is it safe for a subroutine to write in the stack of its calling routine?

From Dev

Calling a FORTRAN subroutine from C#

Related Related

HotTag

Archive