-
Apr 1st, 2012, 03:54 PM
#1
Thread Starter
Member
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
-
Apr 1st, 2012, 09:50 PM
#2
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
-
Apr 1st, 2012, 10:35 PM
#3
Thread Starter
Member
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?
-
Apr 1st, 2012, 10:40 PM
#4
Re: Searching for a specific value
if posible attach the file, i hav only excel 2003.
-
Apr 2nd, 2012, 05:51 AM
#5
Re: Searching for a specific value
you can try
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
-
Apr 2nd, 2012, 08:10 AM
#6
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.
-
Apr 7th, 2012, 10:10 AM
#7
Thread Starter
Member
Re: Searching for a specific value
Thanks Westconn and Seenu.
I have a much simplified version running now.
-
Apr 7th, 2012, 10:26 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|