There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
creating macro VB in Excel - find and move program


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
DictatorDrac's Avatar
Computer Specs
Member with 75 posts.
 
Join Date: Jun 2005
Location: Georgia
Experience: Intermediate
01-Jun-2006, 08:32 AM #1
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:

Code:
Sub FindAndMoveToTop()
    Dim FoundCell 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
            'Loop as many times as there are used rows
            For x = 1 To ActiveSheet.UsedRange.Rows.Count
                'Find the first cell containing WhatToFind (specified by user)
                Set FoundCell = Cells.Find(What:=WhatToFind, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                'If FirstCell exists, move on
                If Not FoundCell Is Nothing Then
                    'Ok, First Cell is set
                    FoundCell.Activate
                    'Select all rows containing WhatToFind - NOT WORKING ARRGGGHHHH!!! only selecting the cell, not the row.
                    'Also, if >1 instance of WhatToFind, acts funky...
                    'Fixed the following line thanks to user Brassman on Microsoft Excel Programmer forums
                    Worksheets("Sheet1").Rows(ActiveCell.Row).EntireRow.Select
                    'Cut all rows containing WhatToFind
                    Selection.Cut
                    'Back to A1
                    Cells(1, 1).Select
                    'Insert cut rows here
                    Selection.Insert Shift:=xlDown
                    'Keep going
                    On Error Resume Next
                End If
            Next x
        'Clean up
        Set FoundCell = Nothing
        Range("A1").Select
    End If
End Sub
only problem is that it only moves the first two results...

any ideas?
__________________
Draco - The MaN, the MyTh, the LeGeNd.

Last edited by DictatorDrac : 01-Jun-2006 01:07 PM.
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 6,371 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
01-Jun-2006, 01:29 PM #2
One reason it is not working is that it doesn't "find" what you ask it to find. It stops at the first row (a2) and then inserts the row at "a1"
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 6,371 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
01-Jun-2006, 02:00 PM #3
I can do this for you, but not using the "Find" function.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:11 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.