Results 1 to 17 of 17

Thread: [RESOLVED] Finding a vlaue and returning to MSGBOX, i have some code already.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Resolved [RESOLVED] Finding a vlaue and returning to MSGBOX, i have some code already.

    Hello i am trying to find a value from a cell in on one sheet, the user would press a command button which will then run the code to look in another sheet for the data, Should it find a match it will return data from the line it found the match. and show it in a message box, the message box will then ask if the user wants to delete, if they choose yes it will wipe the data on the whole line it found the match.
    I have the code but the message box doesnt display any returned data, and it wont delete any data either please help!

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    vb Code:
    1. Sub commandbutton1_click()
    2.  
    3. Dim shtarr(1) As String, s As Integer
    4. shtarr(0) = "Diary"
    5. sheetname = shtarr(s)
    6. For s = 0 To UBound(shtarr)
    7. Set r = Sheets(shtarr(s)).UsedRange.Find(Sheets("DSA").Range("B2").Value)            '<--check
    8. If Not r Is Nothing Then   ' found
    9. Dim msg1 As String
    10. Dim response1
    11. ' you need to select case, to assign variables before trying to use them n the msgbox
    12. Select Case r.Column                     '<--select case for return value
    13. End Select
    14. msg1 = "The customer matching NINO: " & UsedRange.Text _
    15. & vbCrLf & vbCrLf & "Already has an appointment on: " & r.Offset(0, -2) & r.Offset(0, -1) _
    16. & vbCrLf & vbCrLf & "For: " & r.Offset(0, 3) & " with: " & r.Offset(0, 5) _
    17. & vbCrLf & vbCrLf & "This appointment is: " & r.Offset(0, 4) & "And was booked by: " & r.Offset(0, 6) _
    18. & vbCrLf & vbCrLf & "Would you like to delete this appointment?"
    19. response1 = MsgBox(msg1, vbExclamation + vbYesNo)
    20. If response1 = vbYes Then
    21. If Not r Is Nothing Then Range(r, r.Offset(0, 6)).ClearContents
    22. ElseIf response1 = vbNo Then
    23. MsgBox "No Details Where Changed, Your Input Will Be Cleared", vbInformation + vbOKOnly
    24. Exit Sub
    25. Else
    26. MsgBox "No Data"
    27. End If
    28. End If
    29. Next
    30. End Sub

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    with no indenting your code is very hard to follow
    you have a select case that is doing nothing at all
    i doubt that usedrange.text is valid without a sheet object, or what you actually would want in your message
    you should not need a second if not r is nothing as you are already within that condition
    you probably want
    r.entirerow.clearcontents (or r.entirerow.delete)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    The usedrange.text is meant to be what the user has typed to search for, the select case was meant to be removed. I am really bad for not using indents as im sure your aware by now also r.entirerow.clearcontents or r.entirerow.delete would work as the first 2 columns on the holding sheets are protected and must remain intact, so i need 3 to 8 only clearing.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Hello Pete or anyone in fact, i have been messing with the code and re-arranged and hopefully indented it properly well the best i can anyway, now i still cant get a message box to display certain information from the found row and i cant get it to clear contents only in columns c to k, leaving column A and B untouched.
    Here is the code can you give any pointers?

    vb Code:
    1. Private Sub commandbutton1_Click()
    2.  
    3.     Dim vOppLocation As Variant
    4.     Dim c As Variant
    5.     Dim d As Variant
    6.     Dim e As Variant
    7.     Dim f As Variant
    8.     Dim g As Variant
    9.     Dim h As Variant
    10.     Dim i As Variant
    11.     Dim j As Variant
    12.     Dim k As Variant
    13.        
    14.     'Set c = Sheets("Diary").Range("c").Value
    15.     'Set d = Sheets("Diary").Range("d").Value
    16.     'Set e = Sheets("Diary").Range("e").Value
    17.     'Set f = Sheets("Diary").Range("f").Value
    18.     'Set g = Sheets("Diary").Range("g").Value
    19.     'Set h = Sheets("Diary").Range("h").Value
    20.     'Set i = Sheets("Diary").Range("i").Value
    21.     'Set j = Sheets("Diary").Range("j").Value
    22.     'Set k = Sheets("Diary").Range("k").Value
    23.    
    24.     ' -- Check a number has been completed
    25.     If Sheets("DSA").Range("B2") = "" Then
    26.    
    27.         MsgBox "Please enter a Ref Number to find.", vbCritical + vbOKOnly, "Reference Number"
    28.         Sheets("DSA").Range("B2").Select
    29.         Exit Sub
    30.        
    31.     End If
    32.    
    33.     ' -- Search for reference number
    34.     Set vOppLocation = Sheets("Diary").Range("E:E").Find(Sheets("DSA").Range("B2"), LookIn:=xlValues, LookAt:=xlWhole)
    35.    
    36.     If Not vOppLocation Is Nothing Then
    37.    
    38.         ' -- Opp found, Question User
    39.         MsgBox "A Match for : " & Sheets("DSA").Range("B2").Value & " was found" _
    40.         & vbCr & "Would you like to delete it?", _
    41.         vbInformation + vbYesNo, _
    42.         "Found"
    43.  
    44.         Sheets("Diary").Range(vOppLocation.Row & c, k).ClearContents
    45.         ' -- Clear search box
    46.         Sheets("DSA").Range("B2") = ""
    47.    
    48.         MsgBox "The Ref Number" & sheets("DSA").range("B2").value & "removed.", vbInformation +      vbOKOnly, "Remove Ref"
    49.         Sheets("DSA").Range("B2").Select
    50.    
    51.     Else
    52.    
    53.         ' -- Opp not found
    54.         MsgBox "Nothing was found for :  " & Sheets("DSA").Range("B2").Value _
    55.         & vbCrLf & "Please check your input or presume it doesnt exist.", _
    56.         vbCritical + vbOKOnly, _
    57.         "Opportunity Not Found"
    58.            
    59.     End If
    60. End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    i have been messing with the code and re-arranged and hopefully indented it properly well the best i can anyway, now i still cant get a message box to display certain information from the found row
    which msgbox do show?
    if "a match for" is not showing i would assume that the reference number is not found

    i can only suggest you post a sample workbook (zip first) xl 2003 version, with some data and your code and i will test
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    you want a form that asks for a matchable thing and it asks if found whether or not you wish to delete it from the other sheet....


    do your really mean you want a deletion process that searches for a given thing and deletes it if found, after confirmation?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Yes i have the code that will find a match and also tell me if it didnt find a match, Now the message box that prompts the user if they wish to delete, wants to consist of data from the row it finds the match on.
    such as:

    vb Code:
    1. MsgBox "A Match for : " & Sheets("DSA").Range("B2").Value & " was found" _
    2.         & vbCr & "Currently the appointment stands at" & Column1 & Column2 & "if these details are wrong would you like to delete?", _
    3.         vbInformation + vbYesNo, _
    4.         "Found"

    So the message box would display the current details for the match, if the user doesnt want to keep the data they can press yes to clear, it would then only clearcontents of column C to K on the matched row.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    So i have been tweaking and this is sort of the direction i believe is correct, but i cant seem to get the r.offset function to work?
    I think i have to declare the col variable somewhere.

    vb Code:
    1. MsgBox "A Match for : " & vOppLocation & " was found" _
    2.         & vbCr & "The Current Appointment is on" & vOppLocation(r.Offset(col, -4)) & "at time" & vOppLocation(r.Offset(col, -3)) _
    3.         & vbCr & "Would you like to delete it?", _
    4.         vbInformation + vbYesNo, _
    5.         "Found"

    As you can see vopplocation is referred to as where it finds the match, then on that same row im trying to get the offsets of other columns that match this search, so the user can see what they have looked for before deleting.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Ok i have managed to get there myself thank you anyways but if i could just get a small amount of help before i mark resolved, here is the code as follows and i will explain below.

    vb Code:
    1. Private Sub commandbutton1_Click()
    2.  
    3.     Dim vOppLocation As Variant
    4.  
    5.    
    6.     ' -- Check a number has been completed
    7.     If Sheets("DSA").Range("B2") = "" Then
    8.    
    9.         MsgBox "Please enter a ref Number to find.", vbCritical + vbOKOnly, "Reference Number"
    10.         Sheets("DSA").Range("B2").Select
    11.         Exit Sub
    12.        
    13.     End If
    14.    
    15.     ' -- Search for reference number
    16.     Set vOppLocation = Sheets("Diary").Range("E:E").Find(Sheets("DSA").Range("B2"), LookIn:=xlValues, LookAt:=xlWhole)
    17.     If Not vOppLocation Is Nothing Then
    18.    
    19.         ' -- Opp found, Question User
    20.         MsgBox "You Have Searched for : " & vOppLocation & " and the following data was returned" _
    21.         & vbCrLf & "Customers Name : " & vOppLocation.Offset(0, -2) & " " & vOppLocation.Offset(0, -1) _
    22.         & vbCrLf & "The Current Appointment Date is : " & vOppLocation.Offset(0, -4) _
    23.         & vbCrLf & "The Current Appointment Time is : " & vOppLocation.Offset(0, -3) _
    24.         & vbCrLf & "The Customers D.O.B is : " & vOppLocation.Offset(0, 1) _
    25.         & vbCrLf & "The Customers Telephone Number is : " & vOppLocation.Offset(0, 2) _
    26.         & vbCrLf & "The Customers Eligibility is : " & vOppLocation.Offset(0, 3) _
    27.         & vbCrLf & "Support Needed by the Customer : " & vOppLocation.Offset(0, 4) _
    28.         & vbCrLf & "The Current Contract Holder is : " & vOppLocation.Offset(0, 5) _
    29.         & vbCrLf & "The Original Referrer is : " & vOppLocation.Offset(0, 6), _
    30.         vbInformation + vbYesNo, _
    31.         "Found"
    32.  
    33.         'vOppLocation.Offset(0, -2).ClearContents
    34.         'vOppLocation.Offset(0, -1).ClearContents
    35.         'vOppLocation.Offset(0, 0).ClearContents
    36.         'vOppLocation.Offset(0, 1).ClearContents
    37.         'vOppLocation.Offset(0, 2).ClearContents
    38.         'vOppLocation.Offset(0, 3).ClearContents
    39.         'vOppLocation.Offset(0, 4).ClearContents
    40.         'vOppLocation.Offset(0, 5).ClearContents
    41.         'vOppLocation.Offset(0, 6).ClearContents
    42.        
    43.         ' -- Clear search box
    44.  
    45.    
    46.         MsgBox "All appointments and details matching " & Sheets("DSA").Range("B2").Value & " have been removed", vbInformation + vbOKOnly, "Remove Opportunity"
    47.         ElseIf vbYesNo = vbNo Then
    48.         MsgBox "Nothing Removed"
    49.         Exit Sub
    50.         Sheets("DSA").Range("B2") = ""
    51.         Sheets("DSA").Range("B2").Select
    52.    
    53.     Else
    54.    
    55.         ' -- Opp not found
    56.         MsgBox "Nothing was found for :  " & Sheets("DSA").Range("B2").Value _
    57.         & vbCrLf & "Please check your input or presume it doesnt exist.", _
    58.         vbCritical + vbOKOnly, _
    59.         "Opportunity Not Found"
    60.            
    61.     End If
    62. End Sub

    Ok so the code searches and if it finds a match it returns the data and all the offsets as i want them, if the users clicks yes it will wipe the data for them, i have place this part as comments for now whilst i get it fully operational.
    When the user clicks no i want it to display the msgbox i incorporated after the elseif code but im not sure if that is correct.
    and one last thing the line
    vb Code:
    1. & vbCrLf & "The Current Appointment Time is : " & vOppLocation.Offset(0, -3) _
    needs to be formatted as time
    and the line
    vb Code:
    1. & vbCrLf & "The Customers Telephone Number is : " & vOppLocation.Offset(0, 2) _
    needs to be formatted as an 11 digit number so it has the leading zero.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    for the last 2 use the format function
    like
    vb Code:
    1. & vbCrLf & "The Current Appointment Time is : " & format(vOppLocation.Offset(0, -3), "hh:mm:ss") _
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Thank you pete, any help on if the user clicks no?
    At the moment it just runs the procedure as if the user said yes ?

  13. #13
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    you do not have a place where the user can click no - as far as i can see all you have are msgboxes set as vbokonly

    and the else if yes=no is a complete (well i said i would be good today) mess

    you need to change the message boxes into functions and work with the returned value...

    simply wrap the message in () and set the button type to at least vbyesno

    the use like this

    ans=msgbox("hello mum",vbyesno) fill tis in properly!
    if ans=vbyes then
    else
    end if

    is that any clearer for you?

    here to help

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    As you can see this message box contains the vbyesno function.

    vb Code:
    1. ' -- Opp found, Question User
    2.         MsgBox "You Have Searched for : " & vOppLocation & " and the following data was returned" _
    3.         & vbCrLf & "Customers Name : " & vOppLocation.Offset(0, -2) & " " & vOppLocation.Offset(0, -1) _
    4.         & vbCrLf & "The Current Appointment Date is : " & vOppLocation.Offset(0, -4) _
    5.         & vbCrLf & "The Current Appointment Time is : " & vOppLocation.Offset(0, -3) _
    6.         & vbCrLf & "The Customers D.O.B is : " & vOppLocation.Offset(0, 1) _
    7.         & vbCrLf & "The Customers Telephone Number is : " & vOppLocation.Offset(0, 2) _
    8.         & vbCrLf & "The Customers Eligibility is : " & vOppLocation.Offset(0, 3) _
    9.         & vbCrLf & "Support Needed by the Customer : " & vOppLocation.Offset(0, 4) _
    10.         & vbCrLf & "The Current Contract Holder is : " & vOppLocation.Offset(0, 5) _
    11.         & vbCrLf & "The Original Referrer is : " & vOppLocation.Offset(0, 6), _
    12.         vbInformation + vbYesNo, _
    13.         "Found"

    It just does nothing if the user clicks no..

    Thank you pete for your time format code

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    It just does nothing if the user clicks no..
    you do not use the return from the messagebox to know what the user clicked

    see incidentals post
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Ok i have use the functions and everything seems to run fine, i get the nothing removed when i click no and i get the nothing found if a match isnt found, my only issue now is when i do the initial command button click and it find a match, just a blank yesno box appears with none of the description typed up, what have i possibly missed as it doesnt give any error?

    vb Code:
    1. Dim msg1 As String
    2.     Dim response1
    3.    
    4.         ' -- Opp found, Question User
    5.         response1 = MsgBox(msg1, vbInformation + vbYesNo)
    6.         msg1 = "You Have Searched for : " & vOppLocation & " and the following data was returned" _
    7.             & vbCrLf & vbCrLf & "Customers Name : " & vbTab & vOppLocation.Offset(0, -2) & " " & vOppLocation.Offset(0, -1) _
    8.             & vbCrLf & vbCrLf & "The Current Appointment Date is : " & vbTab & vOppLocation.Offset(0, -4) _
    9.             & vbCrLf & vbCrLf & "The Current Appointment Time is : " & vbTab & Format(vOppLocation.Offset(0, -3), "h:mm") _
    10.             & vbCrLf & vbCrLf & "The Customers D.O.B is : " & vbTab & vOppLocation.Offset(0, 1) _
    11.             & vbCrLf & vbCrLf & "The Customers Telephone Number is : " & vbTab & Format(vOppLocation.Offset(0, 2), "00000000000") _
    12.             & vbCrLf & vbCrLf & "The Customers Eligibility is : " & vbTab & vOppLocation.Offset(0, 3) _
    13.             & vbCrLf & vbCrLf & "Support Needed by the Customer : " & vbTab & vOppLocation.Offset(0, 4) _
    14.             & vbCrLf & vbCrLf & "The Current Contract Holder is : " & vbTab & vOppLocation.Offset(0, 5) _
    15.             & vbCrLf & vbCrLf & "The Original Referrer is : " & vbTab & vOppLocation.Offset(0, 6)
    16.  
    17.     If response1 = vbYes Then
    18.             'vOppLocation.Offset(0, -2).ClearContents
    19.             'vOppLocation.Offset(0, -1).ClearContents
    20.             'vOppLocation.Offset(0, 0).ClearContents
    21.             'vOppLocation.Offset(0, 1).ClearContents
    22.             'vOppLocation.Offset(0, 2).ClearContents
    23.             'vOppLocation.Offset(0, 3).ClearContents
    24.             'vOppLocation.Offset(0, 4).ClearContents
    25.             'vOppLocation.Offset(0, 5).ClearContents
    26.             'vOppLocation.Offset(0, 6).ClearContents
    27.            
    28.             MsgBox "All appointments and details matching " & Sheets("DSA").Range("B2").Value & " have been removed", vbInformation + vbOKOnly, "Remove Opportunity"
    29.     ElseIf response1 = vbNo Then
    30.             MsgBox "No Details were removed"
    31.     Exit Sub
    32.     End If
    33.    
    34.         Sheets("DSA").Range("B2") = ""
    35.         Sheets("DSA").Range("B2").Select
    36.    
    37.     Else
    38.    
    39.         ' -- Opp not found
    40.             MsgBox "Nothing has been found for : " & Sheets("DSA").Range("B2").Value _
    41.             & vbCr & "Please check your entry or presume it does not exist", _
    42.             vbExclamation + vbOKOnly, _
    43.             "Referral Not Found"
    44.            
    45.     End If
    46. End Sub

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Finding a vlaue and returning to MSGBOX, i have some code already.

    Dont worry guys i have sorted the issue i misplaced a line of code, this one

    vb Code:
    1. response1 = MsgBox(msg1, vbInformation + vbYesNo)

    The entire thing works as it should now, thank you for all your help thread resolved.

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