If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Append data into one worksheet

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-04, 11:20
budi budi is offline
Registered User
 
Join Date: Feb 2004
Posts: 3
Red face Append data into one worksheet

How can I append data from several worksheets into one worksheet according to one ID number? Example : I have 2 worksheets, one contains database with fields : Name, Age, Job and address. Worksheet2 contains fields : Name, bodyweight, height, hobbies. Can I automatically append the data in Worksheets 2 by using VBA into Sheet1 according to the field Name (so data in sheet1 will be Name, Age, Job, address, bodyweight, height and hobbies).
Reply With Quote
  #2 (permalink)  
Old 03-10-04, 11:48
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
Re: Append data into one worksheet

Probably the best way of doing this is with using the VLOOKUP function.

you could write VBA to find the range your looking for then add that to the VLOOKUP function.

for the example above the function should read something like

=VLOOKUP($A1,sheet2!$A$1:$D$14,2,FALSE)

where $A1 is the value you are wanting to match i.e. Name
sheet2!$A$1:$D$14 is the total range of data you are looking at
2 is the column position of the data in the Table
and FALSE to imply that you are looking for exactly what is in $A1


Hope this Helps

David

Last edited by DavidCoutts; 03-10-04 at 11:55.
Reply With Quote
  #3 (permalink)  
Old 03-11-04, 17:03
knowledgewhore knowledgewhore is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
Are there duplicate records for a given name? If not you could use the "=match()" which would return the corresponding row in the second sheet. Then all you need to do is transfer the values.

Option Explicit

Public Sub test()

Dim col As Integer
Dim row As Integer
Dim match As Variant
Dim z As Integer
z = lastrow'--------------------put your last used row here,
range("Y1:Y" & z & "").Formula = "=MATCH($A1,Sheet2!$A$1:$A$" & z & ",0)"'--------------------------"=MATCH" formula, puts it in column Y
col = 1
For row = 1 To z
match = Cells(row, col + 24).Value
If IsError(match) = True Then----' no match returns #N/A so skip
Else: range("B" & row & ":C" & row & "").Value = _
Worksheets("sheet2").range("B" & match & ":C" & match & "").Value _ 'need to append the range to transfere, right now 'set to copy columns B and C

End If

Next row
range("Y1:Y" & z & "").Clear'-----------clears the cells with the Match function

End Sub
Reply With Quote
  #4 (permalink)  
Old 03-11-04, 22:28
Smitty Smitty is offline
Registered User
 
Join Date: Dec 2003
Location: San Diego, CA
Posts: 153
Welcome to the Board you two!
Quote:
!z = lastrow'--------------------put your last used row here,
You might want to add a bit to that:
Code:
Dim LastRow As Object
  Set LastRow = Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1,0)
Hope that helps,

Smitty
Reply With Quote
  #5 (permalink)  
Old 03-15-04, 18:51
knowledgewhore knowledgewhore is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
My bad
for things i've done lastrow() is a function that's called to determine the last used row which is why it wasn't declared.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On