Having little trouble here in which I'm looping through a big document and highlighting strings. Where I'm having trouble is two fold, eg.
Search Criteria "aaa" in cell "Baaa", this would get highlighted as a hit, which on some level makes sense, but I was hoping only to return highlight if exact match in string and length. Also I'm curious if there is an easy way to compress the following:
Sub Sample()
Dim MyAr(1 To 1092) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "R833"
MyAr(2) = "R853"
MyAr(3) = "R873"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(23).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
aCell.Interior.ColorIndex = 3
Do
Set aCell = .Columns(23).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
aCell.Interior.ColorIndex = 3
Else
Exit Do
End If
Loop
End If
Next
End With
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(24).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
aCell.Interior.ColorIndex = 3
Do
Set aCell = .Columns(24).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
aCell.Interior.ColorIndex = 3
Else
Exit Do
End If
Loop
End If
Next
End With
You have this LookAt:=xlPart in your FIND statement.
Change this to LookAt:=xlWhole to only search for exact matches.
Note: This means the whole cell must match, not just that one word amongst many in the cell.
To find an exact match on a word amongst many use xlPart but add a space at the start and end of your search term - search for " aaa " rather than "aaa".
Edit: It looks like your code could be shortened, unfortunately I haven't got the time to look at it at the moment.
Building on #Darren Bartrup-Cook's comments, you can shorten it thus, though I doubt you need to search the full column?
Sub Sample()
Dim MyAr(1 To 1092) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "R833"
MyAr(2) = "R853"
MyAr(3) = "R873"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Range("W:X").Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
Do
aCell.Interior.ColorIndex = 3
Set aCell = .Range("W:X").FindNext(After:=aCell)
Loop While aCell.Address <> bCell.Address
End If
Next
End With
End Sub
Related
I need to find a string that will be always on the Row 3 but any column in a specific sheet, after the code find it the application GoTo should take the user to this column. However, I am having trouble with my code, maybe someone knows what I am doing wrong?
Sub Pivot1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "Nr. 1"
Application.FindFormat.Clear
With Sheets("Analysis").Cells(3, Columns.Count).End(xlToLeft).Column
Set cl = .Find(What:=SearchString, After:=Range("A:FA"), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then Application.Goto cl
End With
End Sub
I need your help,
Is it possible to search an Excel spreadsheet based on 2 given values, as opposed to just 1? similar to that of an "AND" operator?
Here's what I have so far, but I guess if your looking for a needle in a hay stack:
Str = "apples" AND "oranges"
With Sheets(xSheet)
Set foundCell = .Cells.Find(What:=Str, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
The code below returns the wanted result:
Sub TestMe()
Dim str As String: str = "apples*oranges"
Dim foundCell As Range
Dim options As Variant
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
Dim myVar As Variant
For Each myVar In options
With Worksheets(1)
Set foundCell = .Cells.Find(What:=myVar, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then Exit For
Next myVar
If Not foundCell Is Nothing Then
Debug.Print foundCell.Address
End If
End Sub
We have two different strings that need to be seaerched for: "apples*oranges" and "oranges*apples". The split and the reverse is done pretty primitively:
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
And then, using a For Each Loop with an early Exit For, the .Find() searches for the two strings.
Here is a simple loop alternative:
Sub ApplesAndOranges()
Dim r As Range, fruits As Range
Set fruits = Nothing
For Each r In ActiveSheet.UsedRange
v = r.Value
If v <> "" Then
If InStr(v, "apples") <> 0 And InStr(v, "oranges") <> 0 Then
If fruits Is Nothing Then
Set fruits = r
Else
Set fruits = Union(fruits, r)
End If
End If
End If
Next
MsgBox fruits.Address(0, 0)
End Sub
From the example, you see it returns all cells that contain both sub-strings.
I have each set of strings which required to search in column 2, if it finds the string, Offset(0, -1) and place given text there, and repeat the process for each set of strings and for each set of text. i tried below query but getting 91 error. please some one help me out.
Sub Sample()
Dim MyAr(1 To 3) As String
Dim MyAr1(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'aCell.Interior.ColorIndex = 3
aCell.Offset(0, -1).Value = "g\"
Do
Set aCell = .Columns(2).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'aCell.Interior.ColorIndex = 3
Else
Exit Do
End If
Loop
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set dCell = cCell
cCell.Offset(0, -1).Value = "c\"
Do
Set cCell = .Columns(2).FindNext(After:=cCell)
If Not cCell Is Nothing Then
If cCell.Address = dCell.Address Then Exit Do
Else
Exit Do
End If
Loop
End If
Next
End With
End Sub
Sample image
It seems to be bellow.
Sub test()
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'aCell.Interior.ColorIndex = 3
Do
aCell.Offset(0, -1).Value = "g\"
Set aCell = .Columns(2).FindNext(After:=aCell)
Loop Until aCell.Address = bCell.Address Or aCell Is Nothing
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set dCell = cCell
Do
cCell.Offset(0, -1).Value = "c\"
Set cCell = .Columns(2).FindNext(After:=cCell)
Loop Until aCell.Address = bCell.Address Or aCell Is Nothing
End If
Next
End With
End Sub
I can't get properly what you want, but the following reduced code seems to work....
Sub Sample()
Dim MyAr(1 To 3) As String
Dim MyAr1(1 To 2) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -1).Value = "g\"
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cCell Is Nothing Then
cCell.Offset(0, -1).Value = "c\"
End If
Next
End With
End Sub
Goal:
Search Sheet1 for keywords in column 18 (keywords:case,etc.)
Once keyword is found, offset (0,-11)
enter the given value C1008 in respective cell.
and repeat the same till it completes full column (about 1500 rows)
I am not able to perform 4th step.
And I need multiple keywords to search and perform the same steps.
Program:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Columns(18).Find(What:="case", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
End With
End Sub
If you want just to repeat your code use a loop statement like this:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
DIM isTimetoExit As Boolean
Dim currentRow As Long 'Counter of Rows
Dim KeyWord as String
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
isTimetoExit = False
currentRow = 1 'If you start from row 1
KeyWord = "case"
DO
'
Set aCell = .Columns(18).Find(What:=KeyWord, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
'
' Whit a condition go out of loop
isTimetoExit = NOT (Trim(.Cells(currentRow, 1).Value & "") = "")
If (isTimetoExit) THEN EXIT DO
'Go for next row
currentRow = currentRow + 1
' And set the keyword to your new value;
KeyWord = "next case" 'But I don't know where are those KeyWords stroes !?
LOOP
End With
End Sub
I am using this loop to look for values. .Find works but findNext does not, ommiting many values. Here I drop my code, do you have any advice? thank you very much!!
For Each ws In SourceWb.Worksheets
If IsNumeric(Left(ws.Name, 3)) Then
Set gCell = ws.Columns(6).Find(what:=numdoc, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
'>Having trouble here> **
Set gCell = ws.Columns(6).FindNext(after:=gCell)
'**
Loop Until gCell.Address = oldaddress
End If
End If
Next ws
This was the best I could derive from your clues:
Option Explicit
Sub Test()
Dim WS As Worksheet
Dim SourceWB As Workbook
Dim numdoc As Long
Dim gCell As Range
Dim firstAddress As String
Dim oldaddress As String
Dim finalcell As String
Dim repetidos As Long
Set SourceWB = ThisWorkbook 'added for clarity and safety
numdoc = 456
For Each WS In SourceWB.Worksheets
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = WS.Columns(6).Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = WS.Columns(6).FindNext(after:=gCell)
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
Loop Until gCell.Address = oldaddress
End If
End If
Next WS
End Sub
Not sure if it answers the question but, it does demonstrate indentation.
There's probably room for a With...End With in there but I'm too tired to look for it.
This seems to work at this point.
For Each WS In SourceWB.Worksheets
With ws.Range("F:F")
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = .Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = .FindNext(after:=gCell)
Do
'merged cells code here not displayed
Loop While Not gCell Is Nothing And gCell.Address <> firstAddress
End If
End If
end with
Next WS