+ Reply to Thread
Results 1 to 14 of 14

Code sometimes runs and sometimes doesn't

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Code sometimes runs and sometimes doesn't

    Hi all,

    I have this code in my application, its objective to find a part of code in a specific column

    This code sometimes when i open my app. runs and sometimes doesn't

    I don't know what the defect.

    Sub Done()                                 'Default Option
    'SpeedOn
    On Error Resume Next
    Dim Dishet As Worksheet
    Dim v As Integer, LastRow As Integer
    Dim M As String
    Dim b, F
    List_done.Clear
    MM = textIC.Value         'target number
    Set Dishet = Sheet1
    With Dishet
    .Activate
    LastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
    Set b = .Range("H4:H" & LastRow).Find(MM)           'Target column
    If Not b Is Nothing Then
    F = b.Address
    Do
    If Application.WorksheetFunction.Search(M, b, 1) = 1 And _
      b.Text = b.Offset(0, -6).Text & "/" & MM Then
    
    List_done.ColumnCount = 10
    List_done.AddItem b.Value
    For i = 1 To 10
        CC = Choose(i, -5, -4, -1, 1, 3, 4, 5, 8, 9)
        List_done.List(v, i) = b.Offset(0, CC).Text
    Next
    List_done.List(v, 20) = b.Address
    'List_done.ColumnHeads = True
    List_done.ColumnWidths = "80;62;153;75;133;55;58;55;55;0"
    v = v + 1
    End If
    Set b = .Range("H4:H" & LastRow).FindNext(b)
    Loop While Not b Is Nothing And b.Address <> F
    Else
    MsgBox "There are no results for your search criteria, please try again", vbExclamation, "Error"
    textIC.Text = ""
    textIC.SetFocus
    End If
    End With
    'SpeedOff
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    How are you calling the code?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    It's a button "Search" depending on 2 optionbuttons, if optionbutton1 = true then call done

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  5. #5
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    Sorry for my late reply

    I attached you a file

    try it
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    Sorry, I'm a little confused.

    When do you want this code to run?

    In your first post you seemed to be saying you wanted it to run when the workbook opened, then you say it's run from a button and now I've looked at the workbook it appears to be run from a button on a userform, if a certain option button is selected.

    Whenever I try the button with the relevant option button selected then the code you posted runs.

    So, I'm not quite sure what problem you are having, am I missing something?

  7. #7
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    ok

    1. this userform is one of many,
    2. This userform works from a "Welcome userform" contains many of userforms like that
    3. when you opend that userform from welcome, and put any number (1-999) in that yellow field, it searched the entered number in database, and if found, it gets all items explained below in userform
    4. If the code doesn't find the number, there's a msg error "There are no results for your search criteria, please try again"
    5. I know that the code will runs with you, he almost did that with me, but sometimes the code jump to this msg although the searched number exists

    I hope the problem comes clear

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    What exactly are you searching for in column H?

    The values in that column aren't 1-999, they are values like 14/3/2013/160, 11/2/2013/93 etc.

  9. #9
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    Yes, it's a code and i searched for a bit

    I searched for the last area in code

    ex. the code is 14/3/2013/160, i searched for (160)

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    The only thing you specify in the Find is what you want to search for and where, you don't say to look for a partial match, what direction to do the search in, whethere to search formulas etc.

    If you don't specify things like that then Find will use the settings for the last find that was carried out on the sheet.

    Try specifying some of those arguments in the Find method.

  11. #11
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    also the code works well, but I don't know why the code sometimes jumps to the (else) part of code ?

  12. #12
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    it seems difficult, Norie

    but I have an option to specify the search, and no use, the same problem done

    I have two optionbuttons, if I click (search by year), then i can search by (2013) and (160), but as i said the same problem

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code sometimes runs and sometimes doesn't

    It jumps to the Else part becuase it isn't finding what you are looking for.

    That could be because you've missed out all but one of the arguments of Find.

  14. #14
    Forum Contributor
    Join Date
    09-01-2011
    Location
    England
    MS-Off Ver
    Excel 365
    Posts
    126

    Re: Code sometimes runs and sometimes doesn't

    yea I know, but still why

    the same number of search, find it sometimes and sometimes doesn't

    I meant, What makes the code jump all lines although these lines already have values ?

+ 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. [SOLVED] Event macro runs in wrong workbook/doesn't work properly
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-30-2012, 11:28 AM
  2. [SOLVED] Macro runs perfectly, but dialog box doesn't close automatically
    By Bunnyla in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2012, 01:12 PM
  3. Script works in Debugger but doesn't when Macro runs
    By sqeekypotato in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-02-2009, 11:31 AM
  4. Code help appreciated:Code runs fine
    By peter.thompson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2006, 10:03 PM
  5. [SOLVED] Code runs without error but doesn't print output
    By Robert Hargreaves in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2005, 02:05 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