combining two codes to extract data instead of repeat the code twice

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
Hi experts
I need combining two code instead of repeat the code again for different ranges
so the code import data from Prompt user to browse & Select file from folder . I have two ranges for two files when select from folder
first
VBA Code:
 Sub ge_tdata()
 Dim ar As Variant, lRw As Long


 lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row

 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then
      With GetObject(.SelectedItems(1))
         ar = .Sheets("rp").Range("g2:i100").Value
        
         ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).Resize(UBound(ar), UBound(ar, 2)) = ar
        .Close 0
        End If
      End With
   End If
 End With
End Sub


second
VBA Code:
 Sub ge_tdata()
 Dim ar As Variant, lRw As Long


 lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row

 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then
      With GetObject(.SelectedItems(1))
         ar = .Sheets("rp").Range("A2:E100").Value
     
         ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).Resize(UBound(ar), UBound(ar, 2)) = ar
        .Close 0
        End If
      End With
   End If
 End With
End Sub
as you see there is different in ranges.
I have some notices if it's possible just select the folder and pull data from files without select files and when pull data should skip blank cells in ranges or make search for tha lastrow instead of specify the range every time .
I hope finding easy way to do that .
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Assuming that in both the cases the user select the same file, then:
VBA Code:
Sub ge_tBoth()
 Dim ar As Variant, lRw As Long

 lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row

 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then
      With GetObject(.SelectedItems(1))
         ar = .Sheets("rp").Range("g2:i100").Value
         ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).Resize(UBound(ar), UBound(ar, 2)) = ar
'second range:
         ar = .Sheets("rp").Range("A2:E100").Value
         lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row
         ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).Resize(UBound(ar), UBound(ar, 2)) = ar
        .Close 0
        End If
      End With
   End If
 End With
End Sub
 
Upvote 0
thanks anthony for your trying !
based on OP I have multiple files , if there is possible way to pull data by select folder contains files without open any file , just indicate to sheet name and range ,then the code will pull data from files are existed in folder . instead of select each file seperately and repeat the code because there are different ranges in location .
 
Upvote 0
This is much different than the first message, but much clearer. You should however clarify which is/are the ranges to pull ("g2:i100"? "A2:E100"? Both?) and if there is, in the data to be fetched, a column that can be used to determine which is the last one with data (supect it's A, but this is only a guess)
 
Upvote 0
This is much different than the first message
I no know what you say that despite I said
I have two ranges for two files when select from folder
and you said
Assuming that in both the cases the user select the same file

I didn't say the same file contains two different renges:confused:
You should however clarify which is/are the ranges to pull ("g2:i100"? "A2:E100"? Both?)
should pull data for both ranges with two files
and if there is, in the data to be fetched, a column that can be used to determine which is the last one with data (supect it's A, but this is only a guess)
actually I don't undrestand it, can you clarify,please?
 
Upvote 0
I'm sorry but I have to admit I'm more confused than before.
My suggestion is that you try again to explain the current situation giving the key information: where are the files to import (I understood that you would like to be able to choose the folder that contains them, and then import from each file is in the path); the structure of the sheet you need to import (an image could suffice); which range you need to import from the sheet.
Keep in mind that you know very well your problem, while we can only understand it from your words...
 
Upvote 0
FILE IMPORT-MM.XLSM
IMPORT-MM.xlsm
ABCDE
1ITEMIDBRANDBUYINGSELLING
21CCR1-CMB1C-BM-100233100
32CCR1-CMB2C-BM-101123
43CCR1-CMB3C-BM-102124
54CCR1-CMB4C-BM-1031232
65CCR1-CMB5C-BM-104443
76CCR1-CMB6C-BM-105554
87CCR1-CMB7C-BM-10666
98CCR1-CMB8C-BM-10777700
109CCR1-CMB9C-BM-1088812
RP


file REPORT.xlsx
REPORT.xlsx
GHIJK
1ITEMIDBRANDBUYINGSELLING
21CCR1-CMB1C-BM-1001209
32CCR1-CMB2C-BM-101123312
43CCR1-CMB13C-BM-1123454
54CCR1-CMB14C-BM-113456
65CCR1-CMB15C-BM-11412334
76CCR1-CMB16C-BM-115567876
87CCR1-CMB7C-BM-10666
98CCR1-CMB8C-BM-10777700
RP


the output should be
output.xlsm
ABCDE
1ITEMIDBRANDBUYINGSELLING
21CCR1-CMB1C-BM-100233100
32CCR1-CMB2C-BM-101123
43CCR1-CMB3C-BM-102124
54CCR1-CMB4C-BM-1031232
65CCR1-CMB5C-BM-104443
76CCR1-CMB6C-BM-105554
87CCR1-CMB7C-BM-10666
98CCR1-CMB8C-BM-10777700
109CCR1-CMB9C-BM-1088812
1110CCR1-CMB1C-BM-1001209
1211CCR1-CMB2C-BM-101123312
1312CCR1-CMB13C-BM-1123454
1413CCR1-CMB14C-BM-113456
1514CCR1-CMB15C-BM-11412334
1615CCR1-CMB16C-BM-115567876
1716CCR1-CMB7C-BM-10666
1817CCR1-CMB8C-BM-10777700
sheet1


and every time the run macro should clear data in file output from A2:E before brings data
 
Upvote 0
Hi, try to run this macro

VBA Code:
Sub jec()
 Dim ar, x00, x01, x02
 ar = Array("ITEM", "ID", "BRAND", "BUYING", "SELLING")
 x02 = "SELECT " & Join(ar, ", ") & " FROM `rp$`"
 With Application.FileDialog(4)
   If .Show Then
   
     With Sheet1.Cells(1)
        .CurrentRegion.ClearContents
        .Resize(, 5) = ar
     End With
     
     x00 = Dir(.SelectedItems(1) & "\")
     
     Do Until x00 = ""
       x01 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .SelectedItems(1) & "\" & x00 & ";Extended Properties=""Excel 12.0"""
       With CreateObject("ADODB.recordset")
         .Open x02, x01
          Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1).CopyFromRecordset .DataSource
         .Close
       End With
       x00 = Dir
     Loop
   End If
 End With
End Sub
 
Upvote 0
@JEC
wow ! this is awesome !!
just last thing if you don't mind . actually I have another macro to do that, but I truly appreciate that if you can add this procedure for your code , can I merge values for columns BUYING & SELLING based on column ID when pull data from folder please?
 
Upvote 0
It's not totally clear to me. But here a suggestion, adding a dictionary

VBA Code:
Sub jec()
 Dim Dic, ar, a, x, x00, x01, x02, j As Long
 ar = Array("ITEM", "ID", "BRAND", "BUYING", "SELLING")
 x02 = "SELECT " & Join(ar, ", ") & " FROM `rp$`"
 
 With Application.FileDialog(4)
   If .Show Then
 
     With Sheet1.Cells(1)
        .CurrentRegion.ClearContents
        .Resize(, 5) = ar
     End With
   
     x00 = Dir(.SelectedItems(1) & "\")
     Set dic = CreateObject("scripting.dictionary")
   
     Do Until x00 = ""
       x01 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .SelectedItems(1) & "\" & x00 & ";Extended Properties=""Excel 12.0"""
       With CreateObject("ADODB.recordset")
           .Open x02, x01
            a = .getrows
            For j = 0 To UBound(a, 2)
               If Not dic.exists(a(0, j) & "_") Then
                  dic(a(0, j) & "_") = Array(a(0, j), a(1, j), a(2, j), a(3, j), Val(a(4, j) & "_"))
               Else
                  x = dic(a(0, j) & "_")
                  x(3) = x(3) + a(3, j)
                  x(4) = x(4) + Val(a(4, j) & "_")
                  dic(a(0, j) & "_") = x
               End If
            Next
           .Close
       End With
       x00 = Dir
     Loop
     Sheets(1).Range("A2").Resize(dic.Count, 5) = Application.Index(dic.items, 0, 0)
   End If
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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