Solved

Problem converting an Excel Range to a VBA array

Posted on 2013-05-28
5
189 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 50

Accepted Solution

by:
Rgonzo1971 earned 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 a scrolling table in Microsoft Excel using the INDEX function.

856 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