LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default creating macro VB in Excel - find and move program

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 the
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:

Sub FindAndMoveToTop()
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
Worksheets("Sheet1").Activate
'Start at first cell
Range("A1").Select
'Find the first cell containing WhatToFind (specified by user)
Set FirstCell = Cells.Find(What:=WhatToFind, LookIn:=xlValues,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False)
'If FirstCell exists, move on
If Not FirstCell Is Nothing Then
'Ok, First Cell is set
FirstCell.Activate
'Keep going
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
NextCell
Set NextCell = Cells.FindNext(After:=ActiveCell)
'Don't mistake NextCell for FirstCell, move on
If Not NextCell.Address = FirstCell.Address Then
'Activate subsequent NextCells
NextCell.Activate
End If
Wend
End If
'Select all rows containing WhatToFind - NOT WORKING ARRGGGHHHH!!! only
selecting the cell, not the row.
'Also, if 1 instance of WhatToFind, acts funky...
Worksheets("Sheet1").Rows(ActiveCell).Select
'Cut all rows containing WhatToFind
Selection.Cut
'Back to A1
Cells(1, 1).Select
'Insert cut rows here
Selection.Insert Shift:=xlDown
'Clean up
Set NextCell = Nothing
Set FirstCell = Nothing
Range("A1").Select
End If
End Sub


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
really funky.

i think Worksheets("Sheet1").Rows(ActiveCell).Select is the major problem
spot.
i think Cells(1, 1).Select might be causing the odd behavior for two cells
in the same row.

any ideas?
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
excel macro needed - find and move data The Kid Excel Discussion (Misc queries) 1 December 9th 09 04:14 AM
move excel program from old computer to new gonsailin Excel Discussion (Misc queries) 1 March 15th 06 02:27 PM
Macro - Find a value and then move down Phil Osman Excel Discussion (Misc queries) 4 August 10th 05 01:20 PM
VBA Creating a Macro to Move some of the text from one cell to another Lowell B. Copeland Excel Programming 4 August 28th 03 08:15 PM
VBA Creating a Macro to Move some of the text from one cell to another Ron de Bruin Excel Programming 0 August 28th 03 07:21 PM


All times are GMT +1. The time now is 08:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"