VBA Loop and copy cells to sheet

SGMike

New Member
Joined
Jun 10, 2011
Messages
3
Hi, in excel 2007 trying to loop through (un-fixed length) column (say C) and where row value matches (say "High"), then transfer value of cells Bx and Dx to sheet "transfer", where x is the row# where the matches are found. Assume "transfer" exists.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try

Code:
Sub test()
Dim LR As Long, i As Long
With ActiveSheet
    LR = .Range("C" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        If .Range("C" & i).Value = "High" Then
            .Range("B" & i).Copy Destination:=Sheets("Transfer").Range("B" & i)
            .Range("D" & i).Copy Destination:=Sheets("Transfer").Range("D" & i)
        End If
    Next i
End With
End Sub
 
Upvote 0
Thank you VoG; it's almost there. The code producs the desired copies of desired cells in the right sheet; 2 issues: 1) ideally the copied cells from each row would be on sequential rows rather than as now in same rows as the ones they were copied from; 2) the copying assumes that the cells contain values rather than formulas - what if the cell being copied contains formula?
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
With ActiveSheet
    LR = .Range("C" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        If .Range("C" & i).Value = "High" Then
            .Range("B" & i).Copy
            Sheets("Transfer").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            .Range("D" & i).Copy
            Sheets("Transfer").Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        End If
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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