How to Highlight rows containing particular keyword in VBA? - excel

I'm working with MS Excel 2010 VBA.i want to write a code for highlighting the rows which contains the keyword canada i have written the following code,
Cells.Find(What:="Canada", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
i want to use the code in a loop to highlight all the rows which contains the keyword "canada".How can i do it using VBA?

Example:
Sub Highlight()
Dim vCell As Range
'Loop through every used cell in the active worksheet
For Each vCell In ActiveSheet.UsedRange
'If the cells value contains "Canada" change its font to red
If InStr(vCell.Value, "Canada") Then
vCell.Font.Color = -16776961
End If
Next
End Sub
Here is a link to microsofts office site explaining how loops work:
http://office.microsoft.com/en-us/training/get-in-the-loop-with-excel-macros-RZ001150634.aspx?section=11

Related

Opening a hyperlink

I have no experience of VBA, and i'm trying to understand if i can get it to run something for me in Excel.
I’m trying to get the user to input a value, click find, this value will be present in the column D. Once the value has been found it must move the sheet to that cell, then scroll to the right to open a hyperlink associated with the previous cell.
I can get the code to do the above based on an exact input but can’t get it to work on the user input.
The simple code to do it against a specific value and open the file is below:
`Sub Macro7()
'
' Macro7 Macro
'
'
Cells.Find(What:="BMS1244", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
Range("N1468").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub`
How do I replace the specific value with user input?
thanks
Does this work for you?
Sub Macro1()
'
' Macro1 Macro
'
'
Dim found As Range
Dim LinkCell As Range
Dim what_to_find As String
what_to_find = InputBox("Which value would you like to find?")
Set found = Cells.Find(What:=what_to_find, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'change the 5 to however many columns to the right you want to jump
Set LinkCell = found.Offset(0, 5)
LinkCell.Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub

Using the value of one cell to locate another on a different sheet

I'm trying to get a VBA code together that can take the date from cell B5 on one sheet and locate the exact match from a column on a different sheet, then select that as the active cell. any ideas?
Heres what i've tried;
Sub find()
Sheets("Details").Select
Range("B5").Select
rngY = ActiveCell.Value
Sheets("Calcs").Select
Columns("C:C").Select
Selection.find(What:=rngY, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
End Sub
Something like this should work for you:
Public Sub FindInSheet2()
Dim r As Range, lookupVal As Variant
lookupVal = ThisWorkbook.Worksheets("Details").Range("B5").Value
Set r = ThisWorkbook.Worksheets("Calcs").Range("C:C").Find(What:=lookupVal, LookAt:=xlWhole, LookIn:=xlValues)
If Not r Is Nothing Then
ThisWorkbook.Worksheets("Calcs").Activate
r.Select
Else
MsgBox "Lookup not found", vbOKOnly
End If
End Sub
The main difference between this code and yours is that I haven't relied on ActiveCell or Selection. It's better to be specific about which ranges you are working with. For more info, see How to avoid using Select in Excel VBA

Excel VBA Looping Fill for certain cell values

I'm doing an assignment in VBA. I recorded a macro that found cells with the search criteria 'central', then I colored it blue-green and got the following Macro:
Sub Color()
' Color Macro
' Color a region
'
' Keyboard Shortcut: Ctrl+m
'
Cells.Find(What:="central", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6723891
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End sub
There are 23 occurances of the word 'central' so I thought I could add for k=1 to 23 above the line that starts with cells.find(what...), then add Next K above end with but when I try I get the error
next without for
Bruce Wayne already told you why you got that error
but if you want your macro to find and process all occurrences of "central" in your currently active sheet no matter how many of them, then you can wrap Find() method inside a loop that goes on until all wanted occurrences are found, like follows (explanation in comments):
Dim f As Range
Dim firstAddress As String
With ActiveSheet.UsedRange 'reference currently active sheet used range
Set f = .Find(What:="central", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'search referenced range for first occurrence of "central"
If Not f Is Nothing Then ' if found...
firstAddress = f.Address ' store first occurrence cell
Do
With f.Interior 'reference found cell "Interior" property
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6723891
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Set f = .FindNext(f) ' search for the next "central" occurrence
Loop While f.Address <> firstAddress ' loop till you wrap back to initial occurrence
End If
End With

Range.find method to find cells with specific colorindex

I want to find cells with the same colorindex with a range
So I recorded a macro to find cells with a color
and applied to its code like this.
But it doesn't work.
How can I find cells with a specific colorindex?
For Each r In rngC
lcolorID = r.Offset(, 1).Interior.ColorIndex
With wsD.UsedRange
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = lcolorID
End With
Set c = .Find(What:="", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:= xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, _
SearchFormat:=True)
If Not c Is Nothing Then
Debug.Print c.Address
End If
End With
Next r
If Cell.Interior.ColorIndex = yourColorIndex Then 'using your color variable
'your actions here
End If
Place the above if condition in a for loop where you go through all the rows and it should work! And replace "Cell" by the specific cell you are currently handling in the project using the loop you are using to go through your sheet. If your sheet name is "Sheet" then use:
Sheets("Sheet").Cell(rowindex, columnindex).Interior.ColorIndex
Where rowindex and columnindex are the indexes your are looping through.
Try Application.FindFormat.Clear before With Application.FindFormat.Interior.
You may have some formatting already set which may prevent you in matching desired cells.

vba excel 2003 copy range find and paste the selected range

I am trying to write a macro in excel sheet. In this macro, do I take a copy of the cell number (B04) and I search on the worksheet (3), but the problem that I am having is when I want to change the content of the cell macro is also searching for new content
Range("D6").Select
Selection.Copy
Sheets("3").Select
Cells.Find(What:="Yasser Arafat Ateya ELsayed EL", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("B6").Select
Application.CutCopyMode = False
Selection.ShowDetail = True
End Sub
Going with the flow of your code, and with a bit of explanation to help you, amend your code to something like this. Also learn about explicitly referencing separate worksheets in your code rather than relying on ActiveSheet.
Sub findSomeText()
'the find method returns a Range so define a variable to catch this
Dim fndrng As Range
'to search for different strings use a String variable
Dim srchStr As String
'get the srchStr (in this case from D6 in the ActiveSheet)
srchStr = ActiveSheet.Range("D6").Value
'use Sheets("3") - you don't need to select it
'note that this means a Sheet with a (Tab)NAME of "3", not necessarily the third sheet
With Sheets("3")
'explicitly set the ActiveCell to Find from
Range("A1").Activate
'apply the Find method, note use LookIn:=xlValues; use of srchStr and .Cells
Set fndrng = .Cells.Find(What:=srchStr, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
'check if a match has been found
If Not fndrng Is Nothing Then
'your code if srchStr has been found goes here e.g. found value into cell B6
Range("B6").Value = fndrng.Value
' perhaps use MsgBox "Found" to test?
Else
'your code if srchStr has NOT been found goes here
MsgBox "Not Found"
End If
End Sub

Resources