Assigning a variable to a databodyrange?

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
299
Office Version
  1. 365
I'm using PowerQuery to produce a table containing one column of email addresses. I want these email addresses to be my distribution list in an email. I'm using VBA to create an email window with the addresses already populated.

Originally, I was using a loop to cycle through the column of email addresses and adding to the string variable with each loop. When completed, I put that string into the To: field in my code and all is fine.

But instead of using a loop, is there a way I can assign a variable to the databodyrange of the table produced by PowerQuery?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You could do something like this to get the range with the emails in it.
Code:
Dim tbl As ListObject
Dim colEmail As ListColumn
Dim rngEmail As Range

    Set tbl = Sheets("Sheet2").ListObjects("Table1")
    
    Set colEmail = tbl.ListColumns("Email")
    
    Set rngEmail = colEmail.DataBodyRange
 
Upvote 0
You could do something like this to get the range with the emails in it.
Code:
Dim tbl As ListObject
Dim colEmail As ListColumn
Dim rngEmail As Range

    Set tbl = Sheets("Sheet2").ListObjects("Table1")
   
    Set colEmail = tbl.ListColumns("Email")
   
    Set rngEmail = colEmail.DataBodyRange
Hi Norie and thanks!

I updated a line of your code to reflect the name of my powerquery table (distroSheet is already defined):
VBA Code:
Set tbl = distroSheet.ListObjects("q_NotAcknowledgedEmails")

But it gives this error message:

error message.png
 
Upvote 0
When I debug, it lands on this line of code:
VBA Code:
objOutlookMsg.To = rngEmail
 
Upvote 0
Are you doing anything else with rngEmail?

Don't you need to create a list of email addresses from the values in it?
 
Upvote 0
I'm not doing anything else with rngEmail.

Yes, my goal is to create a list of email addresses in rngEmail to be used in the To: field of the email I'm creating.
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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