+ Reply to Thread
Results 1 to 11 of 11

fastest vba code to add (many) new rows into a protected table at the same time

Hybrid View

  1. #1
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    fastest vba code to add (many) new rows into a protected table at the same time

    Hello. I'm looking for a method to add multiple new rows into a protected table at the same time. I have this
    Dim oList As ListObject                            ' list variable for the Reference Database
    Dim RefTabHeader                                   ' 2-dimensional array for the database header
    
    Private Sub UserForm_Initialize()
    Set oList = ActiveCell.ListObject                  ' set oList to Reference Database
    RefTabHeader = oList.HeaderRowRange.Value          ' populate 2-dimensional array with database header
    End Sub
    
    Private Sub FillInRows(bCopy As Boolean)
    Dim oRow As ListRow
    Dim i As Long
    Application.ScreenUpdating = False
    For i = 1 To CInt(Me.tbNumRows)
       Set oRow = oList.ListRows.Add(ActiveCell.Row - oList.Range.Row + 1)
    Next i
    End Sub
    But I'd rather do it as a single row insert, rather than using the for-next loop. Looping takes too long if I insert many rows, because there's additional code that populates the inserted new rows. Is there a way to insert multiple new rows with only 1 line of code? Any suggestions please?

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    try something like this
    Sub ert()
    Dim i As Long: i = 5
    With ActiveSheet.ListObjects(1)
        .Resize .Range.Resize(.ListRows.Count + i + 1)
    End With
    End Sub

  3. #3
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    I get the error "Cannot use this functionality on a protected sheet". I have in Workbook_Open()
    Sheets("Sheet1").Protect UserInterfaceOnly:=True, AllowInsertingRows:=True, AllowDeletingRows:=True
    shouldnt get this error then?

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    probably, there is only one way
    Sub ert()
    Dim i As Long: i = 5
    With ActiveSheet
        .Unprotect
        With .ListObjects(1)
            .Resize .Range.Resize(.ListRows.Count + i + 1)
        End With
        .Protect UserInterfaceOnly:=True
    End With
    End Sub
    AllowInsertingRows:=True allows you to insert rows into the worksheet, not in a ListObject

  5. #5
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    But wait Nilem!! I have an idea. Maybe... insert rows onto the sheet, instead of inserting directly onto the ListObject?? Is that possible? Because when I did the delete rows code, I believe I was deleting off the sheet, and not referencing the ListObject. What ya think?

  6. #6
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    Ahhh, I didnt know that! Thanks I've been able to delete rows from the table (and with the sheet protected) so I just assumed that inserting new rows should work too. I didnt know there was a difference. Thank you again Nilem

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    I also have an idea Suppose your table is in A1:D10. Run this macro on a protected sheet (with UserInterfaceOnly:=True)
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp)(2, 1).Value = "123"
    , and your table will automatically be raised.
    Perhaps it will be suitable for you.

  8. #8
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    I got the rows to insert by unprotecting the sheet and running your earlier code, but the new rows inserted at the end of the table. I need to insert them after the ActiveCell. Therefore the above wont work either because it inserts at the end

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    Quote Originally Posted by jimmalk View Post
    ... I need to insert them after the ActiveCell
    for this case try
    Sub ertExample()
    Dim i As Long: i = 5
    If Intersect(ActiveCell, ActiveSheet.ListObjects(1).Range) Is Nothing Then _
       MsgBox "active cell is outside the table", 64: Exit Sub
    ActiveCell.Resize(i).EntireRow.Insert
    End Sub

  10. #10
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    tbNumRows is the form text field containing the number of rows to insert. This did the trick
    ActiveCell.Offset(1, 0).Resize(CLng(Me.tbNumRows)).EntireRow.Insert
    It inserts empty rows down from the activecell (and the table is protected). Took only a few seconds of insert a 1000. Other method took about 10 minutes! Perfect!!! Thanks so much Nilem, great work. Un abrazo

  11. #11
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: fastest vba code to add (many) new rows into a protected table at the same time

    I gave you a rep click on an earlier post & now it wont let me give you one on your last post. But I'll remember to get back later for your latest post too. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1