Change sheetname, file name and range by cell value

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
Code:
Public Sub DATA_noibang()Dim cn As Object, rs As Object, i As Byte, lr As Long, fso As Object
Set cn = CreateObject("adodb.connection")
Set fso = CreateObject("Scripting.FileSystemObject")
Sheets("noibang").AutoFilterMode = False
Sheets("noibang").Range("A1").CurrentRegion.Offset(1).ClearContents
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "G14", "*.xl*"
        .InitialFileName = "*G14*.*"
        .AllowMultiSelect = True
        .Show
        For i = 1 To .SelectedItems.Count
            cn.Open ("provider=Microsoft.ACE.OLEDB.12.0;data source=" & .SelectedItems(i) & ";mode=Read;extended properties=""Excel 12.0;hdr=no"";")
       
        Set rs = cn.Execute("select '" & fso.GetBaseName(.SelectedItems(i)) & "',f1,f2,f3,f4,f5,f6,f7,f8, from [G141$B19:I1000] ")
       
            lr = Sheets("noibang").Range("A" & Rows.Count).End(3).Row
            If Not rs.EOF Then Sheets("noibang").Range("A" & lr + 1).CopyFromRecordset rs
            rs.Close
            cn.Close
        Next
    End With
End Sub

How can i change code with:
a = sheets("main").range(D1).value ==> a = G141
b = sheet("main").range(E1).value ==> b = B19:I1000
c = sheets("main").range(F1).value ==> c = *G14*

Then code look like as red font:
Code:
Public Sub DATA_noibang()Dim cn As Object, rs As Object, i As Byte, lr As Long, fso As Object
Set cn = CreateObject("adodb.connection")
Set fso = CreateObject("Scripting.FileSystemObject")
Sheets("noibang").AutoFilterMode = False
Sheets("noibang").Range("A1").CurrentRegion.Offset(1).ClearContents
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
[COLOR=#ff0000]        .Filters.Add "c", "*.xl*"[/COLOR]
[COLOR=#ff0000]        .InitialFileName = "c.*"[/COLOR]
        .AllowMultiSelect = True
        .Show
        For i = 1 To .SelectedItems.Count
            cn.Open ("provider=Microsoft.ACE.OLEDB.12.0;data source=" & .SelectedItems(i) & ";mode=Read;extended properties=""Excel 12.0;hdr=no"";")
       
        Set rs = cn.Execute("select '" & fso.GetBaseName(.SelectedItems(i)) & "',f1,f2,f3,f4,f5,f6,f7,f8, [COLOR=#ff0000]from [a$b] ")[/COLOR]
       
            lr = Sheets("noibang").Range("A" & Rows.Count).End(3).Row
            If Not rs.EOF Then Sheets("noibang").Range("A" & lr + 1).CopyFromRecordset rs
            rs.Close
            cn.Close
        Next
    End With
End Sub

Thanks./.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,375
Messages
6,119,165
Members
448,870
Latest member
max_pedreira

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