VBA On Error help

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,338
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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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.
 
Upvote 0

Forum statistics

Threads
1,214,575
Messages
6,120,344
Members
448,956
Latest member
Adamsxl

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