Please help me shorten this.

O177812

Board Regular
Joined
Apr 16, 2015
Messages
82
Office Version
  1. 365
  2. 2021
I have created the following VBA code to copy 3 columns of data onto a different sheet if Column AQ contains a "0"

ActiveWorkbook.Worksheets("IN PROGRESS").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("IN PROGRESS").AutoFilter.Sort.SortFields.Add Key:= _
Range("AQ2:AQ224"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("IN PROGRESS").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$2:$AQ$224").AutoFilter Field:=43, Criteria1:="0"
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("COMPLETE").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("IN PROGRESS").Select
ActiveWindow.SmallScroll Down:=-27
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("COMPLETE").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("IN PROGRESS").Select
ActiveWindow.SmallScroll Down:=-21
Range("G3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("COMPLETE").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").EntireColumn.AutoFit

I am hoping someone can help me create a shorter way of doing this.

It would be awesome if the code could just search column AQ for any cells that have a "0" and copy the 3 pieces of data from that row in column A, column B, and Column G, and create a list on the "COMPLETE" sheet.

Thanks for the help!,
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It would be awesome if the code could just search column AQ for any cells that have a "0" and copy the 3 pieces of data from that row in column A, column B, and Column G, and create a list on the "COMPLETE" sheet.

Have you tried AutoFilter? Then you can use Goto-->Special-->Visible Cells only and copy/paste. It'll probably be easiest to copy the whole thing to a temp sheet, then delete the columns you don't need, then move what's left to your Complete sheet.

As for cleaning up your code, as a general rule of thumb, whenever you see Select followed by Selection you can eliminate both statements.

E.G.

Code:
    Sheets("Sheet1").Select
    Range("A1").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste

Can simply be: Sheets("Sheet1").Range("A1").Copy Sheets("Sheet2").Range("A1")

And anything having to do with navigation (ActiveWindow.SmallScroll Down:=-21) can be eliminated altogether.

You also only need Application.CutCopyMode = False once, at the end of your code.

HTH,
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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