Search & copy function (macro)
Closed
jkeixm
Posts
5
Registration date
Tuesday October 23, 2012
Status
Member
Last seen
October 25, 2012
-
Oct 23, 2012 at 04:58 PM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Oct 28, 2012 at 12:06 PM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Oct 28, 2012 at 12:06 PM
Related:
- Search & copy function (macro)
- Yahoo search history - Guide
- Safe search settings - Guide
- Google.us search - Guide
- Utorrent web search - Download - Torrent downloads
- Find function on mac - Guide
3 responses
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 23, 2012 at 07:39 PM
Oct 23, 2012 at 07:39 PM
so how this match should occur . Should this be in a batch where as you would enter all names and other information and then run macro to have sheet1 updated or you have some thing else in mind
jkeixm
Posts
5
Registration date
Tuesday October 23, 2012
Status
Member
Last seen
October 25, 2012
Oct 24, 2012 at 02:21 AM
Oct 24, 2012 at 02:21 AM
Hello again.
Sheet2 will work as a input form.
Well i wil enter "John" in A1, age and weight in B2 and C2.
Sheet2:
A B C
1 Name Input Age Input weight
2 John
Than i want it to with help of macro to find "John" in Sheet1. And copy age and weight in sheet1 on same row, where John is already is mentioned.
Sheet2 will work as a input form.
Well i wil enter "John" in A1, age and weight in B2 and C2.
Sheet2:
A B C
1 Name Input Age Input weight
2 John
Than i want it to with help of macro to find "John" in Sheet1. And copy age and weight in sheet1 on same row, where John is already is mentioned.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 24, 2012 at 06:47 AM
Oct 24, 2012 at 06:47 AM
and what if John is not listed then ?
jkeixm
Posts
5
Registration date
Tuesday October 23, 2012
Status
Member
Last seen
October 25, 2012
Oct 24, 2012 at 09:08 AM
Oct 24, 2012 at 09:08 AM
it will be delightfull with a standard error message, because jOhn is not listed.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 24, 2012 at 12:50 PM
Oct 24, 2012 at 12:50 PM
I have my doubts about your requirement. However, based on what you said, try the macro below. Only thing that you need to be aware of is that, the active sheet must be your input sheet and active row must be the row for which you want to run update for
Option Explicit Sub searchAndCopy() Dim activeRow As Long Dim activeName As String Dim matchRow As Long Dim sourceSheet As String Dim inputSheet As String sourceSheet = "Sheet1" inputSheet = "Sheet2" activeRow = ActiveCell.Row activeName = Sheets(inputSheet).Cells(activeRow, "A") With Sheets(sourceSheet) matchRow = getItemLocation(activeName, .Columns(1)) If (matchRow = 0) Then MsgBox "Error, unable to find name " + activeName, vbExclamation, "Error" Exit Sub End If .Cells(matchRow, "D") = Sheets(inputSheet).Cells(activeRow, "B") .Cells(matchRow, "E") = Sheets(inputSheet).Cells(activeRow, "C") End With End Sub Public Function getItemLocation(sLookFor As String, _ rngSearch As Range, _ Optional bFullString As Boolean = True, _ Optional bLastOccurance As Boolean = True, _ Optional bFindRow As Boolean = True) As Long 'find the first/last row/column within a range for a specific string Dim Cell As Range Dim iLookAt As Integer Dim iSearchDir As Integer Dim iSearchOdr As Integer If (bFullString) _ Then iLookAt = xlWhole Else iLookAt = xlPart End If If (bLastOccurance) _ Then iSearchDir = xlPrevious Else iSearchDir = xlNext End If If Not (bFindRow) _ Then iSearchOdr = xlByColumns Else iSearchOdr = xlByRows End If With rngSearch If (bLastOccurance) _ Then Set Cell = .Find(sLookFor, .Cells(1, 1), xlValues, iLookAt, iSearchOdr, iSearchDir) Else Set Cell = .Find(sLookFor, .Cells(.Rows.Count, .Columns.Count), xlValues, iLookAt, iSearchOdr, iSearchDir) End If End With If Cell Is Nothing Then getItemLocation = 0 ElseIf Not (bFindRow) _ Then getItemLocation = Cell.Column Else getItemLocation = Cell.Row End If Set Cell = Nothing End Function
jkeixm
Posts
5
Registration date
Tuesday October 23, 2012
Status
Member
Last seen
October 25, 2012
Oct 24, 2012 at 05:32 PM
Oct 24, 2012 at 05:32 PM
Its working.... Perfect Thanks for ur professional assistance.
Its transfer the data as i want to.....
I have to questions... If it can't find the input, like the name is not mentioned already in database. I expect it with an error message, but it does not. Example "unable to find name" And the oposit, if transfer was ok, so a msg with "transfer done"
My 2nd question is, if my input sources are on C15toE15 instead of A2 to C2. Can you please highlight it in your awsome programming.
Thanks once again.
Its transfer the data as i want to.....
I have to questions... If it can't find the input, like the name is not mentioned already in database. I expect it with an error message, but it does not. Example "unable to find name" And the oposit, if transfer was ok, so a msg with "transfer done"
My 2nd question is, if my input sources are on C15toE15 instead of A2 to C2. Can you please highlight it in your awsome programming.
Thanks once again.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 24, 2012 at 07:47 PM
Oct 24, 2012 at 07:47 PM
try to see this method. searchAndCopy. The other "getItemLocation" is just a helper
jkeixm
Posts
5
Registration date
Tuesday October 23, 2012
Status
Member
Last seen
October 25, 2012
Oct 25, 2012 at 05:09 PM
Oct 25, 2012 at 05:09 PM
Dear Rizvisa1,
I have tried to edit your macro, but i can suceed.
if my input sources in sheet2 are on C15toE15 instead of A2 to C2. What should i edit en your macro.
Sheet2:
C D E
14: Name Input Age Input weight
15: Jakob
I have tried to edit your macro, but i can suceed.
if my input sources in sheet2 are on C15toE15 instead of A2 to C2. What should i edit en your macro.
Sheet2:
C D E
14: Name Input Age Input weight
15: Jakob
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 28, 2012 at 12:06 PM
Oct 28, 2012 at 12:06 PM
It is trying to search in column 1
matchRow = getItemLocation(activeName, .Columns(1))
for the name
So when it finds a match, it get other stuff.
matchRow = getItemLocation(activeName, .Columns(1))
for the name
So when it finds a match, it get other stuff.