| | |
Macro for "Sumif/Vlookup"
![]() |
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
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
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
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..
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)
Dim x as boolean Dim x1 as string x = Cells.Find(What:="Data to Find", After:=ActiveCell, LookIn:=xlValues, LookAt:= _ xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate if x = true then x1 =activecell.row rows(x1).copy sheet2.range("1:1").pastespecial else msgbox "Data not found" end if
Last edited by cguan_77; May 12th, 2009 at 12:58 am.
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
hi shahji, try this, hope it helps... 

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Sub Macro1() Dim x, y As Boolean Dim x1, x2, x3 As String Dim y1, y2, y3 As String 'declare the data to find y1 = "700" y2 = "0014" 'activate sheet1 and search Sheets("Sheet1").Select Columns("b:b").Select x = Selection.Find(What:=y1, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate 'check if data is found If x = True Then x1 = ActiveCell.Row x2 = Cells(x1, 1).Value x3 = ActiveCell.Value End If 'check if data match together if not search again If x2 <> y2 Then y = Cells.FindNext(After:=ActiveCell).Activate Else End If 'if data found found then copy If y = True Then y3 = ActiveCell.Row Rows(y3).Select Rows(y3).Copy End If 'select sheet2 and search the data Sheets("Sheet2").Select Columns("a:a").Select y = Selection.Find(What:=y2, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate ' found then paste the data from sheet1 If y = True Then x1 = ActiveCell.Row Cells(x1, 1).Select Selection.PasteSpecial End If End Sub
try this.. 

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Sub Macro1() Dim x, y As Boolean Dim x1, x2, x3 As String Dim y1, y2 As String 'declare the data to find y1 = "700" y2 = "0014" 'activate sheet1 and search Sheets("Sheet1").Select Columns("b:b").Select x = Selection.Find(What:=y1, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate 'check if data found then copy If x = True Then x1 = ActiveCell.Row x3 = ActiveCell.Value Rows(x1).Select Selection.Copy End If 'select sheet2 and search the data Sheets("Sheet2").Select Columns("a:a").Select y = Selection.Find(What:=y2, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate 'found then paste the data from sheet1 If y = True Then x1 = ActiveCell.Row Cells(x1, 1).Select Selection.PasteSpecial End If End Sub
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: VB6 Datareport
- Next Thread: how to control hardware using vb
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






