Results 1 to 8 of 8

Thread: Searching for a specific value

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    36

    Searching for a specific value

    Hello..

    I am trying to search for, find, and delete certain values. I am encountering a problem when looking for the value 3D.1 because there are other cells in the range containing the value 3D.11, 3D.12, etc. The macro is finding these when it should be finding 3D.1

    This is especially a problem when 3D.1 has already been deleted because it keeps deleting values that aren't 3D.1 and all the data is now inaccurate.

    Is there a way to specify in the range.find method (what I am using) that I want to find 3D.1 with NO CHARACTERS after it?

    Thanks.

    (if there's a better way of doing any of this, let me know. For example... my if * is nothing then /r else way of finding if something is valid probably isn't the best way to get what I want, but it's the only way I know. Thanks for any help you have).

    Code works, just treats 3D.11, 3D.12, etc. as 3D.1

    lname is the lesson name (3D.1, 3D.2, etc.)
    sname is the student name
    gwnamerow is an integer containing the value of the row we're looking at
    lspot is the column containing the lesson (in the specified row)
    Holder is a dimension (not specified. Is it important to specify all dimensions when declaring them? does this make the program run faster? what would I specify it as? an array?)

    Basically, it deletes the lesson, and moves all the other lessons over one, replacing the empty space

    Code:
    If .Range(.Cells(gwnamerow, 3), .Cells(gwnamerow, 32)).Find(lname) Is Nothing Then
    Else
        lspot = .Range(.Cells(gwnamerow, 3), .Cells(gwnamerow, 32)).Find(lname).Column
        holder = .Range(.Cells(gwnamerow, lspot + 1), .Cells(gwnamerow, 31))
        .Range(.Cells(gwnamerow, lspot), .Cells(gwnamerow, 30)) = holder
        If .Cells(gwnamerow, 31).Value <> "" Then .Cells(gwnamerow, 31).Value = ""
        'mark what we found
        Selection.Offset(6 + gwrow, 8) = Left(sname, InStr(sname, ",") - 1) & ", " & lname
        gwrow = gwrow + 1
    End If

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Searching for a specific value

    try like this
    Code:
    Dim rng As Range
    Set rng = Range("a1:a10")
    For Each cel In rng
        If cel.Value = "3D.1" Then
            cel.Value = ""
        End If
    Next
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    36

    Re: Searching for a specific value

    Thanks for your attempted help, but it sees a cell with a value of 3D.10 as having a value of "3D.1"

    I got around it, but perhaps not so elegantly. Since (luckily) I only have to worry about values 3D.10-3D.15 and 3D.1 is always in front of them (and the range.find method goes left to right),

    I got the value

    Code:
    If .Range(.Cells(gwnamerow, 3), .Cells(gwnamerow, 32)).Find(lname) Is Nothing Then
    Else
    dcase = False
    If lname = "3D.1" Then
    'This is to fix the bug of the computer thinking that 3D.1 is also 3D.10, 3D.11, Etc.
    dlname = .Range(.Cells(gwnamerow, 3), .Cells(gwnamerow, 32)).Find(lname).Value
    If dlname = "3D.10" Or _
    dlname = "3D.11" Or _
    dlname = "3D.12" Or _
    dlname = "3D.13" Or _
    dlname = "3D.14" Or _
    dlname = "3D.15" Then dcase = True
    'Lesson 3D.1 is already removed (The .find method searches from left to right)
    'disqualify the 3D.1 from deleting anything else
    End If
    If dcase = flase Then
    lspot = .Range(.Cells(gwnamerow, 3), .Cells(gwnamerow, 32)).Find(lname).Column
    holder = .Range(.Cells(gwnamerow, lspot + 1), .Cells(gwnamerow, 31))
    .Range(.Cells(gwnamerow, lspot), .Cells(gwnamerow, 30)) = holder
    If .Cells(gwnamerow, 31).Value <> "" Then .Cells(gwnamerow, 31).Value = ""
    mark what we found
    Selection.Offset(7 + gwrow, 8) = sname & ", " & lname & ", IN RTF"
    gwrow = gwrow + 1
    End If
    End If
    Anyone got a better idea?

  4. #4
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Searching for a specific value

    if posible attach the file, i hav only excel 2003.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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

    Re: Searching for a specific value

    you can try
    vb Code:
    1. if cel.text = "3D.1"
    this is different from using cel.value
    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

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Searching for a specific value

    Code:
    Cells(row,column)
    will not confuse "CD.10" with "CD.1" I don't believe.

    Try this:

    Code:
    Sub FindCD1()
        If Cells(1, 1) = "cd.1" Then
            'delete
        Else
            'don't delete
        End If
    End Sub
    and put "CD.10" in A10. It won't think it's found "CD.1" in this case.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    36

    Re: Searching for a specific value

    Thanks Westconn and Seenu.

    I have a much simplified version running now.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Searching for a specific value

    EricBeckw1th

    There is nothing wrong with your code All you have to do is make a minor change. I would recommed this link

    Topic: .Find and .FindNext In Excel VBA

    Link: http://siddharthrout.wordpress.com/2...-in-excel-vba/

    The trick is to watch out for
    Code:
    LookAt:=xlWhole
    Give it a try and if you are still stuck then let me know and I will post the code

    HTH

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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