+ Reply to Thread
Results 1 to 16 of 16

VBA search and retunr results - simple programming

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2017
    Location
    Southampton
    MS-Off Ver
    Office 2013
    Posts
    4

    VBA search and retunr results - simple programming

    Hi,

    I know this is probably basic programming for some of you: -) but after spending all afternoon watching youtube videos and getting
    the same error message 'For without next' and 'Cannot execute code without break' I finally give up. ;-(
    I have no training or knowledge of excel apart from as a user.

    PLEASE HELP!!!
    ok....ill keep it simple

    on sheet1 I have a search box to enter 'store number; (and a button to link to the macro).
    if a match is found on sheet2 column A (sheet 2 has 5 columns) results should be displayed back on sheet1 D6,E6,F6,G6,H6
    there is more than one entry for the same store on sheet 2 they all need to be displayed on sheet 1 and its not in order.

    this what I've tried so far.....copying a youtube video but it was design to only return one row I need all matches to be returned....
    and it didn't work anyway.

         
    Sub searchdata()
    
    Dim erow As Long
    Dim ws As Worksheet
    Dim lastrow As Long
    Dim count As Integer
    
    lastrow = Sheets("Data2").cell(Rows.count, 1).End(x1up).Row
    
    For X = 2 To lastrow
    If Sheets("Data2").cellss(X, 1) = Sheet1.Range("b4") Then
    Sheet1.Range("E6") = Sheets("data2").Cells(X, 1)
    Sheet1.Range("F6") = Sheets("data2").Cells(X, 2)
    Sheet1.Range("G6") = Sheets("data2").Cells(X, 3)
    Sheet1.Range("H6") = Sheets("data2").Cells(X, 4)
    Sheet1.Range("I6") = Sheets("data2").Cells(X, 5)
    End If
            
    End Sub
    PS. This spread sheet is for a bank and I can't upload any of the information or onto here :-(
    Last edited by renangv; 04-10-2017 at 02:27 PM.

  2. #2
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Hi renangv

    Your post does not comply with Rule 3 of the Forum....
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window
    Last edited by Sintek; 04-10-2017 at 02:33 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA search and retunr results - simple programming

    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.

    Click on Edit to open your thread, then 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
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Please attach a sample workbook with a before & after scenario making sure that your desired solution is shown - mock up the results manually.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA search and retunr results - simple programming

    from what I can see, you need to add
    Dim X As Long
    with the rest of the dim statements. And add
    Next X
    right after the End If. Note that if there are multiple results found, the last result will overwrite the preceeding ones.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Perhaps this can start you off. Would be better seeing a sample file.
    Option Explicit
    
    Sub StoreNo_Find()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim r As Integer, x As Integer, c As Integer
    Dim storeNo As String
    storeNo = InputBox("Enter Store Number")
    lastRow = Sheets("Sheet2").Cells(Rows.count, "A").End(xlUp).Row
    x = 5
    For r = 1 To lastRow
        For c = 1 To 5
            If Sheets("Sheet2").Cells(r, c) = storeNo Then
                Sheet1.Cells(6, x) = Sheets("Sheet2").Cells(r, c)
                x = x + 1
            End If
        Next c
    Next r
    End Sub
    Last edited by Sintek; 04-10-2017 at 02:51 PM. Reason: Changed x value to 5 Column E

  7. #7
    Registered User
    Join Date
    04-10-2017
    Location
    Southampton
    MS-Off Ver
    Office 2013
    Posts
    4

    Re: VBA search and retunr results - simple programming

    IMG_0478.JPGIMG_0479.JPG
    i've taken some pictures of a sample as i can't upload files from this computer.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA search and retunr results - simple programming

    You added the pics and not said anything about any of the solutions that have been given. Are the codes not working? If not what is not working?

  9. #9
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Here you go...
    Option Explicit
    
    Private Sub CommandButton1_Click()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Data2")
    Dim nextRow As Long
    Dim lastRow As Long
    Dim Indicator As String
    Dim ws As Worksheet
    Dim r As Integer, x As Integer, c As Integer
    Dim storeNo As String
    storeNo = ws1.Range("B4").Value
    nextRow = ws1.Range("D" & Rows.count).End(xlUp).Row + 1
    Indicator = False
    With ws2
        lastRow = ws2.Cells(Rows.count, "A").End(xlUp).Row
        For r = 2 To lastRow
            If .Cells(r, 1) = storeNo Then
                ws1.Cells(nextRow, 4).Resize(, 5) = Array(ws2.Cells(r, 1), ws2.Cells(r, 2), ws2.Cells(r, 3), ws2.Cells(r, 4), ws2.Cells(r, 5))
            nextRow = nextRow + 1
            Indicator = True
            End If
        Next r
        If Indicator = False Then
            MsgBox "No entries found for that No", vbInformation, ""
        End If
    End With
    End Sub
    Attached Files Attached Files
    Last edited by Sintek; 04-11-2017 at 02:59 AM. Reason: Added attachment

  10. #10
    Registered User
    Join Date
    04-10-2017
    Location
    Southampton
    MS-Off Ver
    Office 2013
    Posts
    4

    Smile Re: VBA search and retunr results - simple programming

    Hi Sintek,

    Thank you for your efforts.
    Your spreadsheet it pretty similar to what i need,
    what do i need to add in order for the previous data to clear when you search a new number. *sorry i forgot to mention this.*

    Thanks this is just the initial project i will be adding on more data and more tabs as it progresses but i think this has been really helpful to start me up !

    Quote Originally Posted by sintek View Post
    Here you go...
    Option Explicit
    
    Private Sub CommandButton1_Click()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Data2")
    Dim nextRow As Long
    Dim lastRow As Long
    Dim Indicator As String
    Dim ws As Worksheet
    Dim r As Integer, x As Integer, c As Integer
    Dim storeNo As String
    storeNo = ws1.Range("B4").Value
    nextRow = ws1.Range("D" & Rows.count).End(xlUp).Row + 1
    Indicator = False
    With ws2
        lastRow = ws2.Cells(Rows.count, "A").End(xlUp).Row
        For r = 2 To lastRow
            If .Cells(r, 1) = storeNo Then
                ws1.Cells(nextRow, 4).Resize(, 5) = Array(ws2.Cells(r, 1), ws2.Cells(r, 2), ws2.Cells(r, 3), ws2.Cells(r, 4), ws2.Cells(r, 5))
            nextRow = nextRow + 1
            Indicator = True
            End If
        Next r
        If Indicator = False Then
            MsgBox "No entries found for that No", vbInformation, ""
        End If
    End With
    End Sub

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: VBA search and retunr results - simple programming

    This one should cover that.

    Private Sub CommandButton1_Click()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim nextRow As Long, r As Long
    Dim storeNo As String, Indicator As Boolean
    
    Set ws1 = Sheets("Sheet1"): Set ws2 = Sheets("Data2")
    Indicator = False
    
    With ws1
        .Cells(5, 4).CurrentRegion.Offset(1).ClearContents
        storeNo = .Range("B4").Value
        nextRow = .Range("D" & Rows.count).End(xlUp).Row + 1
    End With
    
    With ws2
        For r = 2 To .Cells(.Rows.count, "A").End(xlUp).Row
            If .Cells(r, 1) = storeNo Then
                ws1.Cells(nextRow, 4).Resize(, 5) = Array(.Cells(r, 1), .Cells(r, 2), .Cells(r, 3), .Cells(r, 4), .Cells(r, 5))
            nextRow = nextRow + 1: Indicator = True
            End If
        Next r
        If Indicator = False Then MsgBox "No entries found for that No", vbInformation, ""
    End With
    End Sub
    Last edited by bakerman2; 04-11-2017 at 06:01 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Yep, that covers it ....bakerman2 to the rescue lol

  13. #13
    Registered User
    Join Date
    04-10-2017
    Location
    Southampton
    MS-Off Ver
    Office 2013
    Posts
    4

    Re: VBA search and retunr results - simple programming

    Yep . That is perfect :-) thanks Everyone
    Thanks Sintek and Bakerman2

  14. #14
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    @ bakerman2

    Indicator As Boolean
    Of course...Silly me.

  15. #15
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,414

    Re: VBA search and retunr results - simple programming

    Pleasure...Thanks for rep points

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: VBA search and retunr results - simple programming

    @ renangv

    You're welcome and thanks for rep+.

+ 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. Simple search box column and retrive results to search box sheet
    By Tanakaka in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 01-08-2017, 08:47 AM
  2. Look up 2 coloumn and retunr the multiple values
    By kiranmayi16 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-31-2014, 02:29 PM
  3. Simple search function and results display.
    By dgbradl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2013, 06:33 PM
  4. MATCH and INDEX function retunr the same value
    By albertoro in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 01-04-2013, 10:44 PM
  5. Simple Spreadsheet programming help
    By theebookzoo in forum Excel General
    Replies: 2
    Last Post: 08-25-2008, 03:27 PM
  6. Programming a simple formula
    By anagaraj1 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 08-04-2005, 11:05 AM
  7. Simple Programming Function??
    By Texins Karate in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2005, 01:06 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