Solved

Problem converting an Excel Range to a VBA array

Posted on 2013-05-28
5
163 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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

708 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

18 Experts available now in Live!

Get 1:1 Help Now