Copy certain columns code dependant on outcome of Loop

Amateurhour101

New Member
Joined
May 8, 2020
Messages
3
Office Version
  1. 2011
Platform
  1. MacOS
Hi all, I am trying to copy Columns A to D where the Column 5 ("E") is Inventory, to another tab -> aptly called Inventory. Excel keeps having trouble with the yellow line in the script below. I would appreciate any help to this issue.

Many thanks
Dave

Sub Inventory_Loop()

Dim irow As Long



A = Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To A

If Worksheets("DATA").Cells(i, 5).Value = "INVENTORY" Then

Worksheets("DATA").Range(Cells(irow, "A"), Cells(irow, "D")).Copy

Worksheets("Inventory").Activate

Worksheets("Inventory").Cells(1,3).Select

ActiveSheet.Paste

Worksheets("DATA").Activate

End If

Next

Application.CutCopyMode = False

ThisWorkbook.Worksheets("DATA").Cells(1, 1).Select

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the Board!

It looks like you are using "irow" in that formula, but you haven't set "irow" to anything yet. So it is trying to access row 0, which is not possible.
Did you, perhaps, mean to use "i" instead of "irow"?
 
Upvote 0
Hi & welcome to MrExcel.
Try it like
VBA Code:
With Worksheets("DATA")
     .Range(.Cells(i, "A"), .Cells(i, "D")).Copy
End With
 
Upvote 0
Hi thanks for your comments I am still encountering an issue with the same line. Apologies for not putting the text as easy to read. It seems whatever i try to put in that line after the if, excel doesn't like or want to accept? Many thanks in advance.

VBA Code:
Sub Inventory_Loop()

A = Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To A

    If Worksheets("DATA").Cells(i, 5).Value = "INVENTORY" Then
    
With Worksheets("DATA").Range(Cells(i, "A"), Cells(i, "D")).Copy

End With

        Worksheets("Inventory").Activate

        Worksheets("Inventory").Cells(1, 3).Select

        ActiveSheet.Paste

        Worksheets("DATA").Activate

    End If
 
Upvote 0
You need to write that line like I showed in post#3 otherwise it will only work if the Data sheet is the activesheet.
 
Upvote 0
It looks like you combined a few rows together, combining your WITH statement with you COPY command.
Get rid of the "WITH" clause (you aren't doing anything with it by changing this:
VBA Code:
With Worksheets("DATA").Range(Cells(i, "A"), Cells(i, "D")).Copy

End With
to this:
VBA Code:
Worksheets("DATA").Range(Cells(i, "A"), Cells(i, "D")).Copy
 
Upvote 0
@Joe4 that will only work if the data sheet is active, as the cells portion has not been qualified with the sheet name.
 
Upvote 0
that will only work if the data sheet is active, as the cells portion has not been qualified with the sheet name.
Good point. I was just pointing out the error in that line of code.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

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