Copy certain rows and columns using VBA

sjfir

New Member
Joined
Aug 24, 2009
Messages
47
Hi,
I have the following code in a sheet that currently copies all rows with a 1 in column F to another worksheet.

Dim LR As Long, i As Long
With Sheets("Jan")
LR = .Range("F" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("F" & i).Value = 1 Then .Rows(i).copy Destination:=Sheets("Matrix input sheet").Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End With

However what I'd really like the code to do is only copy the data from columns A-E in the Jan sheet and nothing else and that once it copies it selects the next empty cell in column A in the 2nd sheet

Any help much appreciated, thanks.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
At the moment you're copying row by row, I'd probably go down the route of applying a filter, then copying the visible cells to the new sheet. It would probably run a touch faster with that method.
Something like this;
Range("A1:E" & ActiveSheet.UsedRange.Row).select
Selection.AutoFilter Field:=6, Criteria1:=1, Operator:=xlAnd
Range("A1:E" & ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible).copy

Note, that isn't tested at all!
 
Upvote 0
Thanks for the reply, that didn't work, I keep getting a run time error as it is only the top two rows that it seems to be trying to copy. Any ideas?
 
Upvote 0
MAybe this...
Also, when posting code can you please use code tags. It's easier to read and debug
Code:
Sub MM1()
Dim LR As Long, i As Long
With Sheets("Jan")
LR = .Range("F" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("F" & i).Value = 1 Then .Range("A" & i & ":E" & i).Copy Destination:=Sheets("Matrix input sheet").Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,273
Members
449,219
Latest member
daynle

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