+ Reply to Thread
Results 1 to 15 of 15

.Find function

Hybrid View

rgiglio7489 .Find function 04-08-2016, 05:23 PM
mehmetcik Re: .Find function 04-08-2016, 06:01 PM
rgiglio7489 Re: .Find function 04-08-2016, 06:04 PM
alansidman Re: .Find function 04-08-2016, 06:01 PM
rgiglio7489 Re: .Find function 04-08-2016, 06:05 PM
mongoose36 Re: .Find function 04-08-2016, 06:12 PM
rgiglio7489 Re: .Find function 04-12-2016, 10:29 AM
mehmetcik Re: .Find function 04-08-2016, 06:31 PM
mongoose36 Re: .Find function 04-12-2016, 11:18 AM
rgiglio7489 Re: .Find function 04-12-2016, 11:44 AM
jolivanes Re: .Find function 04-12-2016, 12:39 PM
mongoose36 Re: .Find function 04-13-2016, 09:42 AM
rgiglio7489 Re: .Find function 04-13-2016, 10:35 AM
mongoose36 Re: .Find function 04-13-2016, 11:19 PM
rgiglio7489 Re: .Find function 04-14-2016, 09:57 AM
  1. #1
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    .Find function

    Hi- I am struggling a bit with the find function. I currently have a summary sheet called "Comments" and I have a data sheet called "Data". In my comments sheet, I am showing the following 7 columns (I have an empty column in between each):
    1. Company name (column B)
    2. Type of business (column D)
    3. Date (column F)
    4. Type of business code (column H)
    5. Clientel (column J)
    6. Revenue (column L)
    7. Comments (column N)

    These 7 columns are being populated from the Data tab manually. the Data tab has about 25 columns of data, and I want my VBA to:
    1. look in column 21, which is a column full of formulas, and find every cell with a "yes"
    2. in that same row, grab the information needed above, and place it into the Comments tab.

    The VBA below gets me halfway there... If there are 15 cells in column 21 with the value of "yes", then there should be 15 rows in the Comments tab. I'm having a hard time finding the "yes" in column 2, then placing it in Comments tab, then going to the next "yes" value in column 21 and placing in the next row of the Comments tab. Very hard to follow- I'm sorry. Here is what I have so far:

    Worksheets("Comments").Range("B4:N1000").ClearContents
     
     
    Dim lCount As Long
    
    Dim TheMark As Range
    
    
    
        Set TheMark = Worksheets("Data").Range("U1")
    
    
            Worksheets("Data").Activate
            For lCount = 1 To WorksheetFunction.CountIf(Columns(21), "yes")
    
                Set TheMark = Columns(21).Find(what:="yes", after:=TheMark, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
                
    
                 With TheMark
                    .Offset(0, -18).Select
                     Set LegalEntity = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("B4").Value = LegalEntity
                        
                    .Offset(0, -5).Select
                     Set LineOfBusiness = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("D4").Value = LineOfBusiness
             
                    .Offset(0, -19).Select
                     Set TradeDate = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("F4").Value = TradeDate
                        
                    .Offset(0, -4).Select
                     Set ErrorType = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("H4").Value = ErrorType
                        
                    .Offset(0, -14).Select
                     Set Counterparty = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("J4").Value = Counterparty
                        
                    .Offset(0, -17).Select
                     Set PNL = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("L4").Value = PNL
                        
                    .Offset(0, -11).Select
                     Set Comments = ActiveCell
                        Worksheets("Comments CMC & NYB").Range("N4").Value = Comments
                    
                    
                 End With
    
                
    
            Next lCount
    Last edited by alansidman; 04-08-2016 at 06:02 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: .Find function

    If you are just copying "yes" to the comments column why can you not use a formula in the comments column?
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    I'm looking for anything that says "yes" in column 21, and I need to return values in other columns based on multiple criteria. Trust me, I would much rather use a formula (like a vlookup or an INDEX), but i think VBA would work smoother.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: .Find function

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html

    Please read the forum rules and abide by them in the future



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    sorry. i will do that going forward

  6. #6
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: .Find function

    Perhaps...
    Sub test()
    Worksheets("Comments").Range("B4:N1000").ClearContents
    'Declareing worksheet variables allows you to refer to specific sheets without activating them
    Dim wsData As Worksheet
    Dim wsComment As Worksheet
    Dim lCount As Long
    Dim CommRow As Long
    Dim TheMark As Range
    
    Set wsData = ThisWorkbook.Worksheets("Data")  'Instantiate worksheet variable declared above
    Set wsComment = ThisWorkbook.Worksheets("Comments CMC & NYB")
    
    Set TheMark = wsData.Range("U1")
    
    CommRow = 4 'Start at row 4 ... change as needed
    
    For lCount = 1 To WorksheetFunction.CountIf(wsData.Columns(21), "yes")
    
        Set TheMark = wsData.Columns(21).Find(what:="yes", after:=TheMark, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
        With TheMark
            'you don't need to select the cell to get or transfer its value
            Worksheets("Comments CMC & NYB").Range("B" & CommRow).Value = .Offset(0, -18).Value
            
            wsComment.Range("D" & CommRow).Value = .Offset(0, -5).Value
        
            wsComment.Range("F" & CommRow).Value = .Offset(0, -19).Value
        
            wsComment.Range("H" & CommRow).Value = .Offset(0, -4).Value
            
            wsComment.Range("J" & CommRow).Value = .Offset(0, -14).Value
        
            wsComment.Range("L" & CommRow).Value = .Offset(0, -17).Value
        
            wsComment.Range("N" & CommRow).Value = .Offset(0, -11).Value
        
        End With
    
        CommRow = CommRow + 1  'Itterate to the next row after values are inserted
    
    Next lCount
    
    End Sub
    If your questions has been answered to your satisfaction please don't forget to do the following:

    Add Reputation ... and ... Mark Thread as Solved

    Thanks,

    Ma 10:8b Freely you have received; freely give.

  7. #7
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    This hit the nail on the head when it comes to what I'm trying to do, thank you... on small problem... lets say there are 3 "yes" values in column 21:

    row 20, row 40 and row 60 have a "yes" value in column 21... the vba is counting the # of "yes" values correctly, and its returning 3 comments correctly to the comments sheet... however its not taking the values from the offset in rows 20/40/60... rather its taking the values from the offset in rows 1/2/3... i'm not sure what the issue is, because everything below seems to be correct below.

    Does this make sense?

    Quote Originally Posted by mongoose36 View Post
    Perhaps...
    Sub test()
    Worksheets("Comments").Range("B4:N1000").ClearContents
    'Declareing worksheet variables allows you to refer to specific sheets without activating them
    Dim wsData As Worksheet
    Dim wsComment As Worksheet
    Dim lCount As Long
    Dim CommRow As Long
    Dim TheMark As Range
    
    Set wsData = ThisWorkbook.Worksheets("Data")  'Instantiate worksheet variable declared above
    Set wsComment = ThisWorkbook.Worksheets("Comments CMC & NYB")
    
    Set TheMark = wsData.Range("U1")
    
    CommRow = 4 'Start at row 4 ... change as needed
    
    For lCount = 1 To WorksheetFunction.CountIf(wsData.Columns(21), "yes")
    
        Set TheMark = wsData.Columns(21).Find(what:="yes", after:=TheMark, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
        With TheMark
            'you don't need to select the cell to get or transfer its value
            Worksheets("Comments CMC & NYB").Range("B" & CommRow).Value = .Offset(0, -18).Value
            
            wsComment.Range("D" & CommRow).Value = .Offset(0, -5).Value
        
            wsComment.Range("F" & CommRow).Value = .Offset(0, -19).Value
        
            wsComment.Range("H" & CommRow).Value = .Offset(0, -4).Value
            
            wsComment.Range("J" & CommRow).Value = .Offset(0, -14).Value
        
            wsComment.Range("L" & CommRow).Value = .Offset(0, -17).Value
        
            wsComment.Range("N" & CommRow).Value = .Offset(0, -11).Value
        
        End With
    
        CommRow = CommRow + 1  'Itterate to the next row after values are inserted
    
    Next lCount
    
    End Sub

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: .Find function

    Ok

    This is the fast way to do what you want.

    This finds Yes in Column 3 and puts a Yes in Column 5.

    
    Sub macro()
        With Columns(3)
            Set rngfind = .Find("Yes", .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngfind Is Nothing Then
                strFirstAddress = rngfind.Address
                Set rngpicked = rngfind
                Do
                    rngpicked.Offset(0, 2).Value = "Yes"
                    Set rngfind = .FindNext(rngfind)
                Loop While Not rngfind Is Nothing And rngfind.Address <> strFirstAddress
            End If
        End With
        
    End Sub

  9. #9
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: .Find function

    I'm not sure what would be causing this behavior. It works fine in my test (see attached).

    http://www.excelforum.com/attachment...1&d=1460474277

  10. #10
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    very interesting... I just tried yours and it works in your spreadsheet. the only difference between mine and yours is that all of my data is in a table, and some of the values are formulas (but thats covered with LookIn:=xlFormulas). any ideas? I'm still playing around with it... does excel version matter?


    Quote Originally Posted by mongoose36 View Post
    I'm not sure what would be causing this behavior. It works fine in my test (see attached).

    http://www.excelforum.com/attachment...1&d=1460474277

  11. #11
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: .Find function

    No need counting or looping, is there?
    Change all sheet/cell references as required.
    Your explanation lacks information for me so can't be more specific.
    Sub With_AutoFilter_Maybe()
    Dim lr As Long
    lr = Cells(Rows.Count, 2).End(xlUp).Row
    Application.ScreenUpdating = False
      With Sheets("Sheet1").UsedRange
        .AutoFilter 21, "yes"
        .Range("B2:N" & lr).SpecialCells(12).Copy Sheets("Sheet2").Range("B2")
        .AutoFilter
      End With
    Application.ScreenUpdating = True
    End Sub

  12. #12
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: .Find function

    Quote Originally Posted by rgiglio7489 View Post
    very interesting... I just tried yours and it works in your spreadsheet. the only difference between mine and yours is that all of my data is in a table, and some of the values are formulas (but thats covered with LookIn:=xlFormulas). any ideas? I'm still playing around with it... does excel version matter?
    The addition of a table to my test did not alter the result. There must be something else going on. Perhaps if you could upload the workbook you are working with.

    @jolivanes I agree there are better ways to achieve the desired result, however what you have suggested is not quite what @rgiglio7489 is trying to do. Below is your code with some alterations to do what @rgiglio7489 wants.

    Sub With_AutoFilter_Maybe()
    
    Dim wsData As Worksheet
    Dim wsComment As Worksheet
    Dim lr As Long
    
    'For some reason I could not get the ListObject to work with the object wsData...it would only work with the ActiveSheet object
    
    If ActiveSheet.Name = "Data" Then
        Set wsComment = ThisWorkbook.Worksheets("Comments CMC & NYB")
          Application.ScreenUpdating = False
          With ActiveSheet
            .ListObjects("Table1").Range.AutoFilter Field:=21, Criteria1:="yes"           'change "Table1" to whatever the name of your table is
            lr = .Cells(Rows.Count, 21).End(xlUp).Row
            If lr > 1 Then
                .Range(.Cells(2, 3), .Cells(lr, 3)).SpecialCells(12).Copy Destination:=wsComment.Range("B4")
                .Range(.Cells(2, 16), .Cells(lr, 16)).SpecialCells(12).Copy Destination:=wsComment.Range("D4")
                .Range(.Cells(2, 2), .Cells(lr, 2)).SpecialCells(12).Copy Destination:=wsComment.Range("F4")
                .Range(.Cells(2, 17), .Cells(lr, 17)).SpecialCells(12).Copy Destination:=wsComment.Range("H4")
                .Range(.Cells(2, 7), .Cells(lr, 7)).SpecialCells(12).Copy Destination:=wsComment.Range("J4")
                .Range(.Cells(2, 4), .Cells(lr, 4)).SpecialCells(12).Copy Destination:=wsComment.Range("L4")
                .Range(.Cells(2, 10), .Cells(lr, 10)).SpecialCells(12).Copy Destination:=wsComment.Range("N4")
            End If
            .AutoFilterMode = False
          End With
        Application.ScreenUpdating = True
    End If
    End Sub

  13. #13
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    See attached... Its taking the 3 "Yes" values, and applying it in column 20 for the first 3 rows... not sure whats going on. Might be a setting? I also had no issue with your spreadsheet when I created a table.
    Attached Files Attached Files

  14. #14
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: .Find function

    I think the code below will deliver the results you are looking for...

    Option Explicit
    Sub With_AutoFilter_Maybe()
    
    Dim wsData As Worksheet
    Dim wsComment As Worksheet
    Dim lr As Long
    
    'For some reason I could not get the ListObject to work with the object wsData...it would only work with the ActiveSheet object
    
    If ActiveSheet.Name = "Data" Then
        Set wsComment = ThisWorkbook.Worksheets("Comments Me & You")
          Application.ScreenUpdating = False
          With ActiveSheet
            .ListObjects("Table_List63680").Range.AutoFilter Field:=21, Criteria1:="yes"           'change "Table1" to whatever the name of your table is
            lr = .Cells(Rows.Count, 21).End(xlUp).Row
            If lr > 1 Then
                .Range(.Cells(2, 3), .Cells(lr, 3)).SpecialCells(12).Copy Destination:=wsComment.Range("B4")
                .Range(.Cells(2, 16), .Cells(lr, 16)).SpecialCells(12).Copy Destination:=wsComment.Range("D4")
                .Range(.Cells(2, 2), .Cells(lr, 2)).SpecialCells(12).Copy Destination:=wsComment.Range("F4")
                .Range(.Cells(2, 17), .Cells(lr, 17)).SpecialCells(12).Copy Destination:=wsComment.Range("H4")
                .Range(.Cells(2, 7), .Cells(lr, 7)).SpecialCells(12).Copy Destination:=wsComment.Range("J4")
                .Range(.Cells(2, 4), .Cells(lr, 4)).SpecialCells(12).Copy Destination:=wsComment.Range("L4")
                .Range(.Cells(2, 10), .Cells(lr, 10)).SpecialCells(12).Copy Destination:=wsComment.Range("N4")
            End If
            
            .ListObjects("Table_List63680").Range.AutoFilter
          End With
        Application.ScreenUpdating = True
    Else
        MsgBox "The 'Data' Sheet must be active to run this code!", vbCritical
    End If
    End Sub

  15. #15
    Registered User
    Join Date
    12-18-2014
    Location
    nyc
    MS-Off Ver
    2013
    Posts
    30

    Re: .Find function

    I haven't tested your new suggestion, but i found a workaround:

    Sub Comments()
    Worksheets("Comments Me & You").Range("B4:N1000").ClearContents
    Dim wsText As Worksheet
    Dim wsComment As Worksheet
    Dim wsData As Worksheet
    Dim lCount As Long
    Dim CommRow As Long
    Dim TheMark As Range
    
    Set wsData = ThisWorkbook.Worksheets("Data")
    Set wsText = ThisWorkbook.Worksheets("Text")
    Set wsComment = ThisWorkbook.Worksheets("Comments Me & You")
    
    wsData.ListObjects("Table_list63680").Range.Copy
    wsText.Range("A1").PasteSpecial xlPasteValues
    CutCopyMode = False
    
    Set TheMark = wsText.Range("V1")
    
    CommRow = 4 
    
    For lCount = 1 To WorksheetFunction.CountIf(wsText.Columns(22), "yes")
    
        Set TheMark = wsText.Columns(22).Find(what:="yes", after:=TheMark, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
    
    
        With TheMark
            Worksheets("Comments Me & You").Range("B" & CommRow).Value = .Offset(0, -19).Value
            
            wsComment.Range("D" & CommRow).Value = .Offset(0, -4).Value
        
            wsComment.Range("F" & CommRow).Value = .Offset(0, -20).Value
        
            wsComment.Range("H" & CommRow).Value = .Offset(0, -4).Value
            
            wsComment.Range("J" & CommRow).Value = .Offset(0, -15).Value
        
            wsComment.Range("L" & CommRow).Value = .Offset(0, -18).Value
        
            wsComment.Range("N" & CommRow).Value = .Offset(0, -12).Value
        
        End With
    
        CommRow = CommRow + 1 
    
    Next lCount
    
    End Sub

+ 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] Can I use FIND function to find specific string in a single cell?
    By bonpara in forum Excel General
    Replies: 2
    Last Post: 10-05-2015, 05:46 PM
  2. If function + Find Function to search for multiple values
    By HabsFan89 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-21-2015, 12:11 PM
  3. add in the parameters to the Find function to only find an exact match.
    By jakeembx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2014, 08:57 AM
  4. [SOLVED] Problem with Find function. Need to find Exact match
    By SMILE in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2012, 07:07 AM
  5. find function doesnt find imported info
    By dscott2479 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 07-12-2011, 07:11 PM
  6. Replies: 2
    Last Post: 03-30-2009, 04:04 AM
  7. Using find function to find entries in multiple cells
    By stanigator in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2007, 08:45 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