Transfer Excel Table into a VBA array

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I know you can do this fairly simply with a Named Range (set myArray = [insert named range]), however when trying to do this with a table I am running into some difficulty. Is there a simple way to do this? Thanks for your thoughts!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi

Let's assume you have a table called MyTable in Sheet1.
To copy the table data into an array, for ex.:

Code:
Dim myArray As Variant

myArray = Worksheets("Sheet1").ListObjects("MyTable").DataBodyRange.Value
 
Upvote 0
Thanks for the response! I tried your code and I am getting a "Type Mismatch" error. Not sure how that could be since the variable is set to variant? Any thoughts?

<font face=Calibri><SPAN style="color:#00007F">Dim</SPAN> sht <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> EmailList <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><br><SPAN style="color:#00007F">Set</SPAN> sht = ThisWorkbook.Sheets(Settings.Name)<br><br><SPAN style="color:#007F00">'Need to check if range is 0,1,or >1</SPAN><br>    <SPAN style="color:#007F00">'TO:</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> EmailList = sht.ListObjects("Email_To").DataBodyRange.Value</FONT>
 
Upvote 0
Hi

Your syntax is wrong, not how I posted.


Either

Code:
[FONT=Calibri]
        EmailList = sht.ListObjects("Email_To").DataBodyRange.Value


[/FONT]

... and you get an array with all the values,

or

Code:
[FONT=Calibri]        [COLOR=#00007F]Set[/COLOR] EmailList = sht.ListObjects("Email_To").DataBodyRange[/FONT][FONT=Calibri]
[/FONT]

... and you get the range where the values are.

Please try.
 
Upvote 0
Gah, that was my problem all along! I don't know what I was thinking, silly mistake. Thanks for clearing that up for me :)
 
Upvote 0
Hey PGC just a quick follow-up.. if my table only has one row in it, the variable turns into a string instead of an array. Is there anyway to check if the variable is a string or an array (without using Error Handlers)?

In my example if there is only one row in the table, my code errors out on the "elseif" line because the variable is a string and doesn't have an upper bound:

<font face=Calibri>    <SPAN style="color:#007F00">'TO:</SPAN><br>        EmailList = sht.ListObjects("Email_To").DataBodyRange.Value<br>        <br>        <SPAN style="color:#00007F">If</SPAN> IsEmpty(EmailList) <SPAN style="color:#00007F">Then</SPAN><br>            MsgBox "Please enter in an email address to send to."<br>            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>        <SPAN style="color:#00007F">ElseIf</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(EmailList) = 1 <SPAN style="color:#00007F">Then</SPAN><br>            CompiledListTo = EmailList(1, 1)<br>        <SPAN style="color:#00007F">Else</SPAN><br>            CompiledListTo = Join(Application.Transpose(EmailList), "; ")<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>
 
Upvote 0
Hi

You can test if it is an array, like:

Code:
       If IsEmpty(EmailList) Then
            MsgBox "Please enter in an email address to send to."
            Exit Sub
        ElseIf Not IsArray(EmailList) Then
            CompiledListTo = EmailList
        Else
            CompiledListTo = Join(Application.Transpose(EmailList), "; ")
        End If
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,336
Members
449,310
Latest member
zztt388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top