Delete only table data using VBA

craig2525

New Member
Joined
Oct 30, 2018
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hi all. I have a sheet that I need to clear the contents of a table but not the entire sheet. The table is in columns B through U. I have other data in the sheet that I do not want to delete. They are in columns AA through AN. I have tried protecting the cells but I get and error when doing this. Here is the code I am using.

Excel 5.PNG
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Here's a couple links to pages dealing with tables that may be of interest to you
 
Upvote 0
I was going to suggest changing your delete line to this:-
VBA Code:
.DataBodyRange.Rows.Delete
But even your code only deletes the rows in the table and does not impact the rows to the right outside the table.

What is the actual issue ?
 
Last edited:
Upvote 0
With this code (originally posted), it was deleting data outside the table.
 
Last edited:
Upvote 0
Sub clearTable()

With toSched.ListObjects("ToScheduleTbl")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.ClearContents

End If
End With

End Sub

I tried this and it didn't delete the other data outside the table but wiped out the formulas in the table.
 
Upvote 0
With the clear contents approach you need to add xlCellTypeConstants into the mix and then a resize table.
I am not finding the need to go through all that trouble though.

With this code I am getting the results in the picture below.
Obviously if I have rows out the side that reference the rows being deleted they will change to #REF!

If it doesn't work for you perhaps you can give a us a sample workbook via a sharing platform to test on.

VBA Code:
Sub testdelete()
    Dim sht As Worksheet
    Dim tbl As ListObject
    
    Set sht = ActiveSheet
    Set tbl = sht.ListObjects("ToScheduleTbl")

    tbl.DataBodyRange.Delete
End Sub
 

Attachments

  • TableDeleteRows.jpg
    TableDeleteRows.jpg
    225.7 KB · Views: 11
Upvote 0
Do you use Dropbox, OneDrive, GoogleDrive or any other sharing mechanism ?
Put a copy of your s/sheet there without any confidential data and then share it as public to anyone with the link and then post the link here.

If you want to try using XL2BB first then we could do that, given the nature of your issue I am just not sure that it will come through using that method.
 
Last edited:
Upvote 0
Schedule Tool.xlsm
ABCDEFGKLMNOPQUVWXY
1
2WO NOOrgEquipmentEQUIPMENT DESCPM CodePM DESC1 WO Owner2 Sched Block3 Shift4 SupervisorSched Start DateSched End Date5 PlannedTech Labor
464196589AAAPCKPACKERA2PACKERSHAWN   12/31/202112/31/2021+0.00
564196590AAARCVRECEIVEA3RECEIVEJOHN   12/31/202112/31/2021+0.00
664196592AAASHPSHIPPERA4SHIPPERBRIAN   12/31/202112/31/2021+0.00
764196594AAASHPSHIPPERA5SHIPPERBRIAN   12/31/202112/31/2021+0.00
864196596AAASHPSHIPPERA6SHIPPERJOHN   12/31/202112/31/2021+0.00
964196598AAA1000086692AUGERA7AUGERBRIAN   12/31/202112/31/2021+0.00
1064196599AAA1000086801AUGERA8AUGERBRIAN   12/31/202112/31/2021+0.0012/3/202112/4/202112/5/2021
1164196600AAA1000086189COMPRESSORA9COMPRESSORBRIAN   12/31/202112/31/2021+0.00FridaySaturdaySunday
1264196601AAA1000086190COMPRESSORA10COMPRESSORBRIAN   12/31/202112/31/2021+0.00KEVINKENPAM
1364196602AAA1000086191BOX MACHINEA11BOX MACHINESHAWN   12/31/202112/31/2021+0.00SAMBRIANJILL
1464196603AAA1000086192BOX MACHINEA12BOX MACHINESHAWN   12/31/202112/31/2021+0.00MIKENATEANGIE
1564196785AAAPCKPACKERA13PACKERSHAWN   12/31/202112/31/2021+0.00
1664196786AAAPCKPACKERA14PACKERSHAWN   12/31/202112/31/2021+0.00
1764196788AAARCVRECEIVEA15RECEIVEJOHN   12/31/202112/31/2021+0.00
1864196791AAASHPSHIPPERA16SHIPPERJOHN   12/31/202112/31/2021+0.00
1964196793AAASHPSHIPPERA17SHIPPERJOHN   12/31/202112/31/2021+0.00
2064196794AAASHPSHIPPERA18SHIPPERBRIAN   12/31/202112/31/2021+0.00
2164196795AAAB.ENCLOSURE.137EnclosureA19EnclosureKEVIN   12/31/202112/31/2021+0.00
2264196796AAAC.INTERIOR.137ConstructionA20ConstructionKEVIN   12/31/202112/31/2021+0.00
2364196797AAAFLS.FASFIRE ALARMA21FIRE ALARMKEVIN   12/31/202112/31/2021+0.00
2464196838AAA1000085910COMPRESSORA22COMPRESSORBRIAN   12/31/202112/31/2021+0.00
2564196903AAAA100002COMPRESSORA23COMPRESSORTIM   12/31/202112/31/2021+0.00
2664197125AAA1000086988AUGERA24AUGERSHAWN   12/31/202112/31/2021+0.00
2764197126AAA1000086120AUGERA25AUGERSHAWN   12/31/202112/31/2021+0.00
2864197127AAA1000086122AUGERA26AUGERJOHN   12/31/202112/31/2021+0.00
2964197128AAA1000086121AUGERA27AUGERJOHN   12/31/202112/31/2021+0.00
3064234621AAAPCKPACKERA27PACKERBRIAN   1/1/20221/1/2022+0.00
3164234622AAAPCKPACKERA29PACKERBRIAN   1/1/20221/1/2022+0.00
"TO SCHEDULE" DOWNLOAD
Cell Formulas
RangeFormula
L4:L31L4=IFERROR(INDEX(#REF!,MATCH([@[Unique Code (Do Not Transfer to Upload Template)]],#REF!,0)),"")
M4:N31M4=IFERROR(INDEX(#REF!,MATCH([@[1 WO Owner]],#REF!,0)),"")
U4:U31U4=SUMIFS([AVG LABOR FOR PM,EQUIP, AND ORG],[1 WO Owner],[@[1 WO Owner]],[Sched Start Date],[@[Sched Start Date]])
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING SORTER/INDUCT/RECIRC STROBE FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING SORTER/INDUCT/RECIRC STROBE BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING SORTER/ INDUCT/ RECIRC DAILY PM"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING LANE STROBE FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING LANE STROBE BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING LANE DAILY PM"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING COLLECTORS STROBE FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING COLLECTORS STROBE BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT SHIPPING COLLECTOR DAILY PM"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT RECEIVE STROBE FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT RECEIVE STROBE BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT RECEIVE DAILY"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANES STROBE 2/2 FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANES STROBE 1/2 FRONT HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANES STROBE 2/2 BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANES STROBE 1/2 BACK HALF"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANE 2/2 DAILY"textNO
G:GCell Valuecontains "TRADITIONAL NON-SORT PACK LANE 1/2 DAILY"textNO
Cells with Data Validation
CellAllowCriteria
K4:K31List=TECH_LOGIN_LIST
 
Upvote 0
Function findFilePath()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select a file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
findFilePath = .SelectedItems(1)
End If
End With
End Function
Private Function findConnStr(fileEx As String, filePath As String)
Select Case fileEx

Case "xlsx"
findConnStr = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & filePath & ";" & _
"Extended Properties='Excel 12.0 Xml;" & _
"HDR=YES';"

Case "xlsm"
findConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & filePath & ";" & _
"Extended Properties='Excel 12.0 Macro;HDR=YES';"

End Select
End Function
Sub ImportEAMexport()
Dim filePath As String: filePath = findFilePath
If filePath = "" Then Exit Sub
Dim fileEx As String: fileEx = Right(filePath, Len(filePath) - InStrRev(filePath, "."))
Dim shtName As String
Dim cFile As ADODB.Connection
Dim rs As ADODB.Recordset
Set cFile = New ADODB.Connection

Select Case fileEx

Case "csv"
nameFile = Dir(filePath)
filePathLoc = Left(filePath, InStrRev(filePath, "\"))
cFile.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & filePathLoc & ";Extensions=asc,csv,tab,txt;"

cFile.Open

Set rs = New ADODB.Recordset
With rs
.ActiveConnection = cFile
.Source = "SELECT * FROM [" & nameFile & "]"
.Open
End With


Case Else
cFile.ConnectionString = findConnStr(fileEx, filePath)

cFile.Open
upload_frm.sht_list.Clear
Set rsSht = cFile.OpenSchema(adSchemaTables)
Do While Not rsSht.EOF
upload_frm.sht_list.AddItem rsSht.Fields("TABLE_NAME").Value
rsSht.MoveNext
Loop
rsSht.Close

If upload_frm.sht_list.ListCount > 1 Then
upload_frm.Show
shtName = upload_frm.selsht
Else
shtName = upload_frm.sht_list.List(0)
End If

Set rs = New ADODB.Recordset
On Error GoTo rsError
With rs
.ActiveConnection = cFile
.Source = "SELECT [Work Order], [Organization], [Equipment], [Equipment Description], [PM Code], [Description], 'R',[Department], 'V', [1 - WO Owner], '', '', '', [Sched# Start Date], [Sched# End Date], '+', '+' FROM [" & shtName & "]"
.Open
End With
On Error GoTo 0

End Select

clearTable

toSched.Range("B3").CopyFromRecordset rs

rs.Close
cFile.Close


schedblock = "=IFERROR(INDEX(SCHED_BLOCK[2 Sched Block],MATCH([@[Unique Code (Do Not Transfer to Upload Template)]],SCHED_BLOCK[Unique Code (autofill)],0)),"""")"
shift = "=IFERROR(INDEX(TECH_INFO[SHIFT CODE],MATCH([@[1 WO Owner]],TECH_INFO[TECH LOGIN],0)),"""")"
super = "=IFERROR(INDEX(TECH_INFO[SUPERVISOR],MATCH([@[1 WO Owner]],TECH_INFO[TECH LOGIN],0)),"""")"
lbr = "=IFERROR(INDEX(SCHED_BLOCK[AVG LABOR FOR PM,EQUIP, AND ORG],MATCH([@[Unique Code (Do Not Transfer to Upload Template)]],SCHED_BLOCK[Unique Code (autofill)],0)),"""")"
Unicode = "=CONCATENATE([Equipment],""."",[PM Code])"

Range("l3") = schedblock
Range("m3") = shift
Range("n3") = super
Range("s3") = lbr
Range("t3") = Unicode
Exit Sub

rsError:
MsgBox "Could not import selected sheet. Check if selected sheet has the correct layout items and try again."

End Sub

Sub clearTable()

With toSched.ListObjects("ToScheduleTbl")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,377
Members
449,097
Latest member
Jabe

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