Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Access Data from Workbook without seeing the workbook open.

I have a workbook that I want to update from another workbook. I currently
open the data source workbook get the data then close it. It runs semi-slow
and I was wondering if there is a way to open the workbook hidden so the user
never sees it open in the task bar at the bottom of the screen? I figure
this would allow the macro to run faster.

Option Explicit

Private Sub btnUpdateParts_Click()

Dim wksJobCosting As Worksheet
Dim varSourceFilePath As Variant
Dim strSourceFileName As String
Dim lngLastRow As Long
Dim myArray As Variant
Dim CloseWorkbook As Boolean

Application.ScreenUpdating = False

' worksheet that recieves new data
Set wksJobCosting = ActiveWorkbook.Sheets("Parts List")

' delete old parts from list
lngLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
If lngLastRow 1 Then
Rows("2:" & lngLastRow).Delete
End If

' name of source data file, returns String or Boolean
varSourceFilePath = Application.GetOpenFilename("All Excel Files
(*.xls),*.xls")

' get file name from the file path
myArray = Split(varSourceFilePath, "\")
strSourceFileName = myArray(UBound(myArray))

If varSourceFilePath = False Then
' if user clicks cancel in the Open Dialog box
Exit Sub
Else
' disable Workbook_Open Event, then open data workbook
With Application
.EnableEvents = False
' check if source file is open already, error occurs if file is
not open
On Error Resume Next
If Workbooks(strSourceFileName) Is Nothing Then
On Error GoTo 0
CloseWorkbook = True
.Workbooks.Open FileName:=varSourceFilePath, ReadOnly:=True
End If

' copy source data, paste data in worksheet, close source workbook
With Workbooks(strSourceFileName)
lngLastRow = .Sheets("Parts List").Cells(Rows.Count,
"A").End(xlUp).Row
.Sheets("Parts List").Rows("2:" & lngLastRow).Copy
Destination:=wksJobCosting.Rows("2:2")

' close source workbook if it wasn't open before
If CloseWorkbook = True Then
.Close SaveChanges:=False
End If
End With
End With
End If

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
--
Cheers,
Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Access Data from Workbook without seeing the workbook open.

Give this a look:
http://www.rondebruin.nl/ado.htm


Regards,
Ryan---

--
RyGuy


"RyanH" wrote:

I have a workbook that I want to update from another workbook. I currently
open the data source workbook get the data then close it. It runs semi-slow
and I was wondering if there is a way to open the workbook hidden so the user
never sees it open in the task bar at the bottom of the screen? I figure
this would allow the macro to run faster.

Option Explicit

Private Sub btnUpdateParts_Click()

Dim wksJobCosting As Worksheet
Dim varSourceFilePath As Variant
Dim strSourceFileName As String
Dim lngLastRow As Long
Dim myArray As Variant
Dim CloseWorkbook As Boolean

Application.ScreenUpdating = False

' worksheet that recieves new data
Set wksJobCosting = ActiveWorkbook.Sheets("Parts List")

' delete old parts from list
lngLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
If lngLastRow 1 Then
Rows("2:" & lngLastRow).Delete
End If

' name of source data file, returns String or Boolean
varSourceFilePath = Application.GetOpenFilename("All Excel Files
(*.xls),*.xls")

' get file name from the file path
myArray = Split(varSourceFilePath, "\")
strSourceFileName = myArray(UBound(myArray))

If varSourceFilePath = False Then
' if user clicks cancel in the Open Dialog box
Exit Sub
Else
' disable Workbook_Open Event, then open data workbook
With Application
.EnableEvents = False
' check if source file is open already, error occurs if file is
not open
On Error Resume Next
If Workbooks(strSourceFileName) Is Nothing Then
On Error GoTo 0
CloseWorkbook = True
.Workbooks.Open FileName:=varSourceFilePath, ReadOnly:=True
End If

' copy source data, paste data in worksheet, close source workbook
With Workbooks(strSourceFileName)
lngLastRow = .Sheets("Parts List").Cells(Rows.Count,
"A").End(xlUp).Row
.Sheets("Parts List").Rows("2:" & lngLastRow).Copy
Destination:=wksJobCosting.Rows("2:2")

' close source workbook if it wasn't open before
If CloseWorkbook = True Then
.Close SaveChanges:=False
End If
End With
End With
End If

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
--
Cheers,
Ryan

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 07:43 AM.

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

About Us

"It's about Microsoft Excel"