Using ADODB way t populate data under header from file to another

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hello,
this code will populate data under headers when match with the other file .
but I have TOTAL row and what I want to insert new row with the same formatting and borders before TOTAL row and expand range for formula in column E for last row if the rows numbers in source file bigger than destination file and if the rows numbers in source file smaller than destination file then should delete empty rows before TOTAL row in destination file

FILE SOURCE
TIRES.xlsx
ABCDEF
1ITEMCODEBRANDUNIT PRICEQTYTOTAL
211568MOTORMECH 150A L KOR1,000.00022,000.000
321586NOVA 200A L KOR1,500.00023,000.000
431540VEGA 55A L KOR400.0001400.000
541300XPRO 70A L KOR540.00042,160.000
651334XPRO 70A R KOR540.00042,160.000
761588BATTREY 30A CHI275.0001275.000
871590BOHANO 55A L JAP370.0001370.000
981343KM 215/65R16 TA21 KOR435.00052,175.000
1091518KM 195/65R15 TA21 KOR350.00041,400.000
11101335XPRO 150A L KOR1,000.00022,000.000
12111496XPRO 100A L KOR واطية625.0001625.000
13121448DUNLOP 285/60R18 AT25 JAP1,110.00011,110.000
14131594APPLLO 265/65R17 INDIA595.00042,380.000
15141471BS 1400R20 R180 JAP4,500.00029,000.000
16151495XPRO 200A L KOR1,285.00045,140.000
17161335XPRO 150A L KOR1,000.00044,000.000
18171595XPRO 100A R KOR610.000106,100.000
19181341XPRO 100A L KOR620.000106,200.000
20191334XPRO 70A R KOR510.000157,650.000
21201300XPRO 70A L KOR515.000105,150.000
22211338XPRO 60A L KOR425.000156,375.000
23221337XPRO 55A L KOR390.000103,900.000
24231596YOKOHAMA 7A MALAYSIA 265.0001265.000
25241598XPRO 80A L KPOR570.0001570.000
26251599HANKOOK 215/60R17 KOR525.00042,100.000
27261603335/80R20 CHI3,125.000412,500.000
28271334XPRO 70A R KOR540.00052,700.000
29281432ASIMCO 70A R KOR540.00094,860.000
30291606FORTEN 235/60R18 XL THI445.00041,780.000
31301607TOP TRUST 6.00-16 8PR F2-1 TT CHI365.0002730.000
32311608PROLITE 74A R KOR565.0001565.000
33321609HANKOOK 235/60R17 K406 KOR540.00052,700.000
34331611KM 155R13C KC55 KOR415.00041,660.000
35341612KM 195R15C KC53 VIT440.0002880.000
36351613CRYSTAL 150A KOR1,000.0001010,000.000
37361335XPRO 150A L KOR1,000.00022,000.000
38371300XPRO 70A L KOR505.00021,010.000
39381541VEGA 70A L KOR505.000105,050.000
40391448DUNLOP 285/60R18 AT25 JAP1,125.00055,625.000
41401614PRINX 235/70R16 HH2 THI475.00041,900.000
42411534BS 265/65R17 D693 THI 895.00043,580.000
43421462HANKOOK 215/70R16C KOR490.00041,960.000
44431334XPRO 70A R KOR515.00021,030.000
45441447PROLITE 44A L KOR365.0001365.000
46451617XPRO 74A L KOR515.0001515.000
47461300XPRO 70A L KOR505.0001505.000
48471339XPRO 90A L KOR600.0001600.000
49481620YOKOHAMA 205/65R16 ES32 JAP455.00041,820.000
50491624DUNLOP 265/70R18 AT23 JAP1,100.00044,400.000
51501621LASSA 4*4 235/70R16 A/T2 TR575.00042,300.000
52511623LASSA 235/50R19 H/P2 TR560.00042,240.000
53521567DUNLOP 285/65R17 JAP950.00021,900.000
54531619VEGA 70A R KOR500.00052,500.000
55541339XPRO 90A L KOR605.00095,445.000
56551598XPRO 80A L KPOR570.0001570.000
57561626KM 285/75R16 MT51 VIT835.00086,680.000
58571338XPRO 60A L KOR425.0002850.000
Page 0
Cell Formulas
RangeFormula
F2:F58F2=D2*E2





destination file


RESULT from AODB.xlsm
ABCDE
23ITEMBRANDQTYUNIT PRICETOTAL
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
69TOTAL0.00
SH1
Cell Formulas
RangeFormula
E69E69=SUM(E24:E68)



result in destination file when add new rows before TOTAL row.
RESULT from AODB.xlsm
ABCDE
23ITEMBRANDQTYUNIT PRICETOTAL
241MOTORMECH 150A L KOR21,000.002,000.00
252NOVA 200A L KOR21,500.003,000.00
263VEGA 55A L KOR1400.00400.00
274XPRO 70A L KOR4540.002,160.00
285XPRO 70A R KOR4540.002,160.00
296BATTREY 30A CHI1275.00275.00
307BOHANO 55A L JAP1370.00370.00
318KM 215/65R16 TA21 KOR5435.002,175.00
329KM 195/65R15 TA21 KOR4350.001,400.00
3310XPRO 150A L KOR21,000.002,000.00
3411XPRO 100A L KOR واطية1625.00625.00
3512DUNLOP 285/60R18 AT25 JAP11,110.001,110.00
3613APPLLO 265/65R17 INDIA4595.002,380.00
3714BS 1400R20 R180 JAP24,500.009,000.00
3815XPRO 200A L KOR41,285.005,140.00
3916XPRO 150A L KOR41,000.004,000.00
4017XPRO 100A R KOR10610.006,100.00
4118XPRO 100A L KOR10620.006,200.00
4219XPRO 70A R KOR15510.007,650.00
4320XPRO 70A L KOR10515.005,150.00
4421XPRO 60A L KOR15425.006,375.00
4522XPRO 55A L KOR10390.003,900.00
4623YOKOHAMA 7A MALAYSIA 1265.00265.00
4724XPRO 80A L KPOR1570.00570.00
4825HANKOOK 215/60R17 KOR4525.002,100.00
4926335/80R20 CHI43,125.0012,500.00
5027XPRO 70A R KOR5540.002,700.00
5128ASIMCO 70A R KOR9540.004,860.00
5229FORTEN 235/60R18 XL THI4445.001,780.00
5330TOP TRUST 6.00-16 8PR F2-1 TT CHI2365.00730.00
5431PROLITE 74A R KOR1565.00565.00
5532HANKOOK 235/60R17 K406 KOR5540.002,700.00
5633KM 155R13C KC55 KOR4415.001,660.00
5734KM 195R15C KC53 VIT2440.00880.00
5835CRYSTAL 150A KOR101,000.0010,000.00
5936XPRO 150A L KOR21,000.002,000.00
6037XPRO 70A L KOR2505.001,010.00
6138VEGA 70A L KOR10505.005,050.00
6239DUNLOP 285/60R18 AT25 JAP51,125.005,625.00
6340PRINX 235/70R16 HH2 THI4475.001,900.00
6441BS 265/65R17 D693 THI 4895.003,580.00
6542HANKOOK 215/70R16C KOR4490.001,960.00
6643XPRO 70A R KOR2515.001,030.00
6744PROLITE 44A L KOR1365.00365.00
6845XPRO 74A L KOR1515.00515.00
6946XPRO 70A L KOR1505.00505.00
7047XPRO 90A L KOR1600.00600.00
7148YOKOHAMA 205/65R16 ES32 JAP4455.001,820.00
7249DUNLOP 265/70R18 AT23 JAP41,100.004,400.00
7350LASSA 4*4 235/70R16 A/T2 TR4575.002,300.00
7451LASSA 235/50R19 H/P2 TR4560.002,240.00
7552DUNLOP 285/65R17 JAP2950.001,900.00
7653VEGA 70A R KOR5500.002,500.00
7754XPRO 90A L KOR9605.005,445.00
7855XPRO 80A L KPOR1570.00570.00
7956KM 285/75R16 MT51 VIT8835.006,680.00
8057XPRO 60A L KOR2425.00850.00
81TOTAL166,875.00
SH1
Cell Formulas
RangeFormula
E81E81=SUM(E24:E79)





another example
TIRES.xlsx
ABCDEF
1ITEMCODEBRANDUNIT PRICEQTYTOTAL
211568MOTORMECH 150A L KOR1,000.00022,000.000
321586NOVA 200A L KOR1,500.00023,000.000
431540VEGA 55A L KOR400.0001400.000
541300XPRO 70A L KOR540.00042,160.000
651334XPRO 70A R KOR540.00042,160.000
761588BATTREY 30A CHI275.0001275.000
871590BOHANO 55A L JAP370.0001370.000
981343KM 215/65R16 TA21 KOR435.00052,175.000
1091518KM 195/65R15 TA21 KOR350.00041,400.000
11101335XPRO 150A L KOR1,000.00022,000.000
12111496XPRO 100A L KOR واطية625.0001625.000
13121448DUNLOP 285/60R18 AT25 JAP1,110.00011,110.000
14131594APPLLO 265/65R17 INDIA595.00042,380.000
15141471BS 1400R20 R180 JAP4,500.00029,000.000
16151495XPRO 200A L KOR1,285.00045,140.000
17161335XPRO 150A L KOR1,000.00044,000.000
18171595XPRO 100A R KOR610.000106,100.000
19181341XPRO 100A L KOR620.000106,200.000
20191334XPRO 70A R KOR510.000157,650.000
21201300XPRO 70A L KOR515.000105,150.000
22211338XPRO 60A L KOR425.000156,375.000
23221337XPRO 55A L KOR390.000103,900.000
24231596YOKOHAMA 7A MALAYSIA 265.0001265.000
25241598XPRO 80A L KPOR570.0001570.000
26251599HANKOOK 215/60R17 KOR525.00042,100.000
27261603335/80R20 CHI3,125.000412,500.000
28271334XPRO 70A R KOR540.00052,700.000
29281432ASIMCO 70A R KOR540.00094,860.000
30291606FORTEN 235/60R18 XL THI445.00041,780.000
31301607TOP TRUST 6.00-16 8PR F2-1 TT CHI365.0002730.000
32311608PROLITE 74A R KOR565.0001565.000
33321609HANKOOK 235/60R17 K406 KOR540.00052,700.000
34331611KM 155R13C KC55 KOR415.00041,660.000
35341612KM 195R15C KC53 VIT440.0002880.000
36351613CRYSTAL 150A KOR1,000.0001010,000.000
37361335XPRO 150A L KOR1,000.00022,000.000
38371300XPRO 70A L KOR505.00021,010.000
39381541VEGA 70A L KOR505.000105,050.000
40391448DUNLOP 285/60R18 AT25 JAP1,125.00055,625.000
41401614PRINX 235/70R16 HH2 THI475.00041,900.000
42411534BS 265/65R17 D693 THI 895.00043,580.000
43421462HANKOOK 215/70R16C KOR490.00041,960.000
44431334XPRO 70A R KOR515.00021,030.000
45441447PROLITE 44A L KOR365.0001365.000
46451617XPRO 74A L KOR515.0001515.000
47461300XPRO 70A L KOR505.0001505.000
48471339XPRO 90A L KOR600.0001600.000
49481620YOKOHAMA 205/65R16 ES32 JAP455.00041,820.000
50491624DUNLOP 265/70R18 AT23 JAP1,100.00044,400.000
Page 0
Cell Formulas
RangeFormula
F2:F50F2=D2*E2



destination file

RESULT from AODB.xlsm
ABCDE
23ITEMBRANDQTYUNIT PRICETOTAL
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
81TOTAL0.00
SH1
Cell Formulas
RangeFormula
E81E81=SUM(E24:E79)




result when delete empty rows

RESULT from AODB.xlsm
ABCDE
23ITEMBRANDQTYUNIT PRICETOTAL
241MOTORMECH 150A L KOR21,000.002,000.00
252NOVA 200A L KOR21,500.003,000.00
263VEGA 55A L KOR1400.00400.00
274XPRO 70A L KOR4540.002,160.00
285XPRO 70A R KOR4540.002,160.00
296BATTREY 30A CHI1275.00275.00
307BOHANO 55A L JAP1370.00370.00
318KM 215/65R16 TA21 KOR5435.002,175.00
329KM 195/65R15 TA21 KOR4350.001,400.00
3310XPRO 150A L KOR21,000.002,000.00
3411XPRO 100A L KOR واطية1625.00625.00
3512DUNLOP 285/60R18 AT25 JAP11,110.001,110.00
3613APPLLO 265/65R17 INDIA4595.002,380.00
3714BS 1400R20 R180 JAP24,500.009,000.00
3815XPRO 200A L KOR41,285.005,140.00
3916XPRO 150A L KOR41,000.004,000.00
4017XPRO 100A R KOR10610.006,100.00
4118XPRO 100A L KOR10620.006,200.00
4219XPRO 70A R KOR15510.007,650.00
4320XPRO 70A L KOR10515.005,150.00
4421XPRO 60A L KOR15425.006,375.00
4522XPRO 55A L KOR10390.003,900.00
4623YOKOHAMA 7A MALAYSIA 1265.00265.00
4724XPRO 80A L KPOR1570.00570.00
4825HANKOOK 215/60R17 KOR4525.002,100.00
4926335/80R20 CHI43,125.0012,500.00
5027XPRO 70A R KOR5540.002,700.00
5128ASIMCO 70A R KOR9540.004,860.00
5229FORTEN 235/60R18 XL THI4445.001,780.00
5330TOP TRUST 6.00-16 8PR F2-1 TT CHI2365.00730.00
5431PROLITE 74A R KOR1565.00565.00
5532HANKOOK 235/60R17 K406 KOR5540.002,700.00
5633KM 155R13C KC55 KOR4415.001,660.00
5734KM 195R15C KC53 VIT2440.00880.00
5835CRYSTAL 150A KOR101,000.0010,000.00
5936XPRO 150A L KOR21,000.002,000.00
6037XPRO 70A L KOR2505.001,010.00
6138VEGA 70A L KOR10505.005,050.00
6239DUNLOP 285/60R18 AT25 JAP51,125.005,625.00
6340PRINX 235/70R16 HH2 THI4475.001,900.00
6441BS 265/65R17 D693 THI 4895.003,580.00
6542HANKOOK 215/70R16C KOR4490.001,960.00
6643XPRO 70A R KOR2515.001,030.00
6744PROLITE 44A L KOR1365.00365.00
6845XPRO 74A L KOR1515.00515.00
6946XPRO 70A L KOR1505.00505.00
7047XPRO 90A L KOR1600.00600.00
7148YOKOHAMA 205/65R16 ES32 JAP4455.001,820.00
7249DUNLOP 265/70R18 AT23 JAP41,100.004,400.00
73TOTAL145,240.00
SH1
Cell Formulas
RangeFormula
E73E73=SUM(E24:E72)

the code
VBA Code:
Sub test()
    Dim myDir As String, cn As Object, rs As Object, r, myHeading
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder"
        If .Show Then myDir = .SelectedItems(1) & "\"
    End With
    If myDir = "" Then Exit Sub
    Application.ScreenUpdating = False
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
   
    With ThisWorkbook
        With .Sheets(1).Cells(23, 1).CurrentRegion
            myHeading = "`" & Join(.Parent.Evaluate(.Rows(1).Address & "&"""""), "`, `") & "`"
          
            If .Rows.Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).ClearContents
        End With
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HDR=Yes;"
            .Open myDir & "TIRES.xlsx"
        End With
        rs.Open "Select " & myHeading & " From TiresTable", cn
       
        .Sheets(1).Cells(24, 1).CopyFromRecordset rs
       
    End With
    Set cn = Nothing: Set rs = Nothing
    Application.ScreenUpdating = True
End Sub
last thing I would running the macro without select folder and show dialog . two files are existed in the same folder(C:\Users\abdom\Desktop\RT\.
I know this code is not easy if there is alternative I accept that.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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