Page 1 of 2 12 LastLast
Results 1 to 40 of 44

Thread: Find function - PLEASE HELP!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Find function - PLEASE HELP!

    Hi

    I am trying to write code that will match cell a1 in workbook A with ANY matching cell in workbook B. If it finds a matching cell in workbook B, then the macro must copy a range of cells (offset from the cell in workbook B) to workbook A. The macro must paste the range in a range offset from cell a1.

    The process must repeat from a2 in workbook A. The process must stop when there are NO MORE CELLS TO MATCH AGAINST in workbook B.

    can anyone help?

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: Find function - PLEASE HELP!

    This example finds a string in a RichTextBox control based on a word entered in a TextBox control. After it finds the specified string, it displays a message box that shows the number of the line containing the specified word. To try this example, put a RichTextBox control, a CommandButton control and a TextBox control on a form. Load a file into the RichTextBox, and paste this code into the General Declarations section of the form. Then run the example, enter a word in the TextBox, and click the CommandButton.

    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim FoundPos As Integer
    3.    Dim FoundLine As Integer
    4.    ' Find the text specified in the TextBox control.
    5.    FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)
    6.  
    7.    ' Show message based on whether the text was found or not.
    8.  
    9.    If FoundPos <> -1 Then
    10.       ' Returns number of line containing found text.
    11.       FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
    12.       MsgBox "Word found on line " & CStr(FoundLine)
    13.    Else
    14.       MsgBox "Word not found."
    15.    End

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Hi malley, Welcome to the Forum

    You may be better of in the Office Development (VBA) Forum.
    In any case, there are plenty of examples posted on this site relating to your request. - Just do a search.

    (I will try to dig one up)

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    shakti5385, chalk and cheese my friend.

    That example has nothing do with his query - sorry

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    thanks for your responses. if there are existing threads which solve my problem, do i do a search using the keyword "find"?

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Have a look at post 6 of this tread: http://www.vbforums.com/showthread.p...ighlight=excel, it may give you some ideas (I'll post back if I can find sometning better)

    The following may be a place to start:
    VB Code:
    1. intRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count 'Capture the number of USED Rows
    2.     intColCount = objExcel.ActiveSheet.UsedRange.Columns.Count  'Capture the number of USED Columns
    3.  
    4.     For intRowIdx = 1 To intRowCount  'Iterate each Row
    5.         For intColIdx = 1 To intColCount  'Iterate each Column
    6.             If whatever = objExcel.ActiveSheet.Cells(intRowIdx, intColIdx).Value Then Msgbox whatever & " item found!", VbOkOnly + vbInformation, "Found"
    7.         Next
    8.     Next

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    This worked (in principle):
    VB Code:
    1. Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("A1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    2.         xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    3.         , SearchFormat:=False).Activate

    Requires work, but much faster than iterating each and every cell.
    You will need to return the found (selected) cell once found. In this example I was looking for the Value in
    Cell A1 of Sheet1, in Sheet2

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    the only problem with this is i will have to specify the number of iterations by copying the code and change a1 to a2, then copying and changing it again to a3, etc. do you agree?

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    No

    It can be automated. I am playing with it now. I'm just trying to sort out the Find iteration.

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Part A nearly finished. This may give you an Idea:
    VB Code:
    1. Private Sub Find_Match()
    2. Dim lngIdx As Long
    3.  
    4.     'Start at A1 - IMPORTANT!
    5.     Worksheets("Sheet2").Range("A1").Select
    6.  
    7.     Do
    8.  
    9.         DoEvents
    10.  
    11.         Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("A1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    12.             xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    13.             , SearchFormat:=False).Activate
    14.    
    15.         If ActiveCell.Row < lngIdx Then
    16.             Exit Do
    17.         Else
    18.             lngIdx = ActiveCell.Row
    19.             '            
    20.             'Stuff here
    21.             '
    22.             Debug.Print ActiveCell.Row & "  " & ActiveCell.Column '<<<<<< For Testing ONLY
    23.  
    24.         End If
    25.  
    26.     Loop
    27.  
    28. End Sub
    Last edited by Bruce Fox; Jun 15th, 2006 at 04:06 AM.

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Question time for you.
    What is this offset range value? Need this so we can go on.


    The code above will ONLY capture a cell in WS2, that matches cell A1 of WS1.

    The next step will be to go on to A2, A3 etc. But how far considering you want a range pasted into WS1 - that will screw with the original data no?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    one minor thing before i answer your questions: how do i modify the code to match cells between two Excel files (i.e. two workBOOKS)?

    Answers to Questions:
    1. when i find a matching value in WS2, it needs to offset 0 rows and 8 columns to the right. then i need to copy a range of 3 cells to the right. then i need to go back to WS1, find the cell which i JUST matched, then offset 8 cells to the right and paste.

    2. yes the next step for me is to go onto a2, a3, etc. my problem is that i will never know how far i will need to go. it will be variable. it must work until there are no more cells in WS1 to match against WS2.

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Bugger, it's buggy; if cell A1 in WS2 was a match, this method wouldn't find it...... (After=ActiveCell since we have to focus on A1 at the start)

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Option 2 above.
    Cant you just add these values (matches from Sheet2) to a NEW Worksheet?

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    yes it definetly is a bugger! i much appreciate your help though.

  16. #16
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    What is the answer to post #14 above

    I have a different approach (automating "Find All"), but will need time to refine it.
    If I can't sus it out tonight, I'll post back tomorrow

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    thanks for your help

  18. #18
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Ok, A1 WS2 bug taken care of...
    VB Code:
    1. Private Sub Find_Match()
    2. Dim lngIdx As Long
    3.  
    4.     'Start at A1 - IMPORTANT!
    5.     Worksheets("Sheet2").Range("A1").Select
    6.  
    7.     If Worksheets("Sheet1").Range("A1").Value = Selection Then
    8.         'A1 WS2 is a match, do whatever. Now move on and check the rest
    9.         'Stuff
    10.         Debug.Print ActiveCell.Row & "  " & ActiveCell.Column   '<<<<< For testing Only
    11.     End If
    12.  
    13.     Do
    14.  
    15.         DoEvents
    16.  
    17.         Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("A1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    18.             xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    19.             , SearchFormat:=False).Activate
    20.    
    21.         If ActiveCell.Row < lngIdx Then
    22.             Exit Do
    23.         Else
    24.             lngIdx = ActiveCell.Row
    25.             'Stuff here
    26.             '
    27.             '
    28.             Debug.Print ActiveCell.Row & "  " & ActiveCell.Column   '<<<<< For testing Only
    29.         End If
    30.  
    31.     Loop
    32.  
    33. End Sub

  19. #19
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Where to from here.

    Turn the Sub Find_Match into a Function. Iterate through the WS1 "A" columb, passing the "A" X value (A1, A2, A3 etc until last used row) to the Function.

    The copy pasting etc you can do as you know what you want to do with the data.

    Yes, you can use 2 WorkBOOKS. Just create an instance of each, and use thier objects
    instead of the static ones I used to test this.


    Good luck.... (I'll be looking for your results tomorrow )

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    thanks for this. unfortunately im having problems. so ill address the first item.

    i changed the code to a private function. the macro then keeps looping. it doesnt seem to be able to exit the loop.
    Last edited by malley; Jun 15th, 2006 at 08:27 PM.

  21. #21
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    By default, lngIdx will be 0 when the function is called. (will also be 0 each and every time the funcion is called). However, I guess I should have assigned it 0 at the start of
    the Function - just to be sure . If was global (public) then that would have been another issue, as it would retain the last value. Note, (as mentioned) if it has scope only
    to that Function (or Sub) then it will be reset each time.

    Now, the looping issue? Worked fine for me.....
    Post the code YOU have at the moment please

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    this is the code i have. the data im testing it on compares sheet2, A1:A4 with sheet1, A1:A4.

    the code keeps pasting the X in B10 and B11.

    Public Function Find_Match()
    Dim lngIdx As Long

    'Start at A1 - IMPORTANT!
    Worksheets("Sheet2").Range("A1").Select

    If Worksheets("Sheet1").Range("a1").Value = Selection Then
    'A1 WS2 is a match, do whatever. Now move on and check the rest
    'Stuff
    Worksheets("Sheet2").Range("b10").Value = "X"
    Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    End If

    Do

    DoEvents

    Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("a1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate

    If ActiveCell.Row < lngIdx Then
    Exit Do
    Else
    lngIdx = ActiveCell.Row
    'Stuff here
    Worksheets("Sheet2").Range("b11").Value = "X"
    '
    Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    End If

    Loop

    End Function

  23. #23
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Use VBCODE tags to post your code snippets (see my Sig)

    I have reposted the above:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Function Find_Match()
    4. Dim lngIdx As Long
    5.  
    6. 'Start at A1 - IMPORTANT!
    7. Worksheets("Sheet2").Range("A1").Select
    8.  
    9. If Worksheets("Sheet1").Range("a1").Value = Selection Then
    10.     'A1 WS2 is a match, do whatever. Now move on and check the rest
    11.     'Stuff
    12.     Worksheets("Sheet2").Range("b10").Value = "X"
    13.     Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    14. End If
    15.  
    16. Do
    17.  
    18.     DoEvents
    19.  
    20.     Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("a1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    21.     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    22.     , SearchFormat:=False).Activate
    23.  
    24.     If ActiveCell.Row < lngIdx Then
    25.         Exit Do
    26.     Else
    27.         lngIdx = ActiveCell.Row
    28.         'Stuff here
    29.         Worksheets("Sheet2").Range("b11").Value = "X"
    30.         '
    31.         Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    32.     End If
    33.  
    34. Loop
    35.  
    36. End Function

  24. #24
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Quote Originally Posted by malley
    this is the code i have. the data im testing it on compares sheet2, A1:A4 with sheet1, A1:A4.
    No it isnt! (A1 only)

  25. #25
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Ok, slight change as your doing the work from within, it may aswell be a Sub (as opposed to a Function, which would normaly return).

    1. I made it a SUB,
    2. Included a Sub Parameter (rngRange) for future use,
    3. I set lngIdx = 0.

    VB Code:
    1. Option Explicit
    2.  
    3. Public Sub Find_Match(rngRange As Range)
    4. Dim lngIdx As Long
    5.  
    6. lngIdx = 0
    7.  
    8. 'Start at A1 - IMPORTANT!
    9. Worksheets("Sheet2").Range("A1").Select
    10.  
    11. If Worksheets("Sheet1").Range("a1").Value = Selection Then
    12.     'A1 WS2 is a match, do whatever. Now move on and check the rest
    13.     'Stuff
    14.     Worksheets("Sheet2").Range("b10").Value = "X"
    15.     Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    16. End If
    17.  
    18. Do
    19.  
    20.     DoEvents
    21.  
    22.     Worksheets("Sheet2").Cells.Find(What:=Worksheets("Sheet1").Range("a1").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    23.     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    24.     , SearchFormat:=False).Activate
    25.  
    26.     If ActiveCell.Row < lngIdx Then
    27.         Exit Do
    28.     Else
    29.         lngIdx = ActiveCell.Row
    30.         'Stuff here
    31.         Worksheets("Sheet2").Range("b11").Value = "X"
    32.         '
    33.         Debug.Print ActiveCell.Row & " " & ActiveCell.Column '<<<<< For testing Only
    34.     End If
    35.  
    36. Loop
    37.  
    38. End Sub
    Last edited by Bruce Fox; Jun 15th, 2006 at 08:54 PM.

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    sorry i had the wrong impression there. just to confirm, your code will take cell sheet2!A1 and search for it in sheet1.

  27. #27
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Can you attach the WorkBook(s).

  28. #28
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Quote Originally Posted by malley
    sorry i had the wrong impression there. just to confirm, your code will take cell sheet2!A1 and search for it in sheet1.
    Close, will find all instances of WS1 "A1" value within WS2.

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    how do i attach files?

  30. #30
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    At the bottom (where you have been replying to) select "Go Advanced"
    When that page loads, you will see a "Manage Attachments" button - use that.
    Should be self explanitory.

  31. #31

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    ok here is the file.

    i think the problem is that due to my misunderstanding (our poor communication), i was trying to use your code to compare two columns of data. i.e. i thought the code would take cell a1, look for it in another sheet, find it, and then perform some action. Then take cell a2, look for in another sheet, find it, and then perform some action.... etc.
    Attached Files Attached Files

  32. #32
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    First part is correct. It WILL find ALL matches on Sheet2. But the other passes (for A2, A3 etc) have yet to be implimented. But that is why I added a parameter to the Sub, so we can do that latter.

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    what do you mean "added a parameter"? what exactly did you add?

  34. #34
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Please dont take this the wrong way, it not ment to be an insult; but you seem out of your depth for this type of project - You need to be able to walk befor you run.

    Why are you doing this? Assignment? Work?

  35. #35

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    i am out of my depth! its for work. unfortunately i have no choice but to sort this out.

  36. #36
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    Are you a paid programmer?

  37. #37

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    no finance analyst. i have very little knowledge of VB (obviously) but in the past have been able to get by using these forums.

  38. #38

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    no. finance analyst. i have very little knowledge of VB (obviously) but in the past have been able to get by using these forums.

  39. #39
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Find function - PLEASE HELP!

    I am now concerned as without days of BETA testing with dummy data etc, this is fraught with danger! - Are you aware of that.

  40. #40

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Find function - PLEASE HELP!

    no i was not aware of that. for the moment though, i need to reach a temporary solution. i have to go lunch now!

Page 1 of 2 12 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width