Copy/paste range in stead of row

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am using this code to search thru a sheet looking for a value in a column, and if found, copy the row to a different sheet. However, when the value is found it is a range, not only a single row. That means I need to replace the "Cells" in this code, Cells(x, 1).Resize(1, 33).Copy, with "Range". Or at least I thought so, but that is not working. What am I doing wrong? Second question, when the range has been copied I would like to find the second range if any. Do I need to add in the "FindNext" before the Elseif, or after? (I Can't get that to work either...)


Full VBA code below:

Public Sub FindCopy()
Sheets("Sheet1").Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To FinalRow
ThisValue = Cells(x, 4).Value
If ThisValue = "Agro" Then
Cells(x, 1).Resize(1, 33).Copy
Sheets("Sheet2").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("Sheet2").Select
ElseIf ThisValue = "Vegro" Then
Cells(x, 1).Resize(1, 33).Copy
Sheets("Sheet3").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("Sheet3").Select
End If
Next x
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Explain what the range is.
You said not a single row.

Here you show:
if ThisValue = "Agro" Then

so what range do you want to copy
 
Upvote 0
Hi,

Sorry, that was very unprecise from my side! What I wish to use is: CurrentRegion
 
Upvote 0
Hi,

Sorry, that was very unprecise from my side! What I wish to use is: CurrentRegion
Sorry I never use current region so:
This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.
 
Upvote 0
HI
I would just filter and copy to the other sheets.
Insert a few rows at the top of your sheet 1, so the header is now in row 4
Whatever your heading is for column 4, type that into cell E1 (in my example) and enter Agro into cell E2

VBA Code:
Sub FilterCopyToOtherSheet()
  
Sheets("Sheet1").Select
Sheets("Sheet1").Range("E2")= "Agro"
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 
    Sheets("Sheet1").Range("A4:AG7" & Finalrow).AdvancedFilter _
        Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("Sheet1").Range("E1:E2"), _
        CopyToRange:=Sheets("Sheet2").Range("A1"), _
        Unique:=False
Sheets("Sheet1").Select

Sheets("Sheet1").Range("E2")= "Vegro"
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 
    Sheets("Sheet1").Range("A4:AG7" & Finalrow).AdvancedFilter _
        Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("Sheet1").Range("E1:E2"), _
        CopyToRange:=Sheets("Sheet3").Range("A1"), _
        Unique:=False
 
End Sub
 
Upvote 0
Thanks, that is an approach I did not think of at all. Will check it out!
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

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