Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

copying and pasting table

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Copying and Pasting a table and then Deleting Rows

Copying and Pasting a table and then Deleting Rows

ResolvedVersion 2007

Sam has attended:
Excel VBA Intro Intermediate course

Copying and Pasting a table and then Deleting Rows

Hi again,

I would like to copy and past a table and then delete empty rows. I've written two bits of code that work, one is a loop with a do while loop that deletes a row as long as it is empty and then repeats the loop. The other is an if statement and if the row is empty it deletes it and then sets i to 1-1 and loops. Both take a very long time- is there a quicker way of doing this?

Thanks,
Sam

RE: Copying and Pasting a table and then Deleting Rows

Hi Sam

Thanks for getting in touch.

Does your code 'select' the rows while it deletes? This can slow things right down. You could try something like:

dim r As Range, rows As Long, i As Long
Set rng = ActiveSheet.Range("A1:J50")
rows = rng.rows.Count
For i = rows To 1 Step (-1)
If WorksheetFunction.CountA(rng.rows(i)) = 0 Then rng.rows(i).Delete
Next i

This FOR NEXT loop loops backwards through the range A1:J50, and if a COUNTA function returns zero, delete the row (you go backwards as deleting an entire row causes the selection to shuffle 'up').

A really neat way to improve performance is to remove some of the automation services:

With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

Disables formula calculation and screen updates. You should insert this at the top of your code, with the corresponding code at the bottom:

With Application
.Calculation = Automatic
.ScreenUpdating = True
End With

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Mon 3 Dec 2012: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Shortcuts for working with named ranges in Excel

If you are working with or creating named ranges in your spreadsheets, then you may find the following shortcut keys useful.

- Bring up the Define Names dialogue box on screen by using Ctrl + F3 (instead of going to Insert - Names).

- Create Names from labels you have entered into the spreadsheet by highlighting the labels and related figures, then hold down Shift + Ctrl + F3. You can then choose to create names from the top or bottom rows, or left or right columns.

- Go directly to a named range by hitting the F5 key. The Go To dialogue box will open and display any named ranges in the spreadsheet. Simply select the named range to navigate to it in the spreadsheet.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.