Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88050

How can I make this sub loop through a range of cells properly

$
0
0

I have values in this "Sample Analysis Data" sheet in the range B2:B10. For each cell in the range, the code need to looks for that value in the sheet "Meta Data". It then copies the cells in that row and pastes it in "Sample Analysis Data" (to the right of the searched value). This all works fine for the value in B2. However, I can't get it to move on to B3...and then B4 and such. It just loops though and does the same thing again for B2.

  • What do I need to do to get it to loop to from B2 through to B10?

  • Along with this, how do I get it to go from B2 to the last entry in the column (as each data set I work with could have any different number of rows of data, not just to B10?

Sub GetMetaData()


    Worksheets("Sample Analysis Data").Activate
    Range("B2").Select


    Dim srch As Range, cell As Variant
    Set srch = Range("B2:B10")


    For Each cell In srch
    Sheets("Meta Data").Activate

    Cells.Find(What:=cell, LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate

    ActiveSheet.Cells(ActiveCell.Row, 1).Select
    Range(ActiveCell, ActiveCell.End(xlToRight).End(xlToRight)).Select

    Application.CutCopyMode = False
    Selection.Copy

    Sheets("Sample Analysis Data").Activate

    ActiveCell.Offset(0, 7).Select
    ActiveSheet.Paste

    Next cell
End Sub

Viewing all articles
Browse latest Browse all 88050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>