+ Reply to Thread
Results 1 to 7 of 7

Function in a cell to search for a text

Hybrid View

Lilly Function in a cell to search... 04-24-2007, 01:59 AM
sweep Hi, Can you attach an... 04-24-2007, 03:51 AM
Leith Ross Hello Lilly, This macro is... 04-24-2007, 03:57 AM
Lilly Hi Ross this is Lilly 04-24-2007, 07:13 AM
starguy Following formula is... 04-24-2007, 07:27 AM
Lilly Thankyou! 04-24-2007, 07:52 AM
starguy You're welcome and thank you... 04-24-2007, 08:42 AM
  1. #1
    Registered User
    Join Date
    04-23-2007
    Posts
    8

    Function in a cell to search for a text

    Hi,

    Pl do help me on this:

    I have entered the names of places falling under category "X" in column A , category "Y" in column B and category "Z" in column C of sheet 1 in excel. Now in sheet 2 column B, I enter the name of a place. In the first cell of column C, I want to write a formula where in it should check if the place entered in sheet 2 column B falls under any of the categories X, Y, Z of sheet 1, and it should display X,Y, Z accordingly or it should display just "Others".

    I need the formula for this so that if it is written in one cell it can be dragged down to all the cells of column C in sheet 2. Note that I am entering the names of places ie. they are texts and not numbers.

    Looking forward to replies.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454
    Hi,

    Can you attach an example of what you neeed?

    Dave

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Lilly,

    This macro is a User Defined Function. It works like a regular Worksheet function. It determines the length of columns A to C on the specified worksheet. The category search columns can not be on the same worksheet as the search term.

    Copy this code. After you have inserted a Standard VBA Module into your Workbook, paste the code into it.

    Example:
    Sheet2 "B1" = "London"
    Sheet2 "C1" Formula: =FindCategory(B1, 'Sheet1'!A:C)
    C1 will display what categories matched.

    Public Function FindCategory(Place_Name As Range, Category_Columns As Range)
    
      Dim Cat As String
      Dim CatA As String
      Dim CatB As String
      Dim CatC As String
      Dim LR(2) As Long
      Dim LastRow As Long
      Dim Wks As Worksheet
      Dim WksName As String
    
      'Udpate when any linked cells are updated or sheet calculates
        Application.Volatile
    
        WksName = [Category_Columns].Parent.Name
          If WksName = ActiveSheet.Name Then
             MsgBox "Category columns must be on a different sheet." 
             FindCategory = ""
             Exit Funtion
          End If
    
        Set Wks = Worksheets(WksName)
        LR(0) = Wks.Cells(Rows.Count, "A").End(xlUp).Row
        LR(1) = Wks.Cells(Rows.Count, "B").End(xlUp).Row
        LR(2) = Wks.Cells(Rows.Count, "C").End(xlUp).Row
    
        'Find the longest row of the 3 columns
          LastRow = ActiveSheet.WorksheetFunction.Max(LR(0), LR(1), LR(2))
    
          For R = 1 To LastRow
            If Wks.Cells(R, "A") = Place_Name Then CatA = "X,"
            If Wks.Cells(R, "B") = Place_Name Then CatB = "Y,"
            If Wks.Cells(R, "C") = Place_Name Then CatC = "Z,"
          Next R
    
          If CatA <> "" Then Cat = Cat & CatA
          If CatB <> "" Then Cat = Cat & CatB
          If CatC <> "" Then Cat = Cat & CatC
          If Cat = "" Then 
             Cat = "Other"
          Else
             Cat = Left(Cat, Len(Cat) - 1)
          End If
    
          FindCategory = Cat
    
    End Function
    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    04-23-2007
    Posts
    8

    Hi Ross this is Lilly

    I entered the codes and also typed the formula in the cell but am getting an error "Name" in the cell

    Actually where should I enter the codes after opening the excel sheet?

    Let me be more specific by giving you the details:

    1) In sheet 1, I have the following data for eg:

    Metropolis Urban Semi-Urban

    Delhi Hyderabad Adilabad
    Mumbai Vijayawada Kagaznagar
    Kolkata Vishakapatnam Nirmal
    Chennai Guntur Bellampalle
    Guwahati Mandamarri
    Patna Mancherial
    Chandigarh Nizamabad
    Durg-Bhilai Bodhan
    Nagar Kamareddy
    Raipur Ramagundam
    Ahmadabad Jagtial

    The category names is given in the first row.ie Metropolis,etc.

    Now in sheet 2 column D cell 2 i will enter the name of the place. In column K , I want this formula such that the place entered in cell 2 of Column D sheet 2 will be categorised into either Metro, Urban, semiurban and if the place does not fall in any of these categories it should display "rural"

    Pl do tell me specifically what I should do. Thanks a lot for all the effort once again. Looking forward to your help.

    Lilly

  5. #5
    Forum Contributor
    Join Date
    03-14-2006
    Location
    Pakistan
    Posts
    1,791

    Lightbulb

    Quote Originally Posted by Lilly
    I entered the codes and also typed the formula in the cell but am getting an error "Name" in the cell

    Actually where should I enter the codes after opening the excel sheet?

    Let me be more specific by giving you the details:

    1) In sheet 1, I have the following data for eg:

    Metropolis Urban Semi-Urban

    Delhi Hyderabad Adilabad
    Mumbai Vijayawada Kagaznagar
    Kolkata Vishakapatnam Nirmal
    Chennai Guntur Bellampalle
    Guwahati Mandamarri
    Patna Mancherial
    Chandigarh Nizamabad
    Durg-Bhilai Bodhan
    Nagar Kamareddy
    Raipur Ramagundam
    Ahmadabad Jagtial

    The category names is given in the first row.ie Metropolis,etc.

    Now in sheet 2 column D cell 2 i will enter the name of the place. In column K , I want this formula such that the place entered in cell 2 of Column D sheet 2 will be categorised into either Metro, Urban, semiurban and if the place does not fall in any of these categories it should display "rural"

    Pl do tell me specifically what I should do. Thanks a lot for all the effort once again. Looking forward to your help.

    Lilly
    Following formula is implemented in attached workbook

    =IF(NOT(ISERROR(MATCH(C5,Sheet1!B:B,0))),"Metro",IF(NOT(ISERROR(MATCH(C5,Sheet1!C:C,0))),"Urban",IF(NOT(ISERROR(MATCH(C5,Sheet1!D:D,0))),"Semi Urban","Rural")))
    Attached Files Attached Files
    Last edited by starguy; 04-24-2007 at 07:31 AM.

  6. #6
    Registered User
    Join Date
    04-23-2007
    Posts
    8

    Thankyou!

    Hello Starguy,

    That was great! It is simple and really works. I am extremely grateful to you.

    May I ask you for some more help with regard to VBA codes and programming?

    Pl do let me know.

    Lilly

  7. #7
    Forum Contributor
    Join Date
    03-14-2006
    Location
    Pakistan
    Posts
    1,791

    Lightbulb

    Quote Originally Posted by Lilly
    Hello Starguy,

    That was great! It is simple and really works. I am extremely grateful to you.

    May I ask you for some more help with regard to VBA codes and programming?

    Pl do let me know.

    Lilly
    You're welcome and thank you for the feedback.
    You can browse following thread for links to learn VBA and Excel Programming.

    http://www.excelforum.com/showthread.php?t=584092

+ Reply to Thread

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