Import Data from Closed Workbook

snigacookie

New Member
Joined
Apr 15, 2008
Messages
24
Hi all,

I have hit a wall as to create an open event to import data from another workbook. This process has to be automated.

Any ideas on where to start?
 
I do not see how that will work according to the fact that the data is imported into an existing model.

The problem I have is that the source data can grow or shrink.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I do not see how that will work according to the fact that the data is imported into an existing model.

The problem I have is that the source data can grow or shrink.
can not help more without knowing what you want to do exactly
please provide an example & explain in more detail
 
Upvote 0
You have several logic and limit condition bugs.

First, you set temp1 and temp2 outside the While loop and never reevaluate them inside the loop. So, you are not really testing the values in the row that x is currently pointing to but are always testing row 2!

Second, your loop works while c<5. That means that when it terminates c will equal 5. But your very next statement checks if c>5 something that will always be false!

Third, the way you are handling c -- while the 1st bug is still present -- you will *always* process every record in the worksheet and then run into an Excel object error when you try and process a record beyond the worksheet's maximum.

Fourth, the way you are handling c -- and after you fix the 1st bug above -- it, i.e., c, will become 5 only when you encounter 5 consecutive source records where temp2="". But what if that never happens? You never check if you exhaust your data set. That means there is a possibility you will run beyond your data set -- as many as 5 records past the data set. That means there is a possibility, if your data set is large, to run out of rows in the worksheet.

Fifth, be careful on how you assign values to temp1 and temp2. You do not explicitly reference a workbook. And, you should.

All of the above said, you might want to look into treating your data source as a SQL source and use MS Query to retrieve the data. I am not sure how to frame the stopping criterion but I imagine it is possible. For an intro to the subject see
RDBMS in Excel
http://www.tushar-mehta.com/excel/newsgroups/rdbms_in_excel/index.html

This is what I currently have. Everything work but I do know know why i get an overflow error @ 'y=y+1'.


Code:
Sub Import_Removal_Data()
Dim wb As Workbook
Dim path As String
Dim x, y As Integer
Dim temp1, temp2 As Variant
 
Application.ScreenUpdating = False
path = "D:\Documents and Settings\xiae1703\Desktop\Project\"
Set wb = Workbooks.Open(path & "Fleet Hours and Cycles.xls", True, True)
'x is the source row
'y is the main data sheet row where it will be inputed
x = 2
y = 1
With thisworkbook.Worksheets(1)

temp1 = Worksheets(1).Cells(x, 1).Value
temp2 = Worksheets(1).Cells(x, 4).Value
 
c = 0
While c < 5

If temp1 <> "" Then
'Sets cells in main workbook equal to cells in other data workbook
thisworkbook.Worksheets(1).Cells(y, 1) = wb.Worksheets(1).Cells(x, 1)
thisworkbook.Worksheets(1).Cells(y, 2) = wb.Worksheets(1).Cells(x, 2)
y = y + 1
End If
x = x + 1
If temp2 = "" Then
    c = c + 1
Else
    c = 0
End If
If c > 5 Then wb.Worksheets(1).Activate

Wend
End With
wb.Close False
Set wb = Nothing
Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
Further to tusharm's post. A query table can import from a closed workbook. You can set it to refresh on file opening. Whole thing happens without any worksheet formulas and with no VBA code. In fact, it can pull from multiple files if you like, and not just from Excel.
 
Upvote 0
Thanks for your input. This is what I have done since then. I put the temp1, temp2 variables within the loop.

The code must end when c=5 and does not currently run into any errors.

However, I have come across the following scenario:

How do I add a line that seperates the data when c=2 so that it could seperate the data and put a table heading?

Code:
Sub Import_Removal_Data()
Dim wb As Workbook
Dim path As String
Dim x, y As Integer
Dim temp1 As String
Dim temp2 As String
Dim id, ac As Integer
 
 

Application.ScreenUpdating = False
path = "D:\Documents and Settings\xiae1703\Desktop\Backup\Project\"
Set wb = Workbooks.Open(path & "Fleet Hours and Cycles.xls", True, True)
'x is the source row
x = 2
'y is the main data sheet row
y = 2

temp1 = Worksheets(1).Cells(x, 1).Value
temp2 = Worksheets(1).Cells(x, 4).Value
 
c = 0
Do

If temp1 <> "" Then
'Sets cells in main workbook equal to cells in other data workbook
id = Worksheets(1).Cells(x, 1).Value
wb.Activate
Worksheets(1).Select
Cells(y, 1).Select
Cells(y, 1) = id
 

thisworkbook.Worksheets(1).Cells(y, 1) = wb.Worksheets(1).Cells(x, 1)
thisworkbook.Worksheets(1).Cells(y, 2) = wb.Worksheets(1).Cells(x, 2)
y = y + 1
End If
x = x + 1
temp1 = thisworkbook.Worksheets(1).Cells(x, 1).Value
temp2 = thisworkbook.Worksheets(1).Cells(x, 4).Value
If temp2 = "" Then
    c = c + 1
Else
    c = 0
End If

Loop Until c = 5
wb.Close False
Set wb = Nothing
Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
Currently, the data I am using to generate this worksheet has two parts that are seperated by two lines of space.

For instance, when doing a parts inventory.

Main section is OEM parts and the second contains aftermarket parts. I need the workbook to pull in both.
 
Upvote 0
HiGuys,<o:p></o:p>
<o:p> </o:p>
I’mtrying to get data to my active workbook (0 New TP Routing Creation) from manydifferent closed workbooks via a selected path created by the below macro.<o:p></o:p>
<o:p> </o:p>
SubTPRoutingsData()<o:p></o:p>
<o:p> </o:p>
DimintChoice As Integer<o:p></o:p>
DimstrPath As String<o:p></o:p>
'onlyallow the user to select one file<o:p></o:p>
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect= False<o:p></o:p>
'makethe file dialog visible to the user<o:p></o:p>
intChoice= Application.FileDialog(msoFileDialogOpen).Show<o:p></o:p>
'determinewhat choice the user made<o:p></o:p>
IfintChoice <> 0 Then<o:p></o:p>
'getthe file path selected by the user<o:p></o:p>
strPath= Application.FileDialog( _<o:p></o:p>
msoFileDialogOpen).SelectedItems(1)<o:p></o:p>
'printthe file path to sheet 1<o:p></o:p>
Cells(2,10) = strPath<o:p></o:p>
EndIf<o:p></o:p>
<o:p> </o:p>
EndSub<o:p></o:p>
<o:p> </o:p>
Theabove works well (Show’s which file we are currently working from – as a check andplaces it into “J2”) but the next step is myproblem.<o:p></o:p>
<o:p> </o:p>
SubRetrievingData()<o:p></o:p>
<o:p></o:p>
DimobjWorkbook As Workbook<o:p></o:p>
Dimi As Integer<o:p></o:p>
'openthe workbook with data<o:p></o:p>
SetobjWorkbook = Workbooks.Open( _<o:p></o:p>
"Z:\15TP ROUTINGS\0 New TP Routing Creation\Information\J2")<o:p></o:p>
‘TubeAssembly - Part number<o:p></o:p>
ThisWorkbook.Activate<o:p></o:p>
'startin THIS workbook<o:p></o:p>
Sheets("Information").Select<o:p></o:p>
'switchto data import sheet<o:p></o:p>
'Openssource file (this filename never changes)<o:p></o:p>
Workbooks.OpenFilename:="Z:\15 TP ROUTINGS\0 New TP Routing Creation\Information\J2" ' ,ReadOnly:=True<o:p></o:p>
Sheets("Basicsheet").Select<o:p></o:p>
'switchto source data sheet<o:p></o:p>
[B7].CurrentRegion.Copy<o:p></o:p>
'Copydata to clipboard<o:p></o:p>
ThisWorkbook.Activate<o:p></o:p>
'Returnto THIS workbook<o:p></o:p>
[D5].PasteSpecialPaste:=xlPasteValues<o:p></o:p>
'pastedata to import start cell<o:p></o:p>
<o:p> </o:p>
Thisrepeats 22 more times to retrieve all data required and then “End Sub”<o:p></o:p>
<o:p> </o:p>
Pleasecan you assist? <o:p></o:p>
<o:p> </o:p>
Kindregards<o:p></o:p>
Tony<o:p></o:p>
 
Upvote 0
Hi, you replied to a post from 5 years ago. You would better start your own thread. You might refer to this one with a link.
Currently I'm only quickly visiting here: no time to reply myself.
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top