0

I keep getting the error message of "application defined or object defined error" at the last step when I need to select the defined "TABLE1".

Appreciate your help!

Dim last_row As Long
last_row = ActiveSheet.Range("A1048576").End(xlUp).Row

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$U$" & last_row), , xlYes).Name = _
    "Table1"
ActiveCell.Range("Table1[#All]").Select
  • `ActiveSheet`, not `ActiveCell`... but it would be better to work with a `ListObject`. – BigBen Nov 19 '20 at 23:42
  • To follow up on what @BigBen said, you might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/q/10714251/4996248) – John Coleman Nov 19 '20 at 23:59
  • You don't need to `Select` anything. Specify a cell as `Cells([Row number], [Column number or letter])`. Specify a range by its first and last cells, like `Range(Cells(1, "A"), Cells(lastRow, "U")`. Find the last ***used*** cell by searching from the last ***column*** cell up, like `Cells(Rows.Count, "A").End(xlUp)`. You can use that cell to define a range, like `Set MyRange = Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))`. Same for last ***used*** column, `Cells(1, Columns.Count).End(xlToLeft)`. Start from the extreme right. – Variatus Nov 20 '20 at 02:20
  • A `Range` can have one cell or many. Therefore, technically speaking, a *Cell* is a *Range*. It has a `Column` and `Row` property, like `Cells(Rows.Count, "A").End(xlUp).Row` or `Cells(1, Columns.Count).End(xlToLeft).Column`. You can assign these numbers to variables and use them to specify cells. Then you can use cells to specify ranges. Once you have identified a cell you can read or set its `Value` without selecting it. – Variatus Nov 20 '20 at 02:27

0 Answers0