ActiveSheet.ListObjects copy only specific columns from a table

Mike756

New Member
Joined
Apr 10, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi!

My problem seems rather easy but I cannot get it to work. I need to copy a table, but I do not want all columns.
The below code works but not as expected. It seems to select the right columns (2,3 and 5), but when I paste the result, I get columns 2,3,4, and 5.

Why is that? I do not want column 4. How can I do that?

VBA Code:
With ActiveSheet.ListObjects("Table1")
   Union(.ListColumns(2).Range, _
              .ListColumns(3).Range, _
              .ListColumns(5).Range).Copy
End With

Thanks for the help.
Mike
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I just realized that it actually works when I paste it into an Excel sheet. When I paste it into an HTML email (Outlook), it also pastes all columns between the selection. I really don't understand why this is happening...
 
Upvote 0
Hello. Try with:

VBA Code:
Dim a, i&, j%
With ActiveSheet.ListObjects(1).Range
  ReDim a(1 To .Rows.Count, 1 To 1)
  For i = 1 To .Rows.Count
    a(i, 1) = .Cells(i, 2).Text & vbTab & .Cells(i, 3).Text & vbTab & .Cells(i, 5).Text
  Next
End With
 
Upvote 0
Hello. Try with:

VBA Code:
Dim a, i&, j%
With ActiveSheet.ListObjects(1).Range
  ReDim a(1 To .Rows.Count, 1 To 1)
  For i = 1 To .Rows.Count
    a(i, 1) = .Cells(i, 2).Text & vbTab & .Cells(i, 3).Text & vbTab & .Cells(i, 5).Text
  Next
End With

Hi! Thanks...sorry for my ignorance but how do I copy the result of this code? I need to get it into my clipboard.
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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