I am trying to build a macro that will search a specific column.
Here are the steps:
1. user enters a number into the cell and then executes the macro.
2. based on the value of what the user has entered, the macro will find the text in a column.
I got everything to work pretty well except I don't know how to define the value of the cell that the user enters. Any help here would be appreciated.
Sheets("New Version ").Select
Range("B4").Select
Sheets("PN_List").Select
Columns("I:I").Select
'below is where I struggle
Selection.Find(What:=(""), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Let's say the user enters a number into cell B4, then you just have to adjust your code into:
Selection.Find(What:=Range("B4").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
You can do this in 2 ways.
Number1:
Module based: (code in module)
Sub Sample()
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
Number2:
Worksheet Event based. (code in Sheet)
Private Sub Worksheet_Change(ByVal Target as Range)
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
If Not Intersect(Target, search_value) Is Nothing Then
query = Msgbox("Search data?", vbYesNo)
If query = 7 Then Exit Sub
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
The first one you enter data in B4 then run the macro.
The second one fires every time you change value in B4.
A msgbox will appear asking if you want to search the data entered.
Hope this helps.
Related
Sub FindMatch()
Dim Cell As Range
Worksheets("Member data").Activate
Columns("T:T").Select
Set Cell = Selection.Find(What:="arabinow#gmail.com", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
MsgBox ("Nothing")
Else
MsgBox ("Something)
End If
End Sub
Keep getting Runtime error 1004
Activate method of worksheet class failed
I definitely have a sheet in my workbook called "Member data"
I ran the sub and expected that sheet to be the active sheet
Try it without using Activate or Select:
Sub FindMatch()
Dim Cell As Range
With Worksheets("Member data")
Set Cell = Columns("T:T").Find(What:="arabinow#gmail.com", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
MsgBox ("Nothing")
Else
Debug.Print Cell.Address
MsgBox ("Something")
End If
End With
End Sub
I need to find a text string and store the item names below the text string to put in a different location in the sheet
Example I want to find "Description" and store all the items below it to use later in the macro
And place them in B1 for example
Here's the code im trying to use but I don't know how to store the Active Range
Sub test()
'find description
Cells.Find(What:="Description", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Offset(1, 0).Select 'Offset after find
Range(Selection, Selection.End(xlDown)).Select 'Selects to end
Dim DescriptionValues As Range
DescriptionValues = Active.Range
ActiveSheet.Range("B10") = DescriptionValues 'put stored text starting in B1
End Sub
Sub test()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:="Description", After:=ActiveSheet.Range("A1"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
Set rng = ActiveSheet.Range(rng.Offset(1, 0), rng.End(xlDown))
ActiveSheet.Range("B1").Resize(rng.Rows, 1).Value = rng.Value 'put stored text starting in B1
End If
End Sub
I am using Excel VBA to search for a string e.g. "CCC" in an Excel worksheet.
I used the simple code shown.
However, I want VBA to select the cell where the first occurrence of "CCC" is found. (Just like when you do a FIND manually).
How can I modify my code to achieve this?`
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rng Is Nothing Then
MsgBox ("found")
Else
MsgBox ("not found")
End If
End Sub
To select a cell, you can use the Select method. So, if you have a Range object which has been set to the cell you want, you can apply the Select method to that object, i.e.
rng.Select
Incorporated into your existing code, it might look something like:
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rng Is Nothing Then
MsgBox "found"
rng.Select
Else
MsgBox "not found"
End If
End Sub
You can use rng.Select but it needs the worksheet to be active on top (or you habe to activate it first i.e. rng.Parent.Activate). Simplest is to use Application.Goto rng
If Not rng Is Nothing Then
Application.Goto rng
MsgBox ("found")
Else
MsgBox ("Not found")
End If
Private Sub CommandButton1_Click()
Range("A1").select
Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).activate
activcecell.select
If Not rng Is Nothing Then
MsgBox ("found")
Else
MsgBox ("not found")
End If
End Sub
Using activecell.select will select your first find cell.
How do I get cell address using Find function.
Here's the code
Dim Found As Range
Set Found = Worksheets("Sheet 1").Cells.Find(What:="test", LookAt:=xlWhole, MatchCase:=True)
If Not Found Is Nothing Then
' do something
End If
When I debug the code, "Found" variable contain a "string" instead of cell address.
It seems you can just use found.address even though it shows as string. The below code worked for me.
Sub findCellAddress()
Dim ra As Range
Set ra = Cells.Find(What:="fff", LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
MsgBox (ra.Address)
End If
End Sub
I could not find this anywhere on the internet.
This code will give you the row and the column.
Dim ThisPos As Range
With Range("A1:J100")
Set ThisPos = .Find(What:="List_Position", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not ThisPos Is Nothing Then
Cell_Add = Split(ThisPos.Address, "$")
ThisRow = Cell_Add(1)
ThisCol = Cell_Add(2)
End If
End With
This code will give you reference style of the cell address.
Dim SValue As Range
With Range("D1:D100")
Set SValue = .Find(What:="Searched Value", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not SValue Is Nothing Then
Cell_Split_R = Split(SValue.Address(ReferenceStyle:=xlR1C1), "R")
Cell_Split_C = Split(Cell_Split_R(1), "C")
SCol = Cell_Split_C(0)
SRow = Cell_Split_C(1)
End If
End With
I am trying to complete a simple macro that looks for '#REF!' in a worksheet due to a user alterting a row and ruining underlying formulas.
I have got as far as the find:
Sheets("Location_Multiple").Select
Range("A1:AL10000").Select
Selection.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
From what I understand I need to enter an If parameter is true then
MsgBox"Please go back and check...."
Im just not sure what should follow the if....
Any pointers would be very much appreciated.
Try below code
Sub DisplayError()
On Error Resume Next
Dim rng As Range
Set rng = Sheets("Location_Multiple").Range("A1:AL10000")
Dim rngError As Range
Set rngError = rng.SpecialCells(xlCellTypeFormulas, xlErrors)
If Not rngError Is Nothing Then
For Each cell In rngError
MsgBox "Please go back and check.... " & cell.Address
Next
End If
End Sub
Use this, change the LookIn argument to xlValues instead of xlFormulas:
Selection.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
For a bit cleaner implementation:
Dim sht as Worksheet
Dim rngSrch as Range
Dim rngErr as Range
Set sht = Sheets("Location_Multiple")
Set rngSrch = sht.Range("A1:AL10000")
Set rngErr = rngSearch.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rngErr Is Nothing Then
'Do something to the offending cell, like, highlight it:
rngErr.Interior.ColorIndex = 39
End If
Anticipating there may be multiple cells with an error like this, you'll probably have to implement your .Find within a Do...While loop. There are several examples of that sort of problem on SO. If you have trouble implementing the loop, let me know.
The problem you have is because you're searching for a string - whereas #REF is an error.
You could use the IsError function, to return true for a cell with an error. Combine this with a loop, and you could achieve what you require. I haven't tested this, but you get the jist:
Set rng = Sheets("Location_Multiple").Range("A1:AL10000")
For Each cell In rngError
If IsError(cell) == true
MsgBox "Please go back and check.... " & cell.Address
Endif
Next