+ Reply to Thread
Results 1 to 11 of 11

Find word and if number appears below word then copy

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Find word and if number appears below word then copy

    I want a macro to search in column A only for the exact word that appears in cell D1. If it finds this word AND if a whole number appears in the cell directly below it then I want to copy one cell to the right and 15 cells down and paste it (transpose it) into sheet2 cell Z1.

    Example would be if it finds the word in cell A14 but there is no number in cell A15 then it keeps going and it finds the word again in cell A25 and a whole number is found in cell A26 then copy cells B26:B40 and paste it into sheet2 Z1 (transposed).

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find word and if number appears below word then copy

    Might need to change the sheet names in the code on the first two lines, you never really specified what the first sheet is named.

    Sub ScabbyDog()
    Set ws1 = Sheets("Sheet1") ' The searched sheet
    Set ws2 = Sheets("Sheet2") ' The pasted to sheet
    SearchFor = ws1.Range("D1").Value
    With ws1.Range("A:A")
        Set c = .Find(SearchFor, lookat:=xlWhole, MatchCase:=False)
        If Not c Is Nothing Then
            FirstAdd = c.Address
            Do
            If Val(c.Offset(1, 0).Value) = Int(Val(c.Offset(1, 0).Value)) Then
                ws1.Range(ws1.Cells(c.Row + 1, 2), ws1.Cells(c.Row + 16, 2)).Copy
                ws2.Range("Z1").PasteSpecial Transpose:=True
                Application.CutCopyMode = False
                Exit Sub
            End If
            Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAdd
            MsgBox ("Value in D1 was found, but with no whole number one cell below")
            Exit Sub
        Else
            MsgBox ("Value in D1 was not found")
        End If
    End With
    End Sub
    Last edited by walruseggman; 01-11-2016 at 10:52 AM.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Find word and if number appears below word then copy

    Another:

    Sub ScabbyDogy()
    Dim rng As Range
    Set rng = Columns(1).Find(Cells(1, "D"), LookIn:=xlValues, lookat:=xlWhole)
        If Not rng Is Nothing Then
            If IsNumeric(rng.Offset(1)) Then
                rng.Offset(, 1).Resize(15).Copy
                    Sheets("Sheet2").Cells(1, "Z").PasteSpecial Transpose:=True
            End If
        End If
    Set rng = Nothing
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Find word and if number appears below word then copy

    @Walruss That works BUT it doesn't take into account that there has to be a number found directly below the cell where D1 is found in column A before doing the copying and pasting. Once that has been added then it's perfect. Any idea how to add that clause?

    @John - That works but if the first time it finds the word and it has no number below it then it stops. It needs to keep searching until it finds a number under the word, then stop looping.
    Last edited by ScabbyDog; 01-11-2016 at 11:05 AM.

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find word and if number appears below word then copy

    Try the code I posted above

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Find word and if number appears below word then copy

    Not sure which post you are referring to:

    Sub ScabbyDogy()
    Dim rng As Range
    Set rng = Columns(1).Find(Cells(1, "D"), LookIn:=xlValues, lookat:=xlWhole)
        If Not rng Is Nothing Then
            If IsNumeric(rng.Offset(1)) Then
                rng.Offset(, 1).Resize(15).Copy
                    Sheets("Sheet2").Cells(1, "Z").PasteSpecial Transpose:=True
            End If
        End If
    Set rng = Nothing
    End Sub

  7. #7
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find word and if number appears below word then copy

    That works BUT it doesn't take into account that there has to be a number found directly below the cell where D1 is found in column A before doing the copying and pasting.
    It does evaluate that at this line, which works on my test sheet:

    If Val(c.Offset(1, 0).Value) = Int(Val(c.Offset(1, 0).Value)) Then
    If it's not working for you, can you post a sample data set where the evaluation fails?

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Find word and if number appears below word then copy

    Maybe:

    Sub ScabbyDogy()
    Dim rng As Range
    For Each rng In Range("A2:A" & Range("A" & Rows.Count).End(3).row)
        If rng = Cells(1, "D") Then
            If IsNumeric(rng.Offset(1)) Then
                rng.Offset(, 1).Resize(15).Copy
                Sheets("Sheet2").Cells(1, "Z").PasteSpecial Transpose:=True
                Exit For
            End If
        End If
    Next rng
    End Sub

  9. #9
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Find word and if number appears below word then copy

    Brilliant guys, working perfectly based off your most recent code.

    How could it be altered if I wanted to find the 2nd instance of the word in column A which also appears with a number directly below it?

  10. #10
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find word and if number appears below word then copy

    Sub ScabbyDog()
    Set ws1 = Sheets("Sheet1") ' The searched sheet
    Set ws2 = Sheets("Sheet2") ' The pasted to sheet
    SearchFor = ws1.Range("D1").Value
    With ws1.Range("A:A")
        Set c = .Find(SearchFor, lookat:=xlWhole, MatchCase:=False)
        If Not c Is Nothing Then
            FirstAdd = c.Address
            Do
            If Val(c.Offset(1, 0).Value) = Int(Val(c.Offset(1, 0).Value)) Then
                ws1.Range(ws1.Cells(c.Row + 1, 2), ws1.Cells(c.Row + 16, 2)).Copy
                ws2.Range("Z1").PasteSpecial Transpose:=True
                Application.CutCopyMode = False
            End If
            Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAdd
            Exit Sub
        End If
    MsgBox ("Value in D1 was not found, or no whole number was found underneath it.")
    End With
    
    End Sub

  11. #11
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Find word and if number appears below word then copy

    Sorted! Thanks all.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] count number of times a word appears
    By m_789 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-19-2013, 10:16 AM
  2. Counting number of times a word appears across worksheets
    By danltd in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-24-2012, 04:33 AM
  3. Replies: 19
    Last Post: 05-26-2011, 04:15 PM
  4. Replies: 3
    Last Post: 10-02-2007, 09:53 AM
  5. [SOLVED] count the number of times a specific word appears in a column
    By BAR in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-27-2006, 12:10 PM
  6. Counting the number of times a word appears 'anywhere' on a page
    By Brother Laz in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-17-2006, 10:10 AM
  7. [SOLVED] [SOLVED] Counting the number of times a word appears in a worksheet
    By Jig Bhakta in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-02-2005, 11:06 PM

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