in Education by
I have 10 worksheets. I want to create a table for each. every table has a different amount of data, ive been using the following code for each, but i was wondering how to do it with a loop. I would truly apreciate some help :) Sub table() Dim sht As Worksheet Dim lastrow As Long Dim LastColumn As Long Dim StartCell As Range Set sht = Worksheets("m9") Set StartCell = Range("A1") lastrow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column sht.Range(StartCell, sht.Cells(lastrow, LastColumn)).Select Dim objTable As ListObject Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes) End Sub Tried the following, but with no luck Sub loop_test() Dim i As Integer Dim ws_num As Integer Dim starting_ws As Worksheet Set starting_ws = ActiveSheet ws_num = ThisWorkbook.Worksheets.Count For i = 1 To ws_num ThisWorkbook.Worksheets(i).Activate ' Dim lastrow As Long Dim LastColumn As Long Dim StartCell As Range Set StartCell = Range("A1") lastrow = Cells(Rows.Count, StartCell.Column).End(xlUp).Row Range(StartCell, Cells(lastrow, LastColumn)).Select Dim objTable As ListObject Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes) Next starting_ws.Activate End Sub JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You should avoid Activate and Select statements. The following will loop through all worksheets in the workbook and add a ListObject to each sheet. It will also test to see whether there is an already existing ListObject. If the existing ListObject overlaps with the range that you're going to add the table into to, it will convert it to a range before recreating the ListObject Sub loop_test() Dim ws As Worksheet Dim StartCell As Range, TblRng As Range Dim LastRow As Long, LastColumn As Long Dim objTable As ListObject For Each ws In ThisWorkbook.Sheets Set objTable = Nothing With ws Set StartCell = .Range("A1") LastRow = .Cells(.Rows.Count, StartCell.Column).End(xlUp).Row LastColumn = .Cells(StartCell.Row, .Columns.Count).End(xlToLeft).Column Set TblRng = .Range(StartCell, .Cells(LastRow, LastColumn)) ' Test if table exists on sheet On Error Resume Next Set objTable = .ListObjects(1) On Error GoTo 0 ' If table overlaps with TblRng - Convert to Range If Not Intersect(objTable.Range, TblRng) Is Nothing Then objTable.Unlist End If ' Create Table Set objTable = .ListObjects.Add(xlSrcRange, TblRng, , xlYes) End With Next ws End Sub

Related questions

0 votes
    I have 10 worksheets. I want to create a table for each. every table has a different amount of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have encountered a problem during my work. There are over one hundred worksheets in my excel, and I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a table: Using vba I need to add a column after UsedRange, add a header and fill cells ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a table: Using vba I need to add a column after UsedRange, add a header and fill cells ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Extracting Unique values from a list (2 answers) Closed 7 years ago ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    How do I transfer data from a worksheet to another based on a specific cell value? I tried several ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Below, I have code that sends a personalized SMS message and includes the name. I got that part to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am trying to do some validation on the file name before it's saved. Here are my three file- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    Below, I have code that sends a personalized SMS message and includes the name. I got that part to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    We have over 10 computers on our network and everyone's computer opens up the file with no issue. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    How do I transfer data from a worksheet to another based on a specific cell value? I tried several ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to adjust the row height based on the cell value. The operation have to run through ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I have set up a new, empty, modeless userform, to fix my problem with the least amount of code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I am trying to export p-values to Excel from the Stata community-contributed command reghdfe: // get data ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I would like to move an entire row to the bottom of a spread sheet if the column k contains a 0 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...