2
\$\begingroup\$

I have an autofiltered table in Excel. I have to copy/paste values based on certain conditions and I also have to perform this on all visible cells in a particular column. I have written the code and it works well but the only thing is that it takes a lot of time as there are many rows. Can anyone please help me how to thrash time required? Here's the code. Thanks!

Sub FilterAndCopy()
Windows("Epson Itemcodes.xlsm").Activate
 Range("A" & i).Select
 Selection.Copy
Windows("Epson ASINs.xlsx").Activate
 Range("U1048576").End(xlUp).Offset(0, -12).Select
If ActiveCell.Value <> "Itemcode" Then
If ActiveCell.Value = "" Then
 ActiveSheet.Paste
 Else
 If ActiveCell.Value = Workbooks("Epson Itemcodes.xlsm").Sheets("Sheet1").Range("A" & i).Value Then
 ActiveSheet.Paste
 Else
 ActiveCell.Value = "Conflct"
 End If
 End If
Else
Windows("Epson Itemcodes.xlsm").Activate
Range("I" & i).Value = "No match found"
End If
If ActiveCell.Value <> "Itemcode" Then
With ActiveSheet
Do
ActiveCell.Offset(-1, 0).Activate
Do While ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(-1, 0).Activate
Loop
If ActiveCell.Value <> "Itemcode" Then
If ActiveCell.Value = "" Then
 ActiveSheet.Paste
 Else
 If ActiveCell.Value = Workbooks("Epson Itemcodes.xlsm").Sheets("Sheet1").Range("A" & i).Value Then
 ActiveSheet.Paste
 Else
 ActiveCell.Value = "Conflct"
 End If
 End If
Else
Exit Do
End If
Loop
End With
End If
End Sub
RubberDuck
31.1k6 gold badges73 silver badges176 bronze badges
asked Dec 11, 2016 at 15:23
\$\endgroup\$
2
  • 2
    \$\begingroup\$ My guess is that your code is not working. (In addition to not declaring variables, the value of i is never set and the first statement would fail.) This site is for reviewing working code. You can find some extremely helpful tips on using Option Explicit, avoiding SELECT, with other very useful tips. \$\endgroup\$ Commented Dec 12, 2016 at 13:41
  • \$\begingroup\$ Using a consistent indent for nested loops and Ifs will help a bunch also... \$\endgroup\$ Commented Dec 12, 2016 at 20:48

1 Answer 1

3
\$\begingroup\$

Before you write another line of code, watch this Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).

Everyone here wants to help you but, and I know that you read this, you have got to stop selecting and activating cells. You also need to describe your data. Posting screenshots wouldn't hurt either. So if I sound mean but you wrote 72 lines of code to copy maybe 12 values.

Range.Copy, Range.Cut, Range.Insert and by default act on only the visible cells.

Sub FilterAndCopy()
 Dim Source As Range, Target As Range
 With Workbooks("Epson ASINs.xlsx").Worksheets("Sheet1")
 Set Source = .Range("U2", .Range("U" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
 End With
 With Workbooks("Epson Itemcodes.xlsm").Worksheets("Sheet1")
 Set Target = .Range("U" & .Rows.Count).End(xlUp).Offset(1)
 End With
 Source.Offset(1).Copy Desination:=Target
End Sub
Raystafarian
7,2991 gold badge23 silver badges60 bronze badges
answered Dec 13, 2016 at 3:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.