943,882 Members | Top Members by Rank

Ad:
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 18th, 2009
0

Re: Macro for "Sumif/Vlookup"

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahji is offline Offline
18 posts
since Mar 2009
May 20th, 2009
0

Re: Macro for "Sumif/Vlookup"

Click to Expand / Collapse  Quote originally posted by shahji ...
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.

Quote ...
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
Reputation Points: 19
Solved Threads: 115
Nearly a Posting Virtuoso
cguan_77 is offline Offline
1,317 posts
since Apr 2007
May 21st, 2009
0

Re: Macro for "Sumif/Vlookup"

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahji is offline Offline
18 posts
since Mar 2009
May 21st, 2009
0

Re: Macro for "Sumif/Vlookup"

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.
Reputation Points: 19
Solved Threads: 115
Nearly a Posting Virtuoso
cguan_77 is offline Offline
1,317 posts
since Apr 2007
May 22nd, 2009
0

Re: Macro for "Sumif/Vlookup"

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahji is offline Offline
18 posts
since Mar 2009
May 22nd, 2009
0

Re: Macro for "Sumif/Vlookup"

check my previous post..i implement the input box..then just put a loop.. like msgbox yes, no..
Reputation Points: 19
Solved Threads: 115
Nearly a Posting Virtuoso
cguan_77 is offline Offline
1,317 posts
since Apr 2007
Jul 4th, 2009
0

Re: Macro for "Sumif/Vlookup"

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ragsy.r is offline Offline
1 posts
since Jul 2009
Jul 4th, 2009
0

Re: Macro for "Sumif/Vlookup"

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!!!
Reputation Points: 19
Solved Threads: 115
Nearly a Posting Virtuoso
cguan_77 is offline Offline
1,317 posts
since Apr 2007
Jul 6th, 2009
0

Re: Macro for "Sumif/Vlookup"

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
'=========================================================================******************************************************
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahji is offline Offline
18 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: VB6 Datareport
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how to control hardware using vb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC