Consulting

Results 1 to 4 of 4

Thread: Making a function based on Find having IF

  1. #1

    Making a function based on Find having IF

    Hello!

    Im trying to make a function that gets the value from certain cell (for example: S-1140M), and if other cell has value "X" it searches for the value in sheet3 columns C:C and gives Offset(0,2) result, if no "X" then it searches columns L:L in sheet3 and also gives Offset(0,2) result.

    Its doable in Excel, but the formula is very long and not elegant at all.
    =If($D$2="X";FIND(I9;'CENNIK CZĘŚCI'!C:C;'CENNIK CZĘŚCI'!E:E;0;1;1);FIND(I9;'CENNIK CZĘŚCI'!L:L;'CENNIK CZĘŚCI'!N:N;0;1;1))
    I had to translate the formulas so Find may not be 100% accurate, as in my language its something like "x.Find".

    I came up with this code, but it doesnt work at all.
    Function Searchy(Value As Range) As Integer
    Dim First As Range
    Dim Second As Range
    Set First = Workbooks("3_Wycena").Worksheets("CENNIK CZĘŚCI").Range("C:C").Find(Range(Value).Offset(0, 2).Value)
    Set Second = Workbooks("3_Wycena").Worksheets("CENNIK CZĘŚCI").Range("L:L").Find(Range(Value).Offset(0, 2).Value)
    If Range("D1").Value = "X" Then
      Searchy = First
    Else
      Searchy = Second
    End If
    End Function
    Could you please help me? Im pretty new to VBA, i could do it in Sub procedure, but i need Function. Its definitely doable, since Excel built-in formulas allow me to do it.
    Last edited by Aussiebear; 11-23-2022 at 03:38 AM. Reason: Applied code tags to supplied code

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Welcome to the forum!

    How about the function on the attached?
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    G'day asktheages, Welcome to the vbaexpress excel forum.

    Excel's Find function is normally used to "find" a substring with a string. So we will need to replace that with something else.


    Never mind Georgiboy has done the work whilst I was on the phone
    Last edited by Aussiebear; 11-23-2022 at 04:11 AM. Reason: Phones can't live with them, can't live without them
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Quote Originally Posted by georgiboy View Post
    Welcome to the forum!

    How about the function on the attached?
    This!!! Works perfectly well, thank you!!

Posting Permissions

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