Results 1 to 4 of 4

Searching for words in excel

Threaded View

  1. #1
    Registered User
    Join Date
    06-10-2009
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    9

    Searching for words in excel

    Hi,

    I am quite new to VBA. I have stolen a little piece of code from another website and the code works very well with a little alteration. The code searches for a value inside a given cell. If it finds a match, it will then copy and paste the whole row into another sheet. Here is the code

    Sub SearchForString()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
    
        On Error GoTo Err_Execute
    
        'Start search in row 1
        LSearchRow = 1
    
        'Start copying data to row 3 in Sheet2 (row counter variable)
        LCopyToRow = 2
    
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
            'If value in column B = "Voids", copy entire row to Voids
            If Range("B" & CStr(LSearchRow)).Value = "Voids" Then
    
                'Select row in All alarms to copy
                Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
                Selection.Copy
    
                'Paste row into Sheet2 in next row
                Sheets("Voids").Select
                Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
                ActiveSheet.Paste
    
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
    
                'Go back to Sheet1 to continue searching
                Sheets("All Alarms").Select
    
            End If
    
            LSearchRow = LSearchRow + 1
    
        Wend
    
        'Position on cell A3
        Application.CutCopyMode = False
        Range("A1").Select
    
        MsgBox "All matching data has been copied."
    
        Exit Sub
        
    Err_Execute:
        MsgBox "An error occurred you tit, sort it out."
    
    End Sub
    What I wanted to do is alter the code marked in red to, rather then = the value "voids". I wanted to search to see if the cell contained the same word. If it does then do exactly the same as I am doing at the moment.

    Thank you in advance for your help

    Ashley
    Last edited by solid_state; 06-10-2009 at 09:03 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1