Extracting data without opening file--please help!!

bonovox1

Board Regular
Joined
Aug 11, 2007
Messages
91
Hi, I run this macro to copy data from one file to another. This macro works fine…except that it opens the tcmochesty file…when copying the data. Is there any way of doing this without opening the file…thks

Sub DataExtract()

Dim objWrkBk, objSht1, objSht2, objSht3



Set objWrkBk = GetObject("U:\FIFX\EOD\PVBP\Eamonn\" & Format(Range("A1").Value, "mm_dd_yy") & "_tcmochesty.xls")
Dim testdata2 As Collection
Set testdata2 = New Collection




Set objSht2 = objWrkBk.Worksheets("USD")
For i = 1 To 40

testdata2.Add (objSht2.Cells(28 + i, 6).Value)
Worksheets("Sheet1").Cells(34 + i, 7) = testdata2.Item(i)
Next i






End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
bonovox1,

Sample data file "09_21_09_tcmochesty.xls", that is closed:


Excel Workbook
EF
29start-->F29
30F30
31F31
32F32
33F33
34F34
35F35
36F36
37F37
38F38
39F39
40F40
41F41
42F42
43F43
44F44
45F45
46F46
47F47
48F48
49F49
50F50
51F51
52F52
53F53
54F54
55F55
56F56
57F57
58F58
59F59
60F60
61F61
62F62
63F63
64F64
65F65
66F66
67F67
68end-->F68
USD



Your current workbook before the macro:


Excel Workbook
ABFG
19/21/2009
34
35start-->
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74end-->
Sheet1



Your current workbook after the macro:


Excel Workbook
ABFG
19/21/2009
34
35start-->F29
36F30
37F31
38F32
39F33
40F34
41F35
42F36
43F37
44F38
45F39
46F40
47F41
48F42
49F43
50F44
51F45
52F46
53F47
54F48
55F49
56F50
57F51
58F52
59F53
60F54
61F55
62F56
63F57
64F58
65F59
66F60
67F61
68F62
69F63
70F64
71F65
72F66
73F67
74end-->F68
Sheet1




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel

Code:
Option Explicit
Sub GetMyData()
' hiker95 at MrExcel.com
' 09/21/2009
' http://www.mrexcel.com/forum/showthread.php?t=417366
'
Dim MyDir As String, FN As String, SN As String, a As Long
Application.ScreenUpdating = False

MyDir = "U:\FIFX\EOD\PVBP\Eamonn\"

SN = "USD"
With ThisWorkbook.Sheets("Sheet1")
  FN = Dir(MyDir & Format(.Range("A1"), "mm_dd_yy") & "_tcmochesty.xls")
  For a = 1 To 40 Step 1
    With .Cells(34 + a, 7)
      .Formula = "='" & MyDir & "[" & FN & "]" & SN & "'!F" & 28 + a & ""
      .Value = .Value
    End With
  Next a
End With
Application.ScreenUpdating = False
End Sub


Then run the "GetMyData" macro.
 
Upvote 0
hi, i have tried this code but my work sheet is very slow as a result. My intial code works a lot faster but my only issue is that the files are opened in the backround and you are asked if you want to update them. Is there away of stopping this or at least ensuring all sheets are closed upon completion of the macro!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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