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
Related
I have a working code to find a specific string in a column of a specific sheet, offset and clear the contents of a specific cell. However it only clears the first occurrence of this search and I would like to have the code work on all occurrences. Can someone help me to wrap a Loop or a FindNext around this code because I wasn't able to. Please see here below the code I already have. Thnx
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
On Error Resume Next
Worksheets(2).Columns("A:A").Select
Set Action6 = Selection.Find(What:=SearchValue6, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Action6 Is Nothing Then
'MsgBox "No clearings made in " & ActiveWorkbook.Name
Else
Action6.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.ClearContents
End If
Please, try using the next updated code and send some feedback:
Sub FindMultipleTimes()
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
Dim ws As Worksheet: Set ws = Worksheets(2)
Dim firstAddress As String
Set Action6 = ws.Columns("A:A").Find(What:=SearchValue6, After:=ws.Range("A1"), LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Action6 Is Nothing Then
firstAddress = Action6.address
Do
Action6.Offset(0, 1).ClearContents
Set Action6 = ws.Columns("A:A").FindNext(Action6) 'find the next occurrence
Loop While Action6.address <> firstAddress
Else
MsgBox SearchValue6 & " could not be found in column ""A:A"" of sheet " & ws.name
End If
End Sub
I only adapted your code, but do you want letting the workbook necessary to extract SearchValue6 value, open?
I'm trying to paste the enire row of information to the next available row but I keep getting errors about not having the Rows(lastrow +1, 1).EntireRow.Paste written correctly. Please let me know how I can perform that action correctly.
Private Sub CommandButton1_Click()
Dim myValue As String
myEmp = InputBox("Search for an employee by last name")
Range("B3").Value = myEmp
With Sheet7
Range("B:B").Select
Set Row = Selection.Find(What:=myEmp, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Row.EntireRow.Copy
End With
Worksheets("Employee Reports").Activate
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Rows(lastrow + 1, 1).EntireRow.Paste
End Sub
Private Sub Workbook_Open()
Application.EnableEvents = False
Worksheets("Sheet3").Range("A4:A20").Value = ""
End Sub
Private Sub CommandButton1_Click()
Dim myValue As String
myEmp = InputBox("Search for an employee by last name")
ActiveSheet.Range("B3").Value = myEmp
Dim lastrow As Long
lastrow = Worksheets("Employee Reports").Range("A65536").End(xlUp).Row
With Sheet7
Dim rw As Range
Set rw = .Range("B:B").Find(What:=myEmp, After:=.Range("B1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rw Is Nothing Then
rw.EntireRow.Copy Worksheets("Employee Reports").Cells(lastrow + 1, 1)
Else
MsgBox myEmp & " Not Found in Range"
End If
End With
End Sub
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
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
1
2
3
4
.
.
So I have a sequence of numbers running from 1-20. I have the number "1" on top selected and I would like to search the entire column and find the number "9". The code works when I don't name the range "rng"; it finds the number and selects. But the code stops working when I name the range of number. What's wrong with the range function? could it be that if I define Dim rng as Range that when I later define the "Set rng=" I cannot have the ".Select" or ".Copy" extension on the end?
Sub macro2()
Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
rng.Find(10).Select
End Sub
Also, If I want to sum the entire column from 1-20, on the last cell below the number "20" should I use the following code? because the application object doesn't seem to do it. Thank you!
rng.End(xlDown).Offset(1, 0).Select
Application.WorksheetFunction.Sum (rng.Value)
To look for 10 in the active column you could try this (which ends up selecting the first 10 - although Select in vba isn't normally needed other than taken the user to location at code end)
test that the found range exists (ie you can find 10 before proceeding)
you should also use xlWhole to avoid matching 100 if the current default for [lookAt] is xlPart
using search [After] as Cells(1, ActiveCell.Column , and [Search Direction] as xlNext finds the first value looking down.
code
Sub QuickFind()
Dim rng1 As Range
Set rng1 = ActiveCell.EntireColumn.Find(10, Cells(1, ActiveCell.Column), xlFormulas, xlWhole, , xlNext)
If Not rng1 Is Nothing Then
Application.Goto rng1
Else
MsgBox "10 not found"
End If
End Sub
Part 2
Sub Other()
Dim rng1 As Range
Set rng1 = Range(Cells(1, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp))
rng1.Cells(rng1.Cells.Count).Offset(1, 0) = Application.WorksheetFunction.Sum(rng1.Value)
End Sub
Try this, I hope this will help u to find the specific row no as well as column name too. In code you can use
strRw = FindColumn(Sheet name, "Value which need to be found", True, "Cell Name",Row number)
sourceCOL = colname(FindColumn(Shee Name, "Value which need to be found", False, , 4))
Below is main function of find
Public Function FindColumn(colnocountWS As Worksheet, srcstr As String, Optional rowflag As Boolean, Optional bycol As String, Optional strw As Integer, Optional stcol As Integer) As Integer
Dim srcrng As Range 'range of search text
Dim srcAddr As String 'address of search text
Dim stcolnm As String
colnocountWS.Activate
If stcol <> 0 Then stcolnm = colname(stcol)
If stcol = 0 Then stcolnm = "A"
If strw = 0 Then strw = 1
colnocountWS.Range(stcolnm & strw).Select
If ActiveSheet.Range(stcolnm & strw) = srcstr Then
ActiveSheet.Range(stcolnm & strw).Select
FindColumn = 1
Else
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
'ByPart
If srcrng Is Nothing Then
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
End If
If srcrng Is Nothing Then
FindColumn = 0
Exit Function
Else
srcAddr = srcrng.Address
colnocountWS.Range(srcAddr).Select
FindColumn = ActiveCell.Column
If rowflag = True Then FindColumn = ActiveCell.Row
End If
End If
End Function
'this function find column name
Public Function colname(iFinalCol1 As Integer) As String
Dim colnm As String
On Error GoTo gg
If Mid(Cells(1, iFinalCol1).Address, 3, 1) = "$" Then
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 1)
Else
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 2)
End If
gg: colname = colnm
End Function