export data from file to another closed file with skip column

Hasson

Active Member
Joined
Apr 8, 2021
Messages
406
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have data in file target from A2:F in sheet DATA and should copy column A to column D for file destenaion and columns B2:F to columns F2:G for sheet EXPORTED. the closed file is destenaiton when export data from open file target .
I have about 4000 rows
I try with this code , but doesn't work for me
VBA Code:
Option Explicit

Sub UpdateData()
    Dim IntSht As Worksheet
    Dim IntBk As Workbook
    Dim ExtBk As Workbook
    Dim ExtFile As String


    Set IntBk = ActiveWorkbook
    Set IntSht = IntBk.ActiveSheet
    ExtFile = "C:\Users\PC Hass\Desktop\TABLES\ file.xlsm"
    If Dir(ExtFile) <> "" Then
  
        IntBk.Worksheets("Data").Range("a2:F4000").Value = ExtFile
    End If
    On Error Resume Next
    Set ExtBk = Workbooks(Dir(ExtFile))
    On Error GoTo 0
    If ExtBk Is Nothing Then
        Application.Workbooks.Open ExtFile
        Set ExtBk = Workbooks(Dir(ExtFile))
    End If
    IntBk.IntSht.Range("A2:F1000").Copy ExtBk.Worksheets("EXPORTED").Range("D" & Rows.Count).End(xlUp).Offset(1)
    Application.DisplayAlerts = False
    ExtBk.Save
    ExtBk.Close
    Application.DisplayAlerts = True
End Sub

target.xlsm
ABCDEF
1ITEMIDQTY1QTY2QTY3BALANCE
21TT/W-1 MM CLA1 23M-1 IT500500
32QQW-2 TH NM-1 CLA2 VBG L CHI0
43QQW-3 CV CLA3 TAI70033733
54QQW-4 M*12.5 CLA4 TR20012188
65QQW-5 CLA5 EG300300
76MMR12/100 AS-1000/1 TMR120012
87QQW-6 M230 TU11
98QQW-7 S** CLA7 US140011399
109QQW-8 CLA8 UK0
1110QQW-9 CLA9 N BR160011599
1211QQW-10 BN CLA10 IT80000800
1312QQW-11 LVD CH900900
1413BB12 QQW-12 CLA12 JA11100011989
1514BB12 QQW-12 CLA12 JA122000101990
1615BB12 QQW-12 CLA12 JA133000102990
1716BB12 QQW-12 CLA12 JA144000103990
1817BB12 QQW-12 CLA12 JA155000104990
1918BB12 QQW-12 CLA12 JA166000105990
2019BB12 QQW-12 CLA12 JA177000106990
2120BB12 QQW-12 CLA12 JA188000107990
2221BB12 QQW-12 CLA12 JA199000108990
2322BB12 QQW-12 CLA12 JA2010000109990
DATA
Cell Formulas
RangeFormula
F2:F23F2=C2+D2-E2



before
destenation.xlsm
DEFGHIJ
1ITEMBATCHIDQTY1QTY2QTY3BALANCE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
EXPORTED

result should be like this
destenation.xlsm
DEFGHIJ
1ITEMBATCHIDQTY1QTY2QTY3BALANCE
21TT/W-1 MM CLA1 23M-1 IT500500
32QQW-2 TH NM-1 CLA2 VBG L CHI0
43QQW-3 CV CLA3 TAI70033733
54QQW-4 M*12.5 CLA4 TR20012188
65QQW-5 CLA5 EG300300
76MMR12/100 AS-1000/1 TMR120012
87QQW-6 M230 TU11
98QQW-7 S** CLA7 US140011399
109QQW-8 CLA8 UK0
1110QQW-9 CLA9 N BR160011599
1211QQW-10 BN CLA10 IT80000800
1312QQW-11 LVD CH900900
1413BB12 QQW-12 CLA12 JA11100011989
1514BB12 QQW-12 CLA12 JA122000101990
1615BB12 QQW-12 CLA12 JA133000102990
1716BB12 QQW-12 CLA12 JA144000103990
1817BB12 QQW-12 CLA12 JA155000104990
1918BB12 QQW-12 CLA12 JA166000105990
2019BB12 QQW-12 CLA12 JA177000106990
2120BB12 QQW-12 CLA12 JA188000107990
2221BB12 QQW-12 CLA12 JA199000108990
2322BB12 QQW-12 CLA12 JA2010000109990
EXPORTED
Cell Formulas
RangeFormula
J2:J23J2=G2+H2-I2
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
VBA Code:
Sub OpenFilesFromFolder()
  Dim ExtBk As Workbook
  Dim IntBk As WorkBook
  Dim FolderPath As String
  Dim FilePath As String
  Dim lRow As Long
 
  IntBk = ActiveWorkbook
  lRow = IntBk.Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row

  FolderPath = "C:\Users\PC Hass\Desktop\TABLES\"
  FilePath = Dir(FolderPath & "file.xlsm")
  If FilePath <> "" Then
    Set ExtBk = Workbooks.Open(FolderPath & FilePath)
  End If
  Application.ScreenUpdating = False
  For i = 2 to lRow
    ExtBk.Worksheets("EXPORTED").Cells(i, 4).Value = IntBk.Worksheets("DATA").Cells(i, 1).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 6).Value = IntBk.Worksheets("DATA").Cells(i, 2).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 7).Value = IntBk.Worksheets("DATA").Cells(i, 3).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 8).Value = IntBk.Worksheets("DATA").Cells(i, 4).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 9).Value = IntBk.Worksheets("DATA").Cells(i, 5).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 10).Formula = "=G" & i & "+H" & i & "-I" & i
  Next
  Application.ScreenUpdating = True
  Application.DisplayAlerts = False
  ExtBk.Save
  ExtBk.Close
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
thanks ! but unfortunately shows error object variable or with block variable not set in this line
VBA Code:
IntBk = ActiveWorkbook
 
Upvote 0
thanks ! but unfortunately shows error object variable or with block variable not set in this line
VBA Code:
IntBk = ActiveWorkbook
Oh, it should include Set command:
VBA Code:
Set IntBk = ActiveWorkbook
 
Upvote 0
thanks for your solution :)
strangely it takes more time when finish running despite of the data are not big , just 4000 rows !!
 
Upvote 0
ı am not good at working with ranges. Maybe this could be a little bit faster:
VBA Code:
Sub OpenFilesFromFolder()
  Dim ExtBk As Workbook
  Dim IntBk As WorkBook
  Dim FolderPath As String
  Dim FilePath As String
  Dim lRow As Long
  Dim Rng1 As Range, Rng2 As Range
 
  Set IntBk = ActiveWorkbook
  lRow = IntBk.Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row

  FolderPath = "C:\Users\PC Hass\Desktop\TABLES\"
  FilePath = Dir(FolderPath & "file.xlsm")
  If FilePath <> "" Then
    Set ExtBk = Workbooks.Open(FolderPath & FilePath)
  End If
  Application.ScreenUpdating = False
  For i = 2 to lRow
    ExtBk.Worksheets("EXPORTED").Cells(i, 4).Value = IntBk.Worksheets("DATA").Cells(i, 1).Value
    ExtBk.Worksheets("EXPORTED").Cells(i, 10).Formula = "=G" & i & "+H" & i & "-I" & i
  Next
  Set Rng1 = IntBk.Worksheets("DATA").Range("B2:E" & lRow)
  Set Rng2 = ExtBk.Worksheets("EXPORTED").Range("F2:I" & lRow)
  Rng1.Copy
  Rng2.PasteSpecial xlPasteValues
  Application.ScreenUpdating = True
  Application.DisplayAlerts = False
  ExtBk.Save
  ExtBk.Close
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
this is really much better(y)
thanks very much for your help .:)
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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