excel vba resize how can i control the resize and keep specific columns

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
113
So when I import data to my table it often has an erradict and unpredictable amount of columns which is fine except when I want to resize I would like to keep the first 8 columns of the table and delete any of the rest.

This works great below except my coulumns seem to keep the last 8 of the columns instead of the first 8.

Any guidance is appreciated.

oh and DCRPT is the "Named Table" I am using.

Code:
With DCRPT.DataBodyRange
    If .Rows.Count > 1 Then
      .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count - 8).Rows.Delete
    End If
  End With
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,383
Office Version
2013
Platform
Windows
I would like to keep the first 8 columns of the table and delete any of the rest.
Try this:
If 'Table1' has more than 8 column then it will delete the 9th to the last column.

Code:
With ActiveSheet.ListObjects("Table1")
    n = .Range.Columns.count
        If n > 8 Then .ListColumns(9).Range.Resize(, n - 8).Delete
End With
 
Last edited:

Forum statistics

Threads
1,082,603
Messages
5,366,575
Members
400,904
Latest member
ndaines meriabi

Some videos you may like

This Week's Hot Topics

Top