VBA On Error help

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,835
Office Version
  1. 365
Platform
  1. Windows
I have code that is used to clear a table. Below I am showing my two codes, the code that clears the table ("ClearTable"), I incorporated into the first code. I am showing both because I don't know if that impacts what I am trying to do. or not. I can merge the two into one code if you think that is a better way to go. I dint separate them for any special reason.

I am clearing the table and then pasting in data from another table. But I am not pasting it over the first two columns in the second table. Those have formulas in them.

The code to clear the table works great - but only if there is data already in the table. if there isn't data in the second table and the code is run I was getting a debug error. (the table may be empty at times because I am using this on a Template for my users).

Here is what I tried.

Code:
Sub PopulateSQDScub()
'G Heyman DRS NIS

'Insert a blank row inbetween each set of ItemID Duplicates

'Clear table
    ClearTable

'Paste SQD to SQD Scrub

    Range("SQDRange1").Copy
        Range("C11").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

'Add Spaces inbetween each unique ItemID

    Dim i As Long

    Application.ScreenUpdating = False


    With ActiveSheet.ListObjects("TableSQDScrub").Range.Columns(4)
        For i = .Rows.Count + 1 To 3 Step -1
            Do
                i = i - 1
                If i = 2 Then Exit For
            Loop While .Cells(i) = .Cells(i - 1)
                .Rows(i).Insert
                i = i + 1
        Next
    End With

    Application.ScreenUpdating = True

End Sub

Sub ClearTable()
'G Heyman DRS NIS

'Clears the SQD Scrub table before populating with SQD data

On Error GoTo Code_Error
    DoCmd.SetWarnings = False

   With Sheets("SQD Scrub").ListObjects("TableSQDScrub")
      If .ShowAutoFilter Then
         If .AutoFilter.FilterMode Then .AutoFilter.ShowAllData
      End If
      With .DataBodyRange
         .Offset(1).Resize(.Rows.Count - 1).Delete
         .SpecialCells(xlConstants).ClearContents
      End With
   End With
   
Code_Error:
   Exit Sub
   
End Sub

Thanks for your time and help! Very much appreciated
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
DoCmd is an Access object and shouldn't be in this code.

You haven't said what the error is, but I suspect a simple check for the Listrows.count property of the table might fix your problem.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,633
Messages
5,626,004
Members
416,151
Latest member
Openminded intellectual

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
Top