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
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).
Looking for some assistance, I'm sure this is an easy fix but I can't get my head around how to google for it!
Basically, I am searching for a value in another sheet (this is part of a loop):
XYZ = ActiveCell.Value
Sheets("sheet1").Select
Range("a1").Activate
Cells.Find(What:=XYZ, After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
However, I have two source values that "overlap", eg: one cell is "Capability" and the other is "Capability Category".
When the macro reaches "Capability" as the look up value, it is finding the cell in with "Capability Category" in the data being searched and applies the action to that instead.
Is there a way I can tell it to look for a cell value that is an exact match, instead of any cell that contains the search term?
(Noting, I cannot re-name any source data or utilise helper columns in this particular workbook.)
Thanks
L
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 have searched through your forum as well as many others for an answer to my question. Every time I get close to what I need; but each time I fall short of getting my need fulfilled.
I am working with two open workbooks; one having a list of phone numbers and data related to incoming calls; the other related to the phone numbers and the user name for the phone number.
On the first workbook I have separated the list into groups by phone number with a blank row between the groups. I then go through the list and capture the phone number to be used in a “Find” in the second workbook, so I can match the phone number and capture the user name to take back to the first workbook and paste into the blank row below the associated group.
My problem is that no matter how I modify my find, I cannot get it to perform properly and select the phone number in the second workbook. I have finally managed to get it to process with no errors, but now it will not do a search. Here is my VB code; if you can help me with this I would really appreciate it.
If I can get the first search to work, I plan to modify that into a loop to continue throughout the first workbook until the end of the worksheet.
Sub Test_Find_Match_Data()
'
' Test_Find_Match_Data Macro
'
Dim Phone_Number As String, _
Called_Number As Long, _
Find_Test As Range
Range("A1").Select
Cells.Find(What:="called number", _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate
Selection.End(xlDown).Select
Phone_Number = ActiveCell.Value
Selection.Copy
ActiveWindow.ActivateNext
Range("A1").Select
If Find_Test Is Nothing Then
ActiveCell.Offset(RowOffset:=1, _
ColumnOffset:=0).Activate
If ActiveCell.Value <> Phone_Number Then
Set Find_Test = Cells.Find(What:=Phone_Number, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
Selection.Copy
Range("A1").Select
ActiveWindow.ActivateNext
End If
End If
End Sub
Try replacing Phone_Number = ActiveCell.Value with Phone_Number = ActiveCell.Text
I'm still learning VB. I tried recording a VB script in excel 2010 that selects a name on the main sheet, then goes to another sheet and finds all the rows with that name, copies all the rows and returns to the main sheet and insert the copied cells below the selected name. The cells are pushed down. The code should repeat for the next name below where the copied cells were paste.
My recording failed to do all of the above. Do you have a suggestion?
Sub Macro5()
'
' Macro5 Macro
'
' Keyboard Shortcut: Ctrl+l
'
Selection.Copy
Sheets("Sheet1").Select
ActiveCell.Offset(2, 3).Range("A1").Select
Cells.Find(What:="Leeanne Hickmott", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Rows("1:3").EntireRow.Select
ActiveCell.Offset(0, -7).Range("A1").Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveCell.Offset(3, 6).Range("A1").Select
End Sub
Excel's record macro process can't record the conditionals or loops you need to make that kind of macro work. It also can't record that you pasted a value into the find dialog, it just records that you want to search for "Leeanne Hickmott"
My first suggestion would be to use range variables to point to the important cells on each worksheet. For example...
Dim rngPasteHere as Range, rngCopyFrom as Range
set rngPasteHere = Selection
set rngCopyFrom = Sheets("Sheet1").Range("D1")
' find first row
set rngCopyFrom = rngCopyFrom.Cells.Find(What:=rngPasteHere.Value _
, After:=rngCopyFrom, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
You will also need two loops, an outer loop that repeats the whole process for the next name and an inner loop that finds all the rows with the current name. I can't really get more specific than that without knowing what your data looks like.
Are the names sorted alphabetically?
Are all rows with the same name right next to each other or are they mixed with other names?
Are there just a few rows of each name or thousands?