+ Reply to Thread
Results 1 to 5 of 5

New to VBA

  1. #1
    Registered User
    Join Date
    03-17-2015
    Location
    Zürich
    MS-Off Ver
    mac 2011 / office 365
    Posts
    2

    New to VBA

    Hi everyone,

    I'm new to VBA and I'm looking for a way to insert a new row at the end of my table.

    The row needs to use all formats and formulas from the row above.

    I found a code which I tried to implement, but it is not working...

    (As far as I understand, I only have to make changes to the tablename and sheetname...., or are there other things to consider?)

    Can someone please tell me what is wrong with the code or what else I need to do in order to be able to use this code?

    I would appreciate your help!

    Thanks in advance
    __________________________________________________________

    Sub AddDataRow(Table1 As String, values() As Variant)
    Dim sheet As Worksheet
    Dim table As ListObject
    Dim col As Integer
    Dim lastRow As Range

    Set sheet = ActiveWorkbook.Worksheets("Sheet1")
    Set table = sheet.ListObjects.Item(Table1)

    'First check if the last row is empty; if not, add a row
    If table.ListRows.Count > 0 Then
    Set lastRow = table.ListRows(table.ListRows.Count).Range
    For col = 1 To lastRow.Columns.Count
    If Trim(CStr(lastRow.Cells(1, col).Value)) <> "" Then
    table.ListRows.Add
    Exit For
    End If
    Next col
    End If

    'Iterate through the last row and populate it with the entries from values()
    Set lastRow = table.ListRows(table.ListRows.Count).Range
    For col = 1 To lastRow.Columns.Count
    If col <= UBound(values) + 1 Then lastRow.Cells(1, col) = values(col - 1)
    Next col
    End Sub

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: New to VBA

    You might have to change
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,028

    Re: New to VBA

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  4. #4
    Registered User
    Join Date
    03-17-2015
    Location
    Zürich
    MS-Off Ver
    mac 2011 / office 365
    Posts
    2

    Re: New to VBA

    Thanks... but it did still not work...

    I changed the code now to:

    Sub AddDataRow()

    Dim Table1 As String
    Dim sheet As Worksheet
    Dim table As ListObject

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.DisplayAlerts = False

    Set sheet = ActiveWorkbook.Worksheets("Sheet1")
    Set table = sheet.ListObjects.Item("Table1")

    table.ListRows(table.ListRows.Count).Range.Copy
    table.ListRows(table.ListRows.Count).Range.Insert Shift:=xlDown
    table(table.ListRows.Count, Columns("e" & "g")).ClearContents
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub


    However, I'm struggling now with another problem which is that I want to have the contents of some specific cells deleted when the row gets copied and that part is still not working... :-(

  5. #5
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: New to VBA

    You have
    Please Login or Register  to view this content.
    Should it be True?
    and/or this is
    Please Login or Register  to view this content.
    Should it be Cut instead of Copy?
    Also, I don't think you can use Dim sheet as Worksheet as I don't think worksheet is a valid variable
    Along those lines, Dim table as ListObject might not be valid either
    If I'm not mistaken, the word after As must change to blue text to be valid
    You have to put "Code Tags" around your code.....Click the # above and insert your code between the 2 words [CODE]
    Last edited by gmr4evr1; 03-17-2015 at 07:38 PM. Reason: Added code tag instructions

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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