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