Excel/VBA find string and copy offset cells - excel

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

Related

Excel search only in column a?

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 ...

VBA : Paste in Find command

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).

How to search a row for a string and then select the column of that string in VBA

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.

Entering number in B column then finding number in A column

Sorry if the title is confusing.
I am working on an excel file that is a bit complicated.
Basically I need it to put a number in column B and have the macro find the same number in column A then copy everything (Including that number) above it.
I have tried using the find button but I cant seem to make it automatically find the number listed in column B in relation to column A.
This is the code I have tried so far:
Range("D1").Select
Cells.Find(What:="12", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Range("A58").Select
Range(Selection, Cells(1)).Select
Selection.Copy
Okay, so I figured it out in my own way.
Column A has a pattern but the changing part is the number like so:
tim
car
1
tim
car
2
and so on.
In column B I made a formula =IF(A3=C1,"COPY","")
C1 would have the number input from the user.
I then made a macro finding "COPY" in column B then offsetting it to the left and made it copy all the way to the top of the column.
It looked like this:
Sub Copy_1()
Application.ScreenUpdating = False
Range("B:B").Select
Selection.Find(What:="COPY", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Offset(0, -1).Activate
Range(Selection, Selection.End(xlUp)).Select
Selection.Copy
Application.ScreenUpdating = True
End Sub

VBA: How do I find a cell containing a substring in a given range?

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.

Resources