Please could you help with Named Table

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I need code to stop running when a Table called Table2 is already there. Some reason I can`t get the Function to work with main code?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim Rng
    Dim Result As Integer
    
    Call DeleteTbl
    
    If Selection.Count > 1 Then Exit Sub
    Set Rng = Range("J:J")
    If Not Intersect(Target, Rng) Is Nothing Then
        
        Result = MsgBox("Need to Send Depot Email Yes/No", vbInformation + vbYesNo, "Send Email Yes/No")
        
        Select Case Result
            Case vbYes
                Call Email_Depot
            Case vbNo
                Exit Sub
        End Select
        
    End If
    
    Range("A" & Cells.Rows.Count).End(xlUp).Select
    
    Call CreateTbl
    
If TableExists = True Then

        Exit Sub
        
End Sub
Function TableExists(tableName As String, sheetName As String) As Boolean

    Dim targetSheet As Worksheet
    Set targetSheet = Worksheets("Data")

    Dim tbl As ListObject

    With targetSheet
        For Each tbl In .ListObjects
            If tbl.Name = "Data2" Then TableExists = True
        Next tbl
    End With

End Function
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The call to TableExists in the SelectionChange event does not have any arguments:
VBA Code:
If TableExists = True Then
I think this should look more like this:
VBA Code:
If TableExists("Data2", "Data") Then

In addition, your function is not using its arguments, I think it should read:
VBA Code:
Function TableExists(tableName As String, sheetName As String) As Boolean

    Dim tbl As ListObject

    With Worksheets(sheetName)
        For Each tbl In .ListObjects
            If tbl.Name = tableName Then 
               TableExists = True
               Exit For
           End If
        Next tbl
    End With

End Function
 
Upvote 0
This Function works fine but when I run it the code keeps going back to Call "CreateTbl". But I need it to Exit Sub after it finds the table in the sheet.
 
Upvote 0
In that case, change this bit:

VBA Code:
    Call CreateTbl
    
If TableExists = True Then

To:

VBA Code:
If Not TableExists Then
    CreateTbl
End If
 
Upvote 0
All works well now but what I don`t understand is why it keeps highlighting the table even when the code is complete??
 
Upvote 0
Place a breakpoint on the first line of code (click on the line of code and press the F9 key). Then allow the code to run by selecting a different cell. Use the F8 key to step through the code. Make sure you place the Excel window next to the VBA Editor so you can see what is happening.
 
Upvote 0

Forum statistics

Threads
1,224,727
Messages
6,180,583
Members
452,988
Latest member
wcself81

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top