Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem converting an Excel Range to a VBA array

Posted on 2013-05-28
5
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 52

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

721 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