Macro for "Sumif/Vlookup"

Reply

Join Date: Mar 2009
Posts: 12
Reputation: shahji is an unknown quantity at this point 
Solved Threads: 0
shahji shahji is offline Offline
Newbie Poster

Macro for "Sumif/Vlookup"

 
0
  #1
May 5th, 2009
Hello,

i am working on a macro where i need to copy data from a sheet to a different sheet in the same workbook. i normally use "vlookup" or "Sumif" or "Sumproduct" formulae to do the job. the datas is quite large and it takes ages to refresh the data when i use "Sumif" or "Vlookup". i am looking for a macro which could replace these formulae. I have included a short example of the database.

Sheet1 has the data and it should be pasted on Sheet2 by looking in Sheet1 column "Code" should be used as a condition. for every "700" there are a few value and they should be taken from Sheet1 and pasted in Sheet2 corresponding to the "Client". same with other "Codes" and i might need to add the Balances in sum instances.

thank you in advance for your help,

Shah
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,094
Reputation: cguan_77 has a little shameless behaviour in the past 
Solved Threads: 91
cguan_77's Avatar
cguan_77 cguan_77 is offline Offline
Veteran Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #2
May 10th, 2009
you're looking for a specific value in sheet1 to pasted in sheet2?
or you need to do calculation first in sheet1 and after doing the calculation you'll find for a specific result and copy over to sheet2?
is it what you're trying to do or what?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: shahji is an unknown quantity at this point 
Solved Threads: 0
shahji shahji is offline Offline
Newbie Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #3
May 11th, 2009
well thank you for your reply,

I would not have to calculate anything as all the values are already in the database. i would need to find a specific result and copy over to sheet2. i hope this helps,

thanks,

Shah
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,094
Reputation: cguan_77 has a little shameless behaviour in the past 
Solved Threads: 91
cguan_77's Avatar
cguan_77 cguan_77 is offline Offline
Veteran Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #4
May 12th, 2009
try this..
it will find a specific value..then copy the whole rows to sheet2.. just modify it to suit your needs.. hope it helps..

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim x as boolean
  2. Dim x1 as string
  3.  
  4. x = Cells.Find(What:="Data to Find", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
  5. xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
  6. False, SearchFormat:=False).Activate
  7.  
  8. if x = true then
  9.  
  10. x1 =activecell.row
  11.  
  12. rows(x1).copy
  13.  
  14. sheet2.range("1:1").pastespecial
  15.  
  16. else
  17.  
  18. msgbox "Data not found"
  19.  
  20. end if
Last edited by cguan_77; May 12th, 2009 at 12:58 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: shahji is an unknown quantity at this point 
Solved Threads: 0
shahji shahji is offline Offline
Newbie Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #5
May 13th, 2009
thank you cguan,

the code works fine.
i was wondering if i could run a loop on this as the code stops after looking at the first value in sheet1 and copies it to sheet2.

Also is it possible to give it more than one codition lets say it looks in sheet2 column1 for a value and copy a value from sheet1 column 3 corresponding to the value in sheet2 column1 and a condition in sheet1 column2.

i hope i have explained it ok.

thank you
Last edited by shahji; May 13th, 2009 at 10:08 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,094
Reputation: cguan_77 has a little shameless behaviour in the past 
Solved Threads: 91
cguan_77's Avatar
cguan_77 cguan_77 is offline Offline
Veteran Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #6
May 14th, 2009
yes it is possible to look for a value in sheet column1 and copy a value from sheet1 column3 but what you mean by "and a condition in sheet1 column2"?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: shahji is an unknown quantity at this point 
Solved Threads: 0
shahji shahji is offline Offline
Newbie Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #7
May 14th, 2009
thank for getting back to me,

well i have attached a small example.

sheet1 has three columns "Client", "Code" and "Balance" and sheet2 has two columns "Client"and "Balance".

i want to copy data from Sheet1 "Balance" to Sheet2 "Balance". the data should correspond to the "client" column.
there are many entries in "Code" but i am just interested in a few. lets say "Code" is "700" and i want to copy "balance" for Client "0014" and past it in sheet2 "Balance" corresponding to the client.

i hope this will help,

btw i was able to loop the macro but as it is not dynamic it just keeps on going through the sheets and copies nothing.

thank you for your help.
Attached Files
File Type: zip example.zip (21.6 KB, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,094
Reputation: cguan_77 has a little shameless behaviour in the past 
Solved Threads: 91
cguan_77's Avatar
cguan_77 cguan_77 is offline Offline
Veteran Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #8
May 15th, 2009
hi shahji, try this, hope it helps...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Macro1()
  2.  
  3. Dim x, y As Boolean
  4. Dim x1, x2, x3 As String
  5. Dim y1, y2, y3 As String
  6.  
  7. 'declare the data to find
  8. y1 = "700"
  9. y2 = "0014"
  10.  
  11. 'activate sheet1 and search
  12. Sheets("Sheet1").Select
  13. Columns("b:b").Select
  14. x = Selection.Find(What:=y1, After:=ActiveCell, LookIn:=xlFormulas, _
  15. LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  16. MatchCase:=False, SearchFormat:=False).Activate
  17.  
  18. 'check if data is found
  19. If x = True Then
  20.  
  21. x1 = ActiveCell.Row
  22. x2 = Cells(x1, 1).Value
  23. x3 = ActiveCell.Value
  24.  
  25. End If
  26.  
  27. 'check if data match together if not search again
  28. If x2 <> y2 Then
  29.  
  30. y = Cells.FindNext(After:=ActiveCell).Activate
  31.  
  32. Else
  33.  
  34. End If
  35.  
  36. 'if data found found then copy
  37. If y = True Then
  38. y3 = ActiveCell.Row
  39. Rows(y3).Select
  40. Rows(y3).Copy
  41. End If
  42.  
  43. 'select sheet2 and search the data
  44. Sheets("Sheet2").Select
  45. Columns("a:a").Select
  46. y = Selection.Find(What:=y2, After:=ActiveCell, LookIn:=xlFormulas, _
  47. LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  48. MatchCase:=False, SearchFormat:=False).Activate
  49.  
  50. ' found then paste the data from sheet1
  51. If y = True Then
  52. x1 = ActiveCell.Row
  53. Cells(x1, 1).Select
  54. Selection.PasteSpecial
  55.  
  56. End If
  57.  
  58. End Sub
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: shahji is an unknown quantity at this point 
Solved Threads: 0
shahji shahji is offline Offline
Newbie Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #9
May 15th, 2009
hey Cguan,

i have tried the above but it copies the macro to sheet2 rather than any data.
any ideas why is that?

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,094
Reputation: cguan_77 has a little shameless behaviour in the past 
Solved Threads: 91
cguan_77's Avatar
cguan_77 cguan_77 is offline Offline
Veteran Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #10
May 15th, 2009
try this..

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Macro1()
  2.  
  3. Dim x, y As Boolean
  4. Dim x1, x2, x3 As String
  5. Dim y1, y2 As String
  6.  
  7. 'declare the data to find
  8. y1 = "700"
  9. y2 = "0014"
  10.  
  11. 'activate sheet1 and search
  12. Sheets("Sheet1").Select
  13. Columns("b:b").Select
  14. x = Selection.Find(What:=y1, After:=ActiveCell, LookIn:=xlFormulas, _
  15. LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  16. MatchCase:=False, SearchFormat:=False).Activate
  17.  
  18. 'check if data found then copy
  19. If x = True Then
  20.  
  21. x1 = ActiveCell.Row
  22. x3 = ActiveCell.Value
  23. Rows(x1).Select
  24. Selection.Copy
  25.  
  26. End If
  27.  
  28.  
  29. 'select sheet2 and search the data
  30. Sheets("Sheet2").Select
  31. Columns("a:a").Select
  32. y = Selection.Find(What:=y2, After:=ActiveCell, LookIn:=xlFormulas, _
  33. LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
  34. MatchCase:=False, SearchFormat:=False).Activate
  35.  
  36. 'found then paste the data from sheet1
  37. If y = True Then
  38. x1 = ActiveCell.Row
  39. Cells(x1, 1).Select
  40. Selection.PasteSpecial
  41.  
  42. End If
  43.  
  44. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC