Copy Data range from multi files to another wb

alonelove

New Member
Joined
Sep 28, 2017
Messages
45
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
Hi all I come from VietNam and have a problem:

I have a workbook "MAIN.xlsm" with 3 sheets "DATA, Noibang, Ngoaibang", sheet "DATA" have 02 botton "Get_noibang and Get_ngoaibang"

When I press "Get_noibang" I want to get data from 03 workbook into the wb MAIN, sheet Noibang
3 wb have 02 sheet and the same sheet name (sheet1 "G000141" Range to get data B19:I785 and sheet2 "G000142" range data B19:G105

And When I press "Get_ngoaibang" I want to get data from 03 workbook into the wb MAIN, sheet ngoaibang,
03 wb have the same sheet name "G000142"; Range to get data B19:G105


The problem is when i press any Botton the data always get into sheet DATA, this mean Press "Get_noibang" data get into sheet DATA, not into sheet Noibang; Press "Get_ngoaibang" data get into sheet DATA, not into sheet ngoaibang

Plz Help me to correct the Code, This's MAIN.xlsm file: https://app.box.com/s/k7tzy35k9zp3rmtjk0o9savoibz45ytx

This is the Get_noibang
Code:
Option Explicit

Public Sub GPE_Noibang()
Dim FOb As Object, Fso As Object, Item, cn As Object, rs As Object, fOld As String, fNew As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cn = CreateObject("ADODB.Connection")
If Application.Version < 12 Then
    fOld = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    fNew = ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
Else
    fOld = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    fNew = ";Extended Properties=""Excel 12.0;HDR=No;IMEX=1"";"
End If
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .Filters.Add "Microsoft Excel Files", "*.xls*", 1
    If Not .Show = -1 Then
        MsgBox "Ban chua chon File", vbInformation, "alonelove"
        Exit Sub
    End If
Range("A7").CurrentRegion.Offset(1).ClearContents
On Error Resume Next
For Each Item In .SelectedItems
    If Left(Item, 1) <> "~" Then
        cn.Open (fOld & Item & fNew)
            Set rs = cn.Execute("select * from [G000141$B19:I785] where F1 Is Not Null")
            If Not rs.EOF Then Range("A65000").End(3)(2).CopyFromRecordset rs
            rs.Close
            cn.Close
    End If
Next Item
End With
Set cn = Nothing
Set rs = Nothing
MsgBox "Done!"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub


This is the Get_ngoaibang
Code:
Option Explicit

Public Sub GPE_NGOAI()
Dim FOb As Object, Fso As Object, Item, cn As Object, rs As Object, fOld As String, fNew As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cn = CreateObject("ADODB.Connection")
If Application.Version < 12 Then
    fOld = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    fNew = ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
Else
    fOld = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    fNew = ";Extended Properties=""Excel 12.0;HDR=No;IMEX=1"";"
End If
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .Filters.Add "Microsoft Excel Files", "*.xls*", 1
    If Not .Show = -1 Then
        MsgBox "Ban chua chon File", vbInformation, "alonelove"
        Exit Sub
    End If
Range("A7").CurrentRegion.Offset(1).ClearContents
On Error Resume Next
For Each Item In .SelectedItems
    If Left(Item, 1) <> "~" Then
        cn.Open (fOld & Item & fNew)
            Set rs = cn.Execute("select * from [G000142$B19:G105] where F1 Is Not Null")
            If Not rs.EOF Then Range("A65000").End(3)(2).CopyFromRecordset rs
            rs.Close
            cn.Close
    End If
Next Item
End With
Set cn = Nothing
Set rs = Nothing
MsgBox "Done!"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Thanks in advanced
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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