Home > OS >  VBA Range Object variable or With block variable not set error
VBA Range Object variable or With block variable not set error

Time:01-30

I am pretty new to VBA and am stuck at trying to simply select a table using VBA.

In order to select the whole table, I have found the first cell (Eg. A1) to the last cell (Eg. C10) of the table. In order to save the range A1:C10 I thought I could simply save the range by the following code -

last_row = Activesheet.Range("A1").End(xlDown).row
last_column = Activesheet.Range("A1").End(xlToRight).columnn

table = Activesheet.Range("A1:" & Cells(last_row ,last_column).Address)

The code is failing at the 3rd line and I am not too sure why. If I input Cells(last_row ,last_column).Address into the immediate window, it gives me the correct cell. Is it because of the way that I concatenated the range, and is there a better way of doing this

Thank you everyone for your help!

CodePudding user response:

It is possible to retrive the last row and column with UsedRange:

    last_row = ActiveSheet.UsedRange.Rows.Count
    last_column = ActiveSheet.UsedRange.Columns.Count

CodePudding user response:

'try this to determine the last column 
 LastColumn = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
  •  Tags:  
  • Related