i've never touched visual basic before, but i managed to take the source from
FindItAll (though i don't think it was the actual source seeing as how it
didn't work) and edit it to fit my needs.
looking to make a VB macro in Excel that will find a cell and move
entire row that cell is in to the top. doing this for work, and my boss knows
i'm not a programmer. i think he wants me to learn. if anyone could point out
bugs or tell me commands, it would be much appreciated.
here's the code:
Dim FirstCell As Range
Dim NextCell As Range
Dim WhatToFind As Variant
'Window prompt allowing user to define WhatToFind
WhatToFind = Application.InputBox("What are you looking for?", "Search", ,
100, 100, , , 2)
'If WhatToFind is a value and not blank, move on
If WhatToFind <> "" And Not WhatToFind = False Then
'Start with first worksheet
'Start at first cell
'Find the first cell containing WhatToFind (specified by user)
Set FirstCell = Cells.Find(What:=WhatToFind, LookIn:=xlValues,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
'If FirstCell exists, move on
If Not FirstCell Is Nothing Then
'Ok, First Cell is set
On Error Resume Next
'NextCell is currently undefined. Don't mistake NextCell for FirstCell
While (Not NextCell Is Nothing) And (Not NextCell.Address = FirstCell.Address)
'Find next row containing what is in FirstCell (ActiveCell) and define as
Set NextCell = Cells.FindNext(After:=ActiveCell)
'Don't mistake NextCell for FirstCell, move on
If Not NextCell.Address = FirstCell.Address Then
'Activate subsequent NextCells
'Select all rows containing WhatToFind - NOT WORKING ARRGGGHHHH!!! only
selecting the cell, not the row.
'Also, if >1 instance of WhatToFind, acts funky...
'Cut all rows containing WhatToFind
'Back to A1
'Insert cut rows here
Set NextCell = Nothing
Set FirstCell = Nothing
major problem is how to select ALL of the ROWS that WhatToFind is found in.
also, if WhatToFind is found in two cells in the same row, it will move the
second cell containing it in that row to the next unused row. idk. it acts
i think Worksheets("Sheet1").Rows(ActiveCell).Select is the major problem
i think Cells(1, 1).Select might be causing the odd behavior for two cells
in the same row.