How to select pivot table range, dynamically

lukisx7

New Member
Joined
May 12, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello,



I have a few excel pivot tables in different sheets, which I need to be sent automatically through outlook.

How could I select the range needed, as I need to send the whole pivot table in the active sheet, but without columns that I hide.



My current Pivot table ranges from A2:J296, with D;E;F;G columns hidden. But the thing is, in other sheets, there could be less columns, less rows.

Maybe there is some way to select the columns range by the text written in the last column? (In J2 in this particular case).



Thank you!



My current code, looks like this (But it still sends with all the hidden columns, and the select range is also not dynamic):



VBA Code:
Sub Send()

Dim x, y As String

  

   ActiveSheet.Range("A2").Select

   ActiveSheet.Range(Selection, Selection.End(xlToRight).End(xlToRight).End(xlToRight)).Select

   ActiveSheet.Range(Selection, Selection.End(xlDown).End(xlDown)).Select

  

   x = ActiveSheet.Range("M1").Value

   y = ActiveSheet.Range("M2").Value

  

   ActiveWorkbook.EnvelopeVisible = True

     

   With ActiveSheet.MailEnvelope

      .Item.to = x

      .Item.cc = y

      .Item.Subject = "Client debts " & Date

      .Item.Send

   End With

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
One way would be to add a temporary worksheet, copy and pivot table to that worksheet, delete the columns that you don't want, send it, and then delete the temporary worksheet. However, if you have Outlook, you can use the following macro to send an email. For testing purposes, though, it displays the email instead of sending it. Once you've tested it, simply replace .Display with .Send to actually send the email.

VBA Code:
Sub Send()

    Dim pivotTableRange As Range
    Set pivotTableRange = Range("A2").CurrentRegion
    
    Dim strTo As String
    strTo = Range("M1").Value
    
    Dim strCC As String
    strCC = Range("M2").Value
    
    Dim strSubject As String
    strSubject = "Client debts " & Date
    
    pivotTableRange.Copy
    
    With CreateObject("Outlook.Application").CreateItem(0)
        .to = strTo
        .cc = strCC
        .Subject = strSubject
        .GetInspector.WordEditor.Application.Selection.Paste
        .display 'or .send
    End With
    
    Set pivotTableRange = Nothing

End Sub

Hope this helps!
 
Upvote 0
You could use the properties, e.g. DataBodyRange, of the pivot table(s) to get the range.
VBA Code:
Sub Send()
Dim pt As PivotTable
Dim ptRange As Range
Dim strTo As String
Dim strCC As String
Dim strSubject As String

    Set pt = Sheets("Sheet1").PivotTables(1)
    
    Set ptRange = pt.DataBodyRange.CurrentRegion
    
    strTo = Range("M1").Value
       
    strCC = Range("M2").Value
     
    strSubject = "Client debts " & Date
    
    ptRange.SpecialCells(xlCellTypeVisible).Copy
    
    With CreateObject("Outlook.Application").CreateItem(0)
        .to = strTo
        .cc = strCC
        .Subject = strSubject
        .GetInspector.WordEditor.Application.Selection.Paste
        .display 'or .send
    End With
    
    Set ptRange = Nothing

End Sub
 
Upvote 0
As @Norie has pointed out, you can refer to the pivot table itself for the desired range. I should have done the same. Note, though, you can also refer to the whole pviot table, including any page fields, as follows...

VBA Code:
Set ptRange = pt.TableRange2

Or, to exclude page fields...

VBA Code:
Set ptRange = pt.TableRange1
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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