Discussion Locked

Loop in MS Excel Macro

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How can I construct a loop (in an Excel Macro) to pick each value in, say cells A1 to A120 in Sheet1, compare with the contents of cells D9 to D185 in Sheet2 and then highlight all matches in both sheets. I'd also like to have all non-matches from each sheet transferred to sheets 3 and 4 respectively.

1 reply

by csanosi

This is pretty simple. First off, record a macro that does the appropriate highlighting. Next, edit this macro and add your loop around the code. I typically use a for loop and use the following command to grab the row number of the last cell to know where to end my loop. Save your values into variables, then loop at the second sheet and search for the values.

lnglastrow = ActiveCell.SpecialCells(xlLastCell).row

This discussion is closed to new replies.