Loop through range and copy adjacent cell to anther sheet

  • I am a total amatuer when it comes to VBA and I cannot seem to get this to work. I have 2 columns on Sheet1. I want to loop through column A and find all cells with a value of 1. The I want to copy the cell to the right into a list on Sheet2 without blank spaces.



    Sheet 1 Sheet2


    A B F


    1 Apples Apples
    1 Oranges Oranges
    0 Peaches Bananas
    1 Bananas Grapes
    0 Pears
    1 Grapes



    Thank you,
    Bill

  • her you go

  • Another way is using excels built in features:



    [VBA]Sub Macro1()
    Dim lRowStart As Long


    Application.ScreenUpdating = False


    With Sheet1
    .Rows(lRowStart).AutoFilter Field:=1, Criteria1:="1"
    .Rows(lRowStart).Columns(2).Offset(1).Resize(.AutoFilter.Range.Rows.Count).Copy Sheet2.Range("A1")
    .UsedRange.AutoFilter
    End With


    Application.ScreenUpdating = True


    End Sub[/VBA]

  • Changing the x to the starting row number is not working. Regardless of what I change x to, the list always start on row 2. I thought the sample I gave would suffice but maybe not.


    The table in on a sheet that has other information on it as well as several other tables.
    The table I am referring to actually starts on in column A, row 22 and does have a header. Sorry for the confusion.
    Thanks

  • Ok so try:


    [VBA]
    Sub Macro1()
    Dim lRowStart As Long


    Application.ScreenUpdating = False


    lRowStart = 22


    With Sheet1
    .Rows(lRowStart).AutoFilter Field:=1, Criteria1:="1"
    .Rows(lRowStart).Columns(2).Offset(1).Resize(.AutoFilter.Range.Rows.Count).Copy Sheet2.Range("A1")
    .UsedRange.AutoFilter
    End With


    Application.ScreenUpdating = True


    End Sub [/VBA]

  • Using your workbook, I used the following code and it delivered the results as expected in Column F of the Sheet2


    Data Range
    [Table="class: grid"]

    [tr][td][/td][td]

    F

    [/td][/tr]


    [tr][td]

    1

    [/td][td]

    [COLOR="#000000"]

    [/COLOR]

    [/td][/tr]


    [tr][td]

    2

    [/td][td]

    [COLOR="#000000"]

    Peaches

    [/COLOR]

    [/td][/tr]


    [tr][td]

    3

    [/td][td]

    [COLOR="#000000"]

    Lemons

    [/COLOR]

    [/td][/tr]


    [tr][td]

    4

    [/td][td]

    [COLOR="#000000"]

    Pears

    [/COLOR]

    [/td][/tr]


    [tr][td]

    5

    [/td][td]

    [COLOR="#000000"]

    Bananas

    [/COLOR]

    [/td][/tr]


    [tr][td]

    6

    [/td][td]

    [COLOR="#000000"]

    Plums

    [/COLOR]

    [/td][/tr]


    [tr][td]

    7

    [/td][td]

    [COLOR="#000000"]

    Oranges

    [/COLOR]

    [/td][/tr]


    [tr][td]

    8

    [/td][td]

    [COLOR="#000000"]

    Tangerines

    [/COLOR]

    [/td][/tr]


    [tr][td]

    9

    [/td][td]

    [COLOR="#000000"]

    Other

    [/COLOR]

    [/td][/tr]


    [/table]


  • This code does work. My questions is, how do I modify the code to start the list on row 8 instead of row 2 in column F.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!