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

Re: Macro for "Sumif/Vlookup"

 
0
  #11
May 18th, 2009
hey Cguan,

thank you for the reply. the macro works but copies the entire row to sheet2.

nearly there but not quite. two more things
1) if i need to look for a whole column instead of y2 = "0014" for example y2 = A:A in sheet 2
2)I need to copy the value in Sheet1 Column C only not the entire row.

thanks
Last edited by shahji; May 18th, 2009 at 7:01 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,096
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
  #12
May 20th, 2009
Originally Posted by shahji View Post
hey Cguan,

thank you for the reply. the macro works but copies the entire row to sheet2.

nearly there but not quite. two more things
1) if i need to look for a whole column instead of y2 = "0014" for example y2 = A:A in sheet 2
2)I need to copy the value in Sheet1 Column C only not the entire row.

thanks
try this, hope it helps.

Sub Macro1()

Dim x, y As Boolean
Dim x1, x2, x3 As String
Dim y1, y2 As String
Dim z1, z2 As String

'declare the data to find
'y1 = "700"
'y2 = "0014"
y1 = InputBox("Enter data to search in sheet 1 column a")
y2 = InputBox("Enter data to search in sheet 2 column a")

'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
z1 = Cells(x1, 3).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, 2).Select
Selection.PasteSpecial

End If

End Sub
TRY MY SUGGESTIONS AT YOUR OWN RISK
Do other alternative first..cheap and easy ways..
Don't take out money from your pocket when you're not so sure that it will solve the problem..
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
  #13
May 21st, 2009
hey Cguan,

thanks for getting back to me.

after a long research i have found the following macro which copies data based on a condition and runs a loop as well.

Sub CopyData()
Dim lRow As Long, x As Long, y As Long, z As Long

lRow = Sheets("sheet1").range("A65535").End(xlUp).Row

y = 2

For x = 2 To lRow
If Sheets("sheet1").range("B" & x) = "700" Then
Sheets("sheet2").range("B" & y).Value = Sheets("sheet1").range("C" & x).Value

y = y + 1
End If
Next
End Sub


one problem remains.
i would like the macro to past value in front of the corresponding client. as from my previous example client "0014" has a "700" code. this macro picks up all the balances which are "700" but it does not match them with the right "client". any idea how i can do that. as both sheets have client codes in Column A.

thank you very much.

shah
Last edited by shahji; May 21st, 2009 at 12:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,096
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
  #14
May 21st, 2009
try this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Macro2()
  2.  
  3.  
  4. Dim lRow As Long, x As Long, y As Long, z As Long, i As Long
  5. Dim xx(5) As String 'increase the array to any number you want
  6.  
  7. lRow = Sheets("sheet1").Range("A65535").End(xlUp).Row
  8.  
  9. y = 0
  10.  
  11. For x = 2 To lRow
  12. If Sheets("sheet1").Range("B" & x) = "700" And Sheets("sheet1").Range("a" & x) = "0014" Then
  13.  
  14. y = y + 1
  15.  
  16. xx(y) = Sheets("sheet1").Range("C" & x).Value
  17.  
  18. End If
  19. Next
  20.  
  21.  
  22.  
  23. For z = 1 To lRow
  24.  
  25. If Sheets("sheet2").Range("a" & z) = "0014" Then
  26. i = i + 1
  27. Sheets("sheet2").Range("b" & z).Value = xx(i)
  28.  
  29. End If
  30. Next
  31.  
  32. End Sub
Last edited by cguan_77; May 21st, 2009 at 10:39 pm.
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
  #15
May 22nd, 2009
thanks man,

we are nearly there. the macro works for one client but it does not pick the balances for remaining clients. lets say 0014 is the first client and 0027A is the 2nd client and 0027A2 is the third client.
the macro stops at first client (0014) and pastes the value.
i want it to keep looking in sheet2 column A and get the balances from sheet1 column C until there are no more clients in Sheet2 column A.

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,096
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
  #16
May 22nd, 2009
check my previous post..i implement the input box..then just put a loop.. like msgbox yes, no..
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: ragsy.r is an unknown quantity at this point 
Solved Threads: 0
ragsy.r ragsy.r is offline Offline
Newbie Poster

Re: Macro for "Sumif/Vlookup"

 
0
  #17
Jul 4th, 2009
Hi Cguan,

I have a similar request to build a macro for the spreadsheet to pull data from the master sheet with a few parameters in particular. If you can help me, I will send you the sample data.

Looking forward for your reply.

-Ragsy
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1,096
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
  #18
Jul 4th, 2009
ok, try sending your data...maybe guys around here also got an idea on what you want to achieve...

and also post details on what you want to do

Welcome to DaniWeb!!!
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
  #19
Jul 6th, 2009
Hello everyone,

I am posting a Vlookup Macro. thanks to Brian Baulsom. I hope this will help everyone. If you have any questions please let me know.


'=========================================================
'- GENERIC LOOKUP MACRO TO
'- FIND A VALUE IN ANOTHER WORKSHEET
'- AND RETURN A VALUE FROM ANOTHER COLUMN
'=========================================================
'- select the cell containing the first search value
'- and run this macro from there.
'- can be set to continue down the column
'- [** need to make changes below as required **]
'- Brian Baulsom May 2005
'==========================================================
Dim MyValue As Variant
Dim FromSheet As Worksheet
Dim LookupColumn As Integer
Dim FromRow As Long
Dim FromColumn As Integer
'-
Dim ToSheet As Worksheet
Dim StartRow As Long
Dim LastRow As Long
Dim ActiveColumn As Integer
Dim ReturnColumnNumber
Dim ToRow As Long
Dim FoundCell As Object

'=============================================================
'- MAIN ROUTINE
'=============================================================
Sub DO_LOOKUP()
Application.Calculation = xlCalculationManual
'----------------------------------------------------------
'- LOOKUP SHEET [**AMEND AS REQUIRED**]
Set FromSheet = Sheets("S6values")
LookupColumn = 1 ' look for match here
FromColumn = 2 ' return value from here
'-----------------------------------------------------------
'- ACTIVE SHEET
Set ToSheet = Sheets("MonthlyFigures")
ActiveColumn = 1
StartRow = 6
'-------------------------------------------------------------
'- COMMENT OUT UNWANTED LINE, UNCOMMENT THE OTHER
'- ..............................[** FOR MULTIPLE ROWS **]
'LastRow = ToSheet.Cells(65536, ActiveColumn).End(xlUp).Row
'-
'- ..............................[** FOR A SINGLE VALUE **]
LastRow = ToSheet.Cells(65536, ActiveColumn).End(xlUp).Row
'-------------------------------------------------------------
'- COLUMN NUMBER TO PUT RETURNED VALUE [**AMEND AS REQUIRED**]
ReturnColumnNumber = 2 ' column number
'-------------------------------------------------------------
'- loop through each row (which may be only 1)
For ToRow = StartRow To LastRow
MyValue = ToSheet.Cells(ToRow, ActiveColumn).Value
FindValue
Next

End Sub
'== END OF PROCEDURE ====================================================

'========================================================================
'- FIND VALUE
'========================================================================
Private Sub FindValue()
Set FoundCell = _
FromSheet.Columns(LookupColumn).Find(MyValue, LookIn:=xlValues)
If FoundCell Is Nothing Then

Else
FromRow = FoundCell.Row
'- transfer additional data.
ToSheet.Cells(ToRow, ReturnColumnNumber).Value = _
FromSheet.Cells(FromRow, FromColumn).Value
End If
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