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
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
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?
Re: Searching for a specific value
if posible attach the file, i hav only excel 2003.
Re: Searching for a specific value
you can try
this is different from using cel.value
Re: Searching for a specific value
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.
Re: Searching for a specific value
Thanks Westconn and Seenu.
I have a much simplified version running now.
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 Give it a try and if you are still stuck then let me know and I will post the code :)
HTH
Sid