Cells.Find(What:=apple, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate
Want to search only in column A
It search whole sheet
Please ignore apple it's only example
Thanks in advance
Implicit cells always refers to the active sheets cells.
Be explicit regarding the range you want to search within
Dim rgToSearch As Range
Set rgToSearch = ActiveSheet.Columns("A")
rgToSearch.Find ...
Related
I want to write the following Context using VBA.
When a specific word is selected in sheet 1, I want to find a specific word in sheet 2 and move it to that cell.
Selection.Copy
Sheets("Sheet2").Select
Cell.Find(What:=Apple, After:=ActiveCell, LookIn:=xlFormulas2, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False).Activate
End Sub
I want to put the word(Clipboard Paste?) to be copied instead of Apple, how can I do in this case ?
Don't use the Clipboard. The idea of the Clipboard is to exchange data between Applications. Just write the content of the selected cell into an intermediate variable and use that as search term.
And never access the result of a Find-command directly. If Find fails to find something, it will return Nothing, you will get nasty runtime errors when you execute Nothing.Activate. Instead, assign the result of the Find to another variable and check if it contains something:
Dim searchTerm as String
searchTerm = ActiveCell.Value
Sheets("Sheet2").Select
Dim hitRange as Range
Set hitRange = Cells.Find(What:=searchTerm, After:=ActiveCell, LookIn:=xlFormulas2, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False)
If Not hitRange Is Nothing Then hitRange.Activate
A remark: Usually it's a bad idea to use Select and Activate in your code. I just left it there because I assume that the reason for your Activate is to show the result to the user. If you plan to do something else with the result, don't use Activate. Instead, work with the Range variable (hitRange).
I'm in need of a small piece of VBA to do the following,
search for a particular string, for instance ("New Applications"), and then copy the cells offset (0,2) and (0,5). Hence if "New Applications" is found in A34, I then need to copy D34 and J34…
Any help, much appreciated.
All I have so far is as below, but i'm sure how to also copy offset(0,5)..
Sub test()
Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Select
Selection.Copy
The rest of the code, re pasting etc I already have, I just need to amend a small part to something like above.
many thanks
In this case it would greatly help to use a variable.
Sub test()
'make a variable called foundrange that is of type "Range"
Dim foundRange as Range
'set this variable based on what is found: (note we remove the 'activate' here
Set foundRange = Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
'You can copy now and do whatever you want. Say you want to copy these values to Sheet2!A1 and B1, respectively:
Sheet2.Range("A1").Value = foundRange.OFfset(0,2).value
Sheet2.Range("B1").Value = foundRange.Offset(0,5).value
'Or copy to the clipboard -- haven't tested this union, but I think it should work
Union(foundRange.Offset(0,2), foundRange.Offset(0,5)).Copy
'Or copy just one and do something
foundRange.Offset(0,2).Copy
'do something
'Copy the other one and do something
foundRange.Offset(0,5).Copy
'do something
I am trying to use VBA Find function to find a date from a column and then return the row number of the date.
This works:
Cells.Find(What:="1 Jul 13", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
But, this doesn't work:
Cells.Find(What:=Range("E2"), After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Any idea? I want the user to input a date to "E2" and then run my code, so I would prefer to use the second way if possible.
Many thanks for your help.
Ben
If 1 Jul 13 actually appears in the cell, then use;
What:=Range("E2").Text
I'm working in VBA trying to select a column based on the occurrence of a certain string in the first row of an excel spreadsheet. First, I am trying to get the code to search for the term "type" in the first row of the spreadsheet, and then select the entire "type" column. From there I've been trying to set that column to a variable so I can call it later on in the code. Sorry I'm not allowed to post the code I've been working as it is for work and not allowed.
Ok, here's the code that I have so far. It searches the first row for "type" and finds that cell and selects it. I'm having trouble tying to assign the variable myCol to the entire column. I keep getting a "Object doesn't support this property or method error". Sorry if this is an obvious fix, but I'm really new to VBA.
Function ColSearch() As Integer
Sheets("CS-CRM Raw Data").Select
Sheets("CS-CRM Raw Data").Unprotect
Dim myCol As Range
Cells.Find(What:="type", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Set myCol = ActiveCell.EnitreColumn
ColSearch = myCol
End Function
EDIT: Ok here's my edited code for searching the columns, selecting the one labelled type, and then setting it equal to a variable. Now I get a "Object variable or With block variable not set" error every time I run it.
Function ColSearch() As Range
Sheets("CS-CRM Raw Data").Select
Sheets("CS-CRM Raw Data").Unprotect
Dim myCol As Range
myCol = Worksheets("CS-CRM Raw Data").Cells.Find(What:="type", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
myCol = ColSearch
End Function
This solution worked for me:
Function ColSearch2() As Integer
ColSearch2 = Worksheets("CS-CRM Raw Data").Cells.Find(What:="type", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
End Function
The problem is in using "EntireColumn".
According to the documentation, "Returns a Range object that represents the entire column (or columns) containing the specified range. Read-only.", not the column index.
http://msdn.microsoft.com/en-us/library/aa199727(v=office.10).aspx
EDIT: Just saw your edit in the main thread. The type mismatch is you return the Range object as an integer.
Say I have a range of data from A1:Y55 containg data like "123 UID"
I am looking for a unique substring (in this case, the UID) in that range, how do I do this?
You can do this with the following code.
On Error Resume Next
Range("A1:Y55").Find(What:="UID", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
The error statement will allow the program to continue if it does not find any matches.