VBA copy Row range loop

ojhawkins

New Member
Joined
Nov 17, 2011
Messages
39
Hi all,

Basically what I am trying to do is.

What ever account in entered into "B1" find in column A where this appears and copy the Row from (A:G) and paste to "I1" next "I2"

Any help is greatly appreciated.


10eelg1.jpg
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi, Try this:

Code:
Sub CopyAcctInfo()
 
Dim FinalRow As Long
Dim DestRow As Long
Dim MyAccount As Long
Dim i As Long
 
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
DestRow = Cells(Rows.Count, 9).End(xlUp).Row
MyAccount = Range("B1").Value
 
For i = 3 To FinalRow
 
    If Cells(i, 1).Value = MyAccount Then
 
        Range("A" & i, "G" & i).Copy Destination:=Range("I" & DestRow)
        DestRow = DestRow + 1
 
    End If
 
Next i
 
End Sub
 
Upvote 0
This version is a little different...this code would run the same as above except, it would perform on the ActiveSheet....so if you had multiple sheets in the same workbook...this is better.

Code:
Sub CopyAcctInfo()
 
Dim FinalRow As Long
Dim DestRow As Long
Dim MyAccount As Long
Dim i As Long
Dim Ws As Worksheet
 
Set Ws = ActiveSheet
FinalRow = Ws.Cells(Rows.Count, 1).End(xlUp).Row
DestRow = Ws.Cells(Rows.Count, 9).End(xlUp).Row
MyAccount = Ws.Range("B1").Value
 
   For i = 3 To FinalRow
       If Cells(i, 1).Value = MyAccount Then
           Ws.Range("A" & i, "G" & i).Copy Destination:=Ws.Range("I" & DestRow)
           DestRow = DestRow + 1
 
        End If
 
    Next i
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,125
Messages
6,189,120
Members
453,524
Latest member
AshJames

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