Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem converting an Excel Range to a VBA array

Posted on 2013-05-28
5
Medium Priority
?
203 Views
Last Modified: 2013-05-28
In this function, why does the first array assignment line work and the second give a type mismatch?  Don't they point to the same thing?
Function testit2() As Variant()

    Dim testarr() As Variant
    
    testarr = Range("A4:A31") ' works
    testarr = ThisWorkbook.Worksheets("FormulaTemplate").Range("A4:A31") ' Gives Type Mismatch error
    
    testit2 = testarr

End Function

Open in new window

0
Comment
Question by:cashonly
  • 3
5 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 39201367
Hi

pls try

Function testit2() As Variant()

    Dim testarr() As Variant
    Dim testarr1 As Variant

    testarr = Range("A4:A31") ' works
    testarr1 = ThisWorkbook.Worksheets("FormulaTemplate").Range("A4:A31") 
    
    testit1 = testarr
    testit2 = testarr1


End Function 

Open in new window

see Locals type for difference

Regards
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39201375
Hi,

It is the use of the () parenthesis that implies that the testarr variable is defined as an Array.

Try this instead:

Function testit2() As Variant

    Dim testarr As Variant
    
    testarr = Range("A4:A31") ' works
    testarr = ThisWorkbook.Worksheets("FormulaTemplate").Range("A4:A31") ' Now works!
    
    testit2 = testarr

End Function

Open in new window


You could test this as follows:

Public Sub Test()

  ThisWorkbook.Worksheets("FormulaTemplate").Range("B4:B31") = testit2()
  
End Sub

Open in new window


The contents of the range [A4:A31] should be copied to the range [B4:B31].

BFN,

fp.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39201380
:)

^ What Rgonzo1971 said!
0
 

Author Closing Comment

by:cashonly
ID: 39201417
Rgonzo1971, thanks, spot on

fanspage, sorry, Rgonzo1971 beat you to the punch.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39201550
Yeah... I attempted to answer your question, that slowed me down! :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

971 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