+ Reply to Thread
Results 1 to 4 of 4

what if i want more than two columns to show my result

  1. #1
    Registered User
    Join Date
    09-22-2014
    Location
    Korea
    MS-Off Ver
    2010
    Posts
    9

    what if i want more than two columns to show my result

    Hello, i found this code a while back and it is exactly what i needed but now i need to add new columns to my search list. how do i make that possible?

    I have only been learning for this week and all the arrParts and VBA codes are oh so new. and i havent been able to modify this so i can add a C or D column when my results show.

    Please help me. Thank you very much.

    Code:

    Sub SearchParts()
    Dim arrParts() As Variant
    Range("A7", "B" & Cells(Rows.CountLarge, "B").End(xlDown).Row).Clear
    arrParts = FindParts(CStr(Trim(Cells(2, 2))))
    Range("A7").Resize(UBound(arrParts, 2), UBound(arrParts)) = _
    WorksheetFunction.Transpose(arrParts)
    End Sub
    Private Function FindParts(PartNumber As String) As Variant
    Dim ws As Worksheet
    Dim FoundCell As Range
    Dim LastCell As Range
    Dim rngParts As Range
    Dim FirstAddr As String
    Dim arrPart() As Variant

    Set ws = Worksheets("Data")
    Set rngParts = ws.Range("B2:C" & ws.Cells(Rows.CountLarge, "B").End(xlUp).Row)

    With rngParts
    Set LastCell = .Cells(.Cells.Count)
    End With

    Set FoundCell = rngParts.Find(What:=PartNumber, After:=LastCell, LookAt:=xlPart)

    If Not FoundCell Is Nothing Then
    FirstAddr = FoundCell.Address
    End If

    ReDim arrPart(1 To 2, 1 To 1)
    Do Until FoundCell Is Nothing
    arrPart(1, UBound(arrPart, 2)) = FoundCell.Offset(0, -1)
    arrPart(2, UBound(arrPart, 2)) = FoundCell.Value

    ReDim Preserve arrPart(1 To 2, 1 To UBound(arrPart, 2) + 1)

    Set FoundCell = rngParts.FindNext(After:=FoundCell)
    If FoundCell.Address = FirstAddr Then
    Exit Do
    End If
    Loop
    FindParts = arrPart
    End Function

  2. #2
    Registered User
    Join Date
    09-22-2014
    Location
    Korea
    MS-Off Ver
    2010
    Posts
    9

    Re: what if i want more than two columns to show my result

    fixed it. boy, english is hard.

    Sub SearchParts()
    Dim arrParts() As Variant
    Range("A7", "D" & Cells(Rows.CountLarge, "D").End(xlDown).Row).Clear
    arrParts = FindParts(CStr(Trim(Cells(2, 2))))
    Range("A7").Resize(UBound(arrParts, 2), UBound(arrParts)) = _
    WorksheetFunction.Transpose(arrParts)
    End Sub


    and

    ReDim arrPart(1 To 4, 1 To 1)
    Do Until FoundCell Is Nothing
    arrPart(1, UBound(arrPart, 2)) = FoundCell.Offset(0, -1)
    arrPart(2, UBound(arrPart, 2)) = FoundCell.Value
    arrPart(3, UBound(arrPart, 2)) = FoundCell.Offset(0, 1)
    arrPart(4, UBound(arrPart, 2)) = FoundCell.Offset(0, 2)

    ReDim Preserve arrPart(1 To 4, 1 To UBound(arrPart, 2) + 1)

    Set FoundCell = rngParts.FindNext(After:=FoundCell)
    If FoundCell.Address = FirstAddr Then


    im so happy

  3. #3
    Registered User
    Join Date
    09-22-2014
    Location
    Korea
    MS-Off Ver
    2010
    Posts
    9

    Re: what if i want more than two columns to show my result

    fixed it. boy, english is hard.

    Sub SearchParts()
    Dim arrParts() As Variant
    Range("A7", "D" & Cells(Rows.CountLarge, "D").End(xlDown).Row).Clear
    arrParts = FindParts(CStr(Trim(Cells(2, 2))))
    Range("A7").Resize(UBound(arrParts, 2), UBound(arrParts)) = _
    WorksheetFunction.Transpose(arrParts)
    End Sub


    and

    ReDim arrPart(1 To 4, 1 To 1)
    Do Until FoundCell Is Nothing
    arrPart(1, UBound(arrPart, 2)) = FoundCell.Offset(0, -1)
    arrPart(2, UBound(arrPart, 2)) = FoundCell.Value
    arrPart(3, UBound(arrPart, 2)) = FoundCell.Offset(0, 1)
    arrPart(4, UBound(arrPart, 2)) = FoundCell.Offset(0, 2)

    ReDim Preserve arrPart(1 To 4, 1 To UBound(arrPart, 2) + 1)

    Set FoundCell = rngParts.FindNext(After:=FoundCell)
    If FoundCell.Address = FirstAddr Then


    im so happy

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: what if i want more than two columns to show my result

    I am happy that you are happy

    For future reference, please note Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/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 here
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ 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. Replies: 1
    Last Post: 01-25-2013, 01:58 PM
  2. Replies: 4
    Last Post: 07-20-2011, 09:20 AM
  3. Replies: 4
    Last Post: 11-17-2008, 12:15 PM
  4. Show result as +20
    By jimmisavage in forum Excel General
    Replies: 5
    Last Post: 06-26-2007, 10:38 AM
  5. [SOLVED] Advanced formula - Return result & Show Cell Reference of result
    By Irv in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-05-2006, 10:40 PM

Tags for this Thread

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