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?
Related
I have a table in Sheet1. I need to search in Sheet1 for terms in Sheet2-ColumnA.
The exclusion list in Sheet2-ColumnA does not match the cell contents in Sheet1, but is found within the cell contents (Ex: find "orange" in "yellow;orange" or "orange;yellow").
If that criteria is found, delete the row. If it doesn't find the criteria, continue on down the list until it reaches an empty cell.
I recorded one round of this, but I need to modify it to loop through the entire exclusion list until it reaches an empty cell in the exclusion list.
Sub ExclusionList()
'
' ExclusionList Macro
' Find terms from exclusion list and delete row
'
' Go to sheet2 and select first term in exclusion list
Sheets("Sheet2").Select
Range("A1").Select
' Copy cell contents and find in sheet 1
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Cells.Find(What:="orange", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
' Delete row if found
Application.CutCopyMode = False
Selection.EntireRow.Delete
End Sub
In this example, "orange" is the criteria in Sheet2 A1. If it is possible to skip the copy/paste and refer directly to the exclusion list in the Cells.Find() function it seems like that would clean up the code and be more efficient overall.
Try this.
Here is a useful resource on avoiding Select/Activate. This shortens code considerably and makes it more effective.
Sub ExclusionList()
Dim r As Range, rFind As Range
With Sheets("Sheet2")
For Each r In .Range("A1", .Range("A" & Rows.Count).End(xlUp)) 'loop through every cell in sheet2 column A
Set rFind = Sheets("Sheet1").Cells.Find(What:=r.Value, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then 'check that value is found to avoid error on next line
rFind.EntireRow.Delete
End If
Next r
End With
End Sub
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
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
I need to search a workbook for a few key phrases (always in the header row) and when the phrase is found copy the entire column to a new workbook, then carry on to find the next phrase. When I recorded my macro this is what it gave me, but the key phrase may not always be in Column B so this could err quickly! The scope of my project is to find these keywords, select the column until the last row with data, and copy into a separate workbook. I will be merging 6 - 7 workbooks.
Cells.Select
Selection.Find(What:="Emp Name", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Columns("B:B").Select
Selection.Copy
Well you don't say why you need it in C# but the VBA way to select the "current" column is:
ActiveCell.EntireColumn.Select
This will select whatever column contains the keyword.
The VBA to create a new workbook and paste in the column is just
Workbooks.Add
ActiveSheet.Paste
Allow me to explain the scenario first...
We have a reporting spreadsheet that is capable of updating monthly data for several different measures.
Over time, more and more macros have been added and now total over 20.
To make work a little easier I've added yet another macro, which brings up a user form that
Calls each of the other macros one by one and shows a progress bar to indicate how many tasks (macros) have been completed.
The first 8 macros called upon prompt with an input box for which month is being updated - this will always be the same month across all 8.
SO, what I want to do is add a global input box as the first thing the userform does, then for this input to be referenced in the other macros (having removed their individual prompts).
To be perfectly honest I have absolutely no idea how to do this but have tried the following (all together).
In Workbook
Public Monthglobal As Variant
At start of userform code
Function GetMonth()
Monthglobal = InputBox("Please enter the 3 letter abbreviation for the Month which your are updating (e.g. Jan, Feb...)", "Month")
If strName = vbNullString Then Exit Function
End Function
at the start of the userform Sub which calls the macros one by one
GetMonth
Within each of the 8 macros (contained in Module 1)
'Searches for correct column for month and pastes data
Selection.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Activate
ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Searches for correct column for month and pastes data
Result
Run-time error '91':
Object Variable or With block variable not set
The error is returned with the search (for Variable) section highlighted:
Selection.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
I hope thats enough to make sense of it, if anyone needs to see more of the code for the individual macros please gimme a shout but I thought the error message clearly indicates WHERE the issue is...just not WHAT to someone of my lacking experience!
Thanks in advance,
I would do something like this:
Public Monthglobal As String
Function GetMonth()
Monthglobal = InputBox("Please enter the 3 letter abbreviation for the Month which your are updating (e.g. Jan, Feb...)", "Month")
End Function
The search bit - it's better to define what range to search through programatically, rather than using 'Selection'. Will the 3 letter month be the entire cell contents, or just part of it?
Dim rngFind as Range, rngFound as Range
Set rngFind = Range("A:A") ' Set this to whatever 'Selection' is currently
'Now set the rngFound variable to be the eventual 'ActiveCell' from your macro above (found cell, offset(1,0))
Set rngFound = rngFind.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,MatchCase:=False).Offset(1, 0)
'Now paste without activating anything:
rngFind.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False