+ Reply to Thread
Results 1 to 23 of 23

how to tell if a table exists in a worksheet

Hybrid View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    how to tell if a table exists in a worksheet

    how can i tell if a table (there will be only 1 per sheet) exists in a worksheet.

    i have tried the following:

    if activesheet.listobjects(1) = True then
    and

    if activesheet.listobjects(1) <> "" then
    how can i get this to work?

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: how to tell if a table exists in a worksheet

    What do you want to do if it does?
    Maybe:

    Sub dmcgovzzzz()
    Dim i As Long
    For i = 1 To Sheets.Count
    If Sheets(i).ListObjects.Count > 0 Then MsgBox Sheets(i) & "Contains a Table"
    Next
    End Sub

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so i used what you said and did the following:

                    Set ws = Sheets("Purchase Requisitions")
                    MsgBox ws.ListObjects(1).Count
                    If ws.ListObjects(1).Count = 0 Then
                        Application.Run "MakeTableWithTotalRow"
                    End If
                    Target.ClearContents
    but the message box never appears, the target.clear doesn't do it and finally it doesn't make the table. what am i doing wrong?

    to answer your question, if there is a table, do nothing. if there is no table, then create one and give a total row.

  4. #4
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    i also tried the following:

                    Set ws = Sheets("Purchase Requisitions")
                    Set listobj = ws.ListObjects(1)
                    On Error GoTo 0
                    
                    'MsgBox ws.ListObjects(1).Count
                    If listobj Is Nothing Then
                        Application.Run "MakeTableWithTotalRow"
                    End If
                    Target.ClearContents

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to tell if a table exists in a worksheet

    Hi,

    It's
    If ws.ListObjects.Count = 0 Then
    and not what you have which is
    If ws.ListObjects(1).Count = 0 Then
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: how to tell if a table exists in a worksheet

    Where is your code? Is it Sheet Code? Where does Target.ClearContents come from? Can you post the entire code?

  7. #7
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    xlnitwit. yup i see that now. :-(

    john, here is the entire sheet code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim AWorksheet As Worksheet, ws As Worksheet
        Dim KeyCells As Range, Sendrng As Range, rng As Range
        Dim subject As String, NewString As String, SelectedRow As String, ApprovedReq As String
        Dim NewPO As String, NewPOCell As String, FullExcelPath As String
        Dim xyz As Long, LastRow As Long
        Dim tbl As ListObject
        Dim listobj As ListObject
        
        
        FullExcelPath = ThisWorkbook.FullName
        'MsgBox FullExcelPath
        
        Application.DisplayAlerts = False
        
        ' The variable KeyCells contains the cells that will
        ' cause an alert when they are changed.
        Set KeyCells = Range("k:k")
        'below is used if there are multiple ranges to work on
        'Set KeyCells = Range("J:J,A:A,Z:Z")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
    
            ' Display a message when one of the designated cells has been changed to "Approved".
            'If Range(Target.Address) = "Approved" Then
            Select Case Target.Text
                Case "Outstanding"
                    MsgBox "Outstanding"
                
                    Target.ClearContents
                Case "AddRow"
                    Application.Run "AddVendorRowsTable"
                    Target.ClearContents
                Case "UpdateNotes"
                'this doesnt work yet.
                    'MsgBox "UpdateNotes"
                    Set ws = Sheets("Purchase Requisitions")
                    'On Error Resume Next
                    'If Not ws.ListObjects(1) = "" Then
                        Set tbl = ws.ListObjects(1)
                        xyz2 = InputBox("What are the new Notes? ", "UpdateNotes", tbl.ListRows.Item(1).Range.Cells(1, 7).Value)
                        tbl.ListRows.Item(1).Range.Cells(1, 9).Value = xyz2
                        'MsgBox xyz
                        Target.ClearContents
                    'End If
                Case "Make Table"
                    Set ws = Sheets("Purchase Requisitions")
                    Set listobj = ws.ListObjects(1)
                    On Error GoTo 0
                    
                    'MsgBox ws.ListObjects(1).Count
                    If ws.ListObjects.Count = 0 Then
                        Application.Run "MakeTableWithTotalRow"
                    End If
                    Target.ClearContents
                Case Else
                    'this should never run as there is data validation on the cells
                    'actually this runs if the Action is a blank cell
                    If Not Target.Text = Null Then MsgBox Target.Text
                    On Error Resume Next
                    'MsgBox "Case Else"
                    
                End Select
        End If
        Application.DisplayAlerts = True
        
    End Sub
    the case statement "Make Table" is the code that i am looking for. does this help?

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to tell if a table exists in a worksheet

    Perhaps
                Case "Make Table"
                    Set ws = Sheets("Purchase Requisitions")
                    If ws.ListObjects.Count = 0 Then
                        Application.Run "MakeTableWithTotalRow"
                    End If
                    Target.ClearContents
    If the MakeTableWithTotalRow routine is in the same workbook, you should call it directly instead of using Run. Also, there is no point to using Range(Target.Address) since it just returns the same range as Target.

  9. #9
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    i tried that and nothing happened. i verified that there is no table on the sheet. i would like to upload a sample workbook but have to get rid of all the data. give me a minute to do that.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to tell if a table exists in a worksheet

    At the very least you might post the code for MakeTableWithTotalRow?

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: how to tell if a table exists in a worksheet

    Maybe?

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim AWorksheet As Worksheet, ws As Worksheet
        Dim KeyCells As Range, Sendrng As Range, rng As Range
        Dim subject As String, NewString As String, SelectedRow As String, ApprovedReq As String
        Dim NewPO As String, NewPOCell As String, FullExcelPath As String
        Dim xyz As Long, LastRow As Long
        Dim tbl As ListObject
        Dim listobj As ListObject
        
        
        FullExcelPath = ThisWorkbook.FullName
        'MsgBox FullExcelPath
        
        Application.DisplayAlerts = False
        Set ws = Sheets("Purchase Requisitions")
        ' The variable KeyCells contains the cells that will
        ' cause an alert when they are changed.
        Set KeyCells = Range("k:k")
        'below is used if there are multiple ranges to work on
        'Set KeyCells = Range("J:J,A:A,Z:Z")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
    
            ' Display a message when one of the designated cells has been changed to "Approved".
            'If Range(Target.Address) = "Approved" Then
            Select Case Target.Text
                Case "Outstanding"
                    MsgBox "Outstanding"
                
                    Target.ClearContents
                Case "AddRow"
                    Application.Run "AddVendorRowsTable"
                    Target.ClearContents
                Case "UpdateNotes"
                'this doesnt work yet.
                    'MsgBox "UpdateNotes"
                    'On Error Resume Next
                    'If Not ws.ListObjects(1) = "" Then
                    If ws.ListObjects.Count > 0 Then GoTo zz
                        Set tbl = ws.ListObjects(1)
                        xyz2 = InputBox("What are the new Notes? ", "UpdateNotes", tbl.ListRows.Item(1).Range.Cells(1, 7).Value)
                        tbl.ListRows.Item(1).Range.Cells(1, 9).Value = xyz2
                        'MsgBox xyz
                        Target.ClearContents
                    'End If
                Case "Make Table"
                    If ws.ListObjects.Count > 0 Then GoTo zz
                    Set listobj = ws.ListObjects(1)
                    On Error GoTo 0
                    
                    'MsgBox ws.ListObjects(1).Count
    
                        Application.Run "MakeTableWithTotalRow"
                    Target.ClearContents
                Case Else
                    'this should never run as there is data validation on the cells
                    'actually this runs if the Action is a blank cell
                    If Not Target.Text = Null Then MsgBox Target.Text
                    On Error Resume Next
                    'MsgBox "Case Else"
                    
                End Select
        End If
    zz:
        Application.DisplayAlerts = True
    
    End Sub

  12. #12
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    john, i tried that and it appears to do nothing.

    xlnitwit, here is the code for maketablewithtotalrow but now i can see that it needs to be fixed. i originally created a table called prtable. this worked when it was just a macro and not fired from row k as an action. so i am sure this needs to be recoded.

    Sub MakeTableWithTotalRow()
    '
    ' Macro1 Macro
    '
    'ActiveSheet.ListObjects("Table3").Delete
    '
        With Sheets("Purchase Requisitions")
        Range("A1").Select
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$G$16"), , xlYes).Name = _
            "PRTable"
        'Range("A13").Select
        ActiveSheet.ListObjects("PRTable").ShowTotals = True
        Range("PRTable[[#Totals],[Amount]]").Select
        ActiveSheet.ListObjects("PRTable").ListColumns("Amount").TotalsCalculation = _
            xlTotalsCalculationSum
        ActiveSheet.ListObjects("PRTable").TableStyle = "TableStyleLight21"
        End With
    End Sub
    im going to recode for just a messagebox to see if the action is working as expected.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to tell if a table exists in a worksheet

    Perhaps this version
    Sub MakeTableWithTotalRow()
     Dim LO as Listobject
    
        With Sheets("Purchase Requisitions")
           Set LO = .ListObjects.Add(xlSrcRange, .Range("$A$1:$G$16"), , xlYes)
        End With
        With LO
           .Name = "PRTable"
           .ShowTotals = True
           .ListColumns("Amount").TotalsCalculation = xlTotalsCalculationSum
           .TableStyle = "TableStyleLight21"
        End With
    End Sub

  14. #14
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so made some progress with your help.

    john, made clerical error (i so hate spaces), now routine is working as you wrote it.

    xlnitwit,

    thanks for that code. made a few changes and it looks like this:

    Sub MakeTableWithTotalRow()
    Dim LO As ListObject
    
        With Sheets("Purchase Requisitions")
            .UsedRange
            LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
           Set LO = .ListObjects.Add(xlSrcRange, .Range("$A$1:$G$" & LastRow), , xlYes)
        End With
        With LO
           .Name = "PRTable"
           .ShowTotals = True
           .ListColumns("Amount").TotalsCalculation = xlTotalsCalculationSum
           .TableStyle = "TableStyleLight21"
        End With
    End Sub
    wanted to account for dynamic data (not a fixed row) so lastrow does that. so that helps to make this action work as expected.

    thanks to both of you.

  15. #15
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: how to tell if a table exists in a worksheet

    You're welcome. Glad I could contribute, and thanks for the feedback and reps.

  16. #16
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so im working on the update notes case. got the if statement working for either table or range. but i need help with the range part. tried to do offset, but couldn't make the syntax correct. what am i doing wrong?


    Case "UpdateNotes"
                'this doesnt work yet.
                    'MsgBox "UpdateNotes"
                    Set ws = Sheets("Purchase Requisitions")
                    'On Error Resume Next
                    'If Not ws.ListObjects(1) = "" Then
                    If ws.ListObjects.Count > 0 Then
                        Set tbl = ws.ListObjects(1)
                        xyz2 = InputBox("What are the new Notes? ", "UpdateTableNotes", tbl.ListRows.Item(1).Range.Cells(1, 7).Value)
                        tbl.ListRows.Item(1).Range.Cells(1, 7).Value = xyz2
                        'MsgBox xyz
                        Target.ClearContents
                    Else
                        'MsgBox Target.Address
                        'rng3 = Range(Target.Offset(0, -4)).Value
                        'MsgBox rng3
                        rng2 = Range(Target.Address).Text
                        'MsgBox "Not a Table"
                        xyz2 = InputBox("What are the new Notes? ", "UpdateRangeNotes", rng2)
                        rng2 = xyz2
                        'Taget.ClearContents 'dont do this till the else works as expected
                    'End If
                    End If
    so the target.address correctly identifies the changed cell(col K), but the field i want to update is in column G so that is why i tried the offset that i did.

    any help is appreciated.

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to tell if a table exists in a worksheet

    Target is a range object so you only need
    Target.Offset(0, -4).Value

  18. #18
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    thanks xlnitwit, that worked.

    i also found this to work as well

    NotesRng = Cells(Target.Row, 7).Value

  19. #19
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so i put the following code in my "Closed POs" sheet.

    [CODE]
    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range

    Dim FullExcelPath As String

    FullExcelPath = ThisWorkbook.FullName
    'MsgBox FullExcelPath

    Application.DisplayAlerts = False

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("d:d")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then
    Select Case Target.Value
    Case Is < Now - 30
    MsgBox "Move to Closed PO archive file"
    Case Is >= Now - 30
    MsgBox "Keep for now"
    Case Else
    MsgBox "Case Else"
    End Select
    End If
    End Sub
    /CODE]

    so the stub works as expected, when i change dates it goes through the case statement properly. but what i really want is for this to be a OPEN worksheet event as well. it should check the Aging date in column D and see if its a month old or older.

    i figure this should be a worksheet event but im not sure of how to transfer the code in the stub to the ThisWorkbook code sheet.

    thoughts?
    Last edited by dmcgov; 08-23-2017 at 08:44 AM.

  20. #20
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: how to tell if a table exists in a worksheet

    There is a Workbook Sheet Change Event which is appied to every sheet if that's what you're looking for?

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim KeyCells As Range
    
     Dim FullExcelPath As String
    
     FullExcelPath = ThisWorkbook.FullName
     'MsgBox FullExcelPath
    
     Application.DisplayAlerts = False
    
     ' The variable KeyCells contains the cells that will
     ' cause an alert when they are changed.
     Set KeyCells = Range("d:d")
    
     If Not Application.Intersect(KeyCells, Range(Target.Address)) _
     Is Nothing Then
     Select Case Target.Value
     Case Is < Now - 30
     MsgBox "Move to Closed PO archive file"
     Case Is >= Now - 30
     MsgBox "Keep for now"
     Case Else
     MsgBox "Case Else"
     End Select
     End If
    End Sub
    Or if you're looking too activate the sheet on Workbooks Open Event then:

    Private Sub Workbook_Open()
    Sheets("Purchase Requisitions").Activate
    End Sub

  21. #21
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    John,

    Neither work as I expect. The first didnt work as expected, and the second error'ed out with a "run time error - 1004, Activate method of worksheet class failed" code. Let me explain what i am looking for.

    i have a worksheet change event that fires on the "Closed POs" sheet. the keycell is column D. and it works as expected, when i change a date, i get a messagebox. which is what i want for this stub. So lets say that i put a date in D2 that is 7/25/17 which is one day less than the 30 (in my code) so the message box says to "Keep for Now". Then I save the file. When I return to the file the next day and open it, i want the workbook open event to check all the cells in column d to see if they need to get moved to the closed archived file or just leave it alone (and give me a messagebox).

    How can that be done?

  22. #22
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so i realize that the code will have to be completely overhauled. cant use intersect cause the date might already be old without changing the value on the sheet. the code would have to look at just the "Closed POs" sheet, examine each cell in the target.range and then do a messagebox. how can i do that?

  23. #23
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to tell if a table exists in a worksheet

    so this last question doesn't go with the OP, so im going to close this and open up a new case.

    thanks to everyone for their help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Can't name a table as the name exists, but it does not!?
    By brucemc777 in forum Excel General
    Replies: 0
    Last Post: 11-04-2015, 08:05 PM
  2. [SOLVED] If Worksheet Exists
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-17-2015, 11:16 AM
  3. Check if Worksheet exists and Insert Worksheet
    By ddanmcgrew in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2012, 03:19 PM
  4. Replies: 6
    Last Post: 08-04-2011, 12:18 PM
  5. worksheet exists or not
    By skvabbili in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2006, 05:50 PM
  6. Worksheet exists
    By Kaval in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2006, 04:55 AM
  7. check if worksheet exists
    By joeeng in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM

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