Dim a Long variable, use it to store the value of the last used row, then use that in your processes:

Dim LR as Long





    LR = Range("A" & Rows.Count).End(xlUp).Row

    Range("A1:Q" & LR).Select
    ActiveSheet.ListObjects("Table8").TableStyle = "TableStyleMedium2"