• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

A Table cant overlap another table

Vishalunleashed

New Member
Dear Excel Masters,

I am intending to check if the table exists in the file while running the macro. If it exists then it should unlist or delete it and if does not then it should add it. Following is the code that I came up with. Somehow it is not completely checking the if conditions:

Sub AddDevices()
Dim tblExists As Boolean
tblExists = False
For Each o In Sheets("Input").ListObjects
If o.Name = "Scan Your Devices Here" Then tblExists = True
Next o

If (tblExists) Then
Sheets("Input").ListObjects("Scan Your Devices Here").Unlist
End If


Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$L$4"), , xlYes).Name = "Table1"

Range("Table1[[#Headers],[Column1]]").Select
ActiveCell.FormulaR1C1 = "Scan Your Devices Here"
ActiveSheet.ListObjects("Table1").Resize ActiveSheet.ListObjects("Table1").Range.Resize(Range("H1").Value)
Columns("L:M").Select
Selection.EntireColumn.Hidden = False
Range("L5").Select
End Sub

Thank you!
 
I'd guess that the table name Scan Your Devices Here doesn't exist. The table name isn't what's in the top left header.
Your line:
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$L$4"), , xlYes).Name = "Table1"
gives the name Table1 to the new table, not Scan Your Devices Here
 
Impossible to advise further. Could there be a third table somewhere?
Need to see the file.
 
Dear P45cal,

Please find the attached report. The macro is add devices of 1st tab (input). The whole idea ; it should have ability to override the table if user has mistakenly added wrong number of rows.

Thank you!
 

Attachments

  • Macro_DB.xlsm
    86.1 KB · Views: 14
A few tweaks to your code:
Code:
Sub AddDevices()
Dim tblExists As Boolean
tblExists = False
For Each o In Sheets("Input").ListObjects
  If o.Name = "ScanYourDevicesHere" Then tblExists = True
Next o

If tblExists Then
  Sheets("Input").ListObjects("ScanYourDevicesHere").Unlist
End If

With ActiveSheet.ListObjects.Add(xlSrcRange, Range("$L$4"), , xlYes)
  .Name = "ScanYourDevicesHere"
  .HeaderRowRange(1).Value = "Scan Your Devices Here"
  .Resize .Range.Resize(Range("H1").Value)
End With
Columns("L:M").EntireColumn.Hidden = False
Range("L5").Select
End Sub
As a once-only operation, before you run this, you need to ensure that any existing table is named "ScanYourDevicesHere" (no spaces allowed). The easiest way to do that is to delete the exisiting table and run this once.
 
Back
Top