Copying from one tab to another - problem: its copying titles if there is no data in Row 3 or higher

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I have this code that works great if there is data in row 3. It moves all the data as I need it to if there is data in column a in row 3 down. But if there is not data its copying row 2 which is the titles of the columns

how can I adjust this so that is there is no data from row 3 down it doesn't paste or move the data. I still want it to clear Table7\

Thanks for the Help!

Code:
Sub MoveCStoCSTable()

Sheets("CS Table").Visible = True

'***************************
'Clear Table'
'***************************

      Sheets("CS Table").Select

    ActiveSheet.ListObjects("Table7").HeaderRowRange.Select
    'Remove the filters if one exists.
    If ActiveSheet.FilterMode Then
    Selection.AutoFilter
    End If
    
    'Clear all lines but the first one in the table leaving formulas for the next go round.
    With Worksheets("CS Table").ListObjects("Table7")
       .Range.AutoFilter
        On Error Resume Next
        .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete

            If .ListColumns.Count > 1 Then
                .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
             Else
                    With .DataBodyRange.Cells(1)
            If Not .HasFormula Then .ClearContents

                    End With
            End If

    End With
    
'************************************
'Repopulate table with cost Source
'************************************
    
 Dim CSLR As Long
 
 CSLR = Sheets("Cost Sources").Cells(Rows.Count, "A").End(xlUp).Row
 
 
    Sheets("Cost Sources").Range("A3:" & "AE" & CSLR).Copy
 
    Sheets("CS Table").Range("B11").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
          Application.CutCopyMode = False
        


        

'   Application.Goto Sheet18.Range("B16")


End Sub

Thank you!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
After this line:
VBA Code:
CSLR = Sheets("Cost Sources").Cells(Rows.Count, "A").End(xlUp).Row
you can check to see if the value is less than 3, and if it is, exit, i.e.
VBA Code:
If CSLR < 3 Then Exit Sub
 
Upvote 0
Solution
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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