简体   繁体   中英

Loop through a column to find matches in other sheet and cut/paste matching row

I am new to VBA excel and need to do the following:

I have two sheets in the same workbook with similar information. Both worksheets have a Program ID that identifies a row of data. For each Program ID there may be different "layers". These "layers" would be represented by rows that have very similar information to each other (same Program ID ), the only things that change are the Layer # fields.

So for example, I could have the following:

PROGRAM ID     NAME     LAYER
1234           test       1
1234           test       2
1234           test       3

My issue is that the information is split into the two worksheets, one worksheet (called noLayers ) contains only the first layer of each Program ID , while the other worksheet (called "withLayers") contains the remaining layers (ie all but the first layer) of each Program ID .

What I would like to do is a search that loops through each Program ID in the noLayers worksheet and finds it in the withLayers worksheet, then it should cut or copy the row and paste it below the corresponding Program ID in the "noLayers" worksheet.

Keep in mind that there may be several instances of the same Program ID in the withLayers worksheet, since a single Program may have more than 2 layers.

Any help with this would be GREATLY appreciated. Thank you!

Hmm, I can't recall exactly how to do that in VBA, it's been a while. But I can give you the general structure and perhaps someone can extrapolate.

You need to create an array of cells (usually held in a Range object, if I recall). And assign it's value as the range of cells in PROGRAM_ID on noLayers. Then you need to set another Range to cover the PROGRAM_ID column in the withLayers sheet. For simplicity let's call them "layersRange" and "noLayersRange".

Then you need to set up a couple of for each loops to iterate through them. If you pair that with an if statement and an insert statement, you should be able to do it.

PSUEDOCODE:

Dim layersRange as Range
Dim noLayersRange as Range

set layersRange = <column A on layers page>
set noLayersRange = <column A on noLayers page>

foreach (noLayers in noLayersRange)
    foreah (hasLayers in LayerseRange)
          if(nolayers.value = haslayers.valye)
              nolayers.upone.insert(hasLayers.wholeRow) 'or something like that
          end if
    end loop
end loop

Sorry, I know it's terrible pseudocode... But the basic jist is that you're looping through all of the cells in the column on the first page, and on every single cell you're looping through all the cells on the second sheet and, if you find anything, you insert it right above your current cell on the first sheet. (Please note: if you insert it below, you will add to the loop your are iterating through, which cause an infinite loop of insertions as the process repeatedly puts a copy of what it finds below itself, moves to that cell, then does the same process, ad infinitum.)

Hope that this at least gets you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM