Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Open a workbook and get data from it

Hello

I'm working a project where I've got a list of names in one spreadsheet, and
I need to pull corrasponding data from another spreadsheet. The concept is
simple.... get a name from spreadsheet1, then go into spreadsheet2, find
that username, and copy the row over to spreadsheet1. At least that is how
you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,522
Default Open a workbook and get data from it

Modify to suit

Sub OpenFileFindNameCopyToThisFile()
Workbooks.Open Filename:="C:\sourcefoldername\sourcefilename.xls"
Set myfind = ActiveWorkbook.Sheets("sourcesheetname").Columns(" B") _
.find(What:="leslies", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not myfind Is Nothing Then
myfind.EntireRow.Copy _
Workbooks("destinationfilename.xls").Sheets("sheet 36").Range("a17")
ActiveWorkbook.Close SaveChanges:=False
End If
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Peter" wrote in message
...
Hello

I'm working a project where I've got a list of names in one spreadsheet,
and I need to pull corrasponding data from another spreadsheet. The
concept is simple.... get a name from spreadsheet1, then go into
spreadsheet2, find that username, and copy the row over to spreadsheet1.
At least that is how you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Open a workbook and get data from it

This is psuedo code, intended to illustrate how to do what you want.
There are propbably more sophisticated ways but this is based on
the description provided of the task.


Dim wb1 As WorkBook, wb2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim lr1 As Long, lr2 As long, c As Range, fN As Range
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open FileName:='path and file to open
Set sh1 = wb1.ActiveSheet
Set sh2 = wb2.Sheets('Sheet name containing data in second wb2)
lr1 = sh1.Cells(Rows.Count, 1).End(xlUp).Row 'Assumes Col A with names
lr2 = sh2 Cells(Rows.Count, 1).End(xlUp).Row 'Change Col if req'd
For Each c in Range("A2:A" & lr1)
If Not c Is Nothing Then
Set fN = sh2.Range("A2:A" & lr2).Find(c.Value, LookIn:=xlValues)
If Not fN is Nothing Then
fN.EntireRow.Copy sh1."need to specify destination range
here

'If you only want to add supplemental data to the existing
'row in sh1, then you would not use EntireRow as the range
'to copy. Say you only need five columna to the right of
'the found fN, then you could do:
'fN.Offset(0, 1).Resize(0, 5),Copy sh1.Range("B" & c.Row)
'Which would put the copied data immediately to the right
'of the source search item.

End If
End If
Next





"Peter" wrote in message
...
Hello

I'm working a project where I've got a list of names in one spreadsheet,
and I need to pull corrasponding data from another spreadsheet. The
concept is simple.... get a name from spreadsheet1, then go into
spreadsheet2, find that username, and copy the row over to spreadsheet1.
At least that is how you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Open a workbook and get data from it

Thanks for the help that works, just one more question.

When I open the second workbook, the one that I'm coping data from
Workbooks.Open Filename:="c:\UserInfor.xls" the workbook actually opens and
is visible to the user. Is there a way to hide it so a person can't see it?


"Peter" wrote in message
...
Hello

I'm working a project where I've got a list of names in one spreadsheet,
and I need to pull corrasponding data from another spreadsheet. The
concept is simple.... get a name from spreadsheet1, then go into
spreadsheet2, find that username, and copy the row over to spreadsheet1.
At least that is how you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Open a workbook and get data from it

You can hide individual sheets in the workbook. As an alternative, you can
close the workbook after you have copied your data. Open and close it as
needed. Unless it is a very large workbook, it doesn't take much longer to
close and open than to hide and unhide the sheets.




"Peter" wrote in message
...
Thanks for the help that works, just one more question.

When I open the second workbook, the one that I'm coping data from
Workbooks.Open Filename:="c:\UserInfor.xls" the workbook actually opens
and is visible to the user. Is there a way to hide it so a person can't
see it?


"Peter" wrote in message
...
Hello

I'm working a project where I've got a list of names in one spreadsheet,
and I need to pull corrasponding data from another spreadsheet. The
concept is simple.... get a name from spreadsheet1, then go into
spreadsheet2, find that username, and copy the row over to spreadsheet1.
At least that is how you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?









  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Open a workbook and get data from it

I'm able to find what I need, just having some issues with the syntax for
coping a row of data from the source sheet to the destination.

Here is what I've got so far...

Sub FindStuff()
Dim wbkthis As Workbook
Dim shtthis As Worksheet
Dim rngThis As Range
Dim rngFind As Range
Dim firstAddress As String
Dim addSelection As String
Dim cnt
Dim Workbook
Dim Worksheet
Dim Xobj
Dim SrcRange As Range


Set wbkthis = ThisWorkbook
Set shtthis = wbkthis.Worksheets("UserList")
Workbooks.Open Filename:="c:\UserInfo.xls", ReadOnly:=True
Set Xobj = ActiveWorkbook.Sheets("owssvr(1)") 'owssvr is in userinfo.xls
Windows("UserInfo.xls").Visible = False


Set SrcRange = Xobj.Range("B02", "B275")

With SrcRange
Set rngFind = .Find("username1")

cnt = 0
If Not rngFind Is Nothing Then
firstAddress = rngFind.Address 'Take a note of where we first
found it
addSelection = addSelection & rngFind.Address & "," 'Add the
cell's range to our selection
rngFind.EntireRow.Copy
Workbooks(wbkthis).Sheets(shtthis).Range("H2") '(attempt to copy)
cnt = cnt + 1
End If
End With
Debug.Print cnt
Workbooks("UserInfo.xls").Close SaveChanges:=False
End Sub



The idea, is in the userlist workbook(ThisWorkBook) I've got a list of user
namea, and I'm searching for the match over in userInfo.xls, then I'd copy
that row, and paste it in the workbook. So if I want to pull information
about username1, which happens to be at H3 in this example, I search
UserInfo.xls, when I find the match, I copy the row and paste it in the cell
next to the username, H3 H4 etc etc.




"Peter" wrote in message
...
Hello

I'm working a project where I've got a list of names in one spreadsheet,
and I need to pull corrasponding data from another spreadsheet. The
concept is simple.... get a name from spreadsheet1, then go into
spreadsheet2, find that username, and copy the row over to spreadsheet1.
At least that is how you do it manually.

In excel VBa, from one workbook, how do I a open another workbook and
worksheet then get the correct data i need?





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to: Open closed workbook/Search data tables/Return data to open workbook Hugh Adams Excel Discussion (Misc queries) 0 August 18th 10 02:04 PM
how do i open a data workbook when i open a timesheet workbook [email protected] uk Excel Discussion (Misc queries) 2 January 4th 09 04:50 PM
1.Open workbook. 2 copy data. 3 replace data. 4 save workbook. Cristobalitotom Excel Programming 0 July 6th 06 12:24 AM
Importing Data from unopened Workbook into an open Workbook GrayesGhost Excel Discussion (Misc queries) 0 March 5th 05 11:25 PM
Open a password protected excel workbook from second workbook to fetch data using dynamic connection kaustav choudhury Excel Programming 2 April 3rd 04 06:18 AM


All times are GMT +1. The time now is 04:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"