0

I am trying to code a macro that would resize the table to fit the content. Right now I have this code and I keep getting the error that my range is invalid. What am I doing wrong? Thanks

Workbooks("A-50 -Suivi des risques V.0.xlsm").Sheets("Feuil1").Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

Dim SelRange As Range
Set SelRange = Selection

Workbooks("A-50 -Suivi des risques V.0.xlsm").Sheets("Feuil1").ListObjects("Tableau12").Resize Range(SelRange)
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • `.Resize selRange`. `selRange` is already a `Range` and should not be enclosed in a `Range` call. – BigBen Aug 03 '21 at 14:41
  • Don't use `Select` that is a very bad practice. See [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) if you want your code to be reliable. – Pᴇʜ Aug 03 '21 at 14:41

1 Answers1

0

Try:

With Workbooks("A-50 -Suivi des risques V.0.xlsm").Sheets("Feuil1")
    .ListObjects("Tableau12").Resize .Range("A1").CurrentRegion
End With
Tim Williams
  • 154,628
  • 8
  • 97
  • 125