Results 1 to 5 of 5

Thread: [RESOLVED] How to check if a subspring is within a string?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    110

    Resolved [RESOLVED] How to check if a subspring is within a string?

    Hi, I need know if the caption of a checkbox is within the name of a sheet.

    I tried this, but it's not working, because "Else" aways happens, to all sheets(k), even in not having the text of the checkbox in the name of the sheet.

    Code:
    If CheckBox2 = True Then
    j=0
       For k = 1 To Sheets.Count
            If InStr(1, Sheets(k).Name, checkbox2.caption, 1) = 0 And InStr(1, Sheets(k).Name, checkbox1.text, 1) = Null Then
            Else
                Sheets_I_Need(j) = k
                j = j + 1
            End If
        Next k
    End If
    What could I do?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    110

    Re: How to check if a subspring is within a string?

    I tried just
    Code:
    InStr(Sheets(k).Name, checkbox2.caption) = 0 Then
    and I guess it's ok now,

    though it doesn't check the case "Null", it already helps a little
    Last edited by marlonsaveri; Aug 4th, 2012 at 08:04 PM. Reason: *though, not thoug

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

    Re: How to check if a subspring is within a string?

    you can not check for null using AND

    you must check for null first, then the other criteria if not null
    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
    Lively Member
    Join Date
    Jan 2011
    Posts
    110

    Re: How to check if a subspring is within a string?

    Oh, right, I should use "Or", not "And" in that case.

    Thanks

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

    Re: [RESOLVED] How to check if a subspring is within a string?

    I should use "Or", not "And" in that case.
    same applies, as it always evaluates both conditions, in cases it is null, evaluating the other condition will error
    must be done like

    Code:
    if not isnull(something) then
       if something = x then somethingelse = y
    end if
    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

Tags for this Thread

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