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 .
 
I mean summing values in th column BUYING & SELLING for the same duplicated item in column B like this
for instance see th row2,3 how merge values for duplicated items
OUTPUT.xlsx
ABCDE
1ITEMIDBRANDBUYINGSELLING
21CCR1-CMB1C-BM-100353109
32CCR1-CMB2C-BM-101135612
43CCR1-CMB3C-BM-102124
54CCR1-CMB4C-BM-1031232
65CCR1-CMB5C-BM-104443
76CCR1-CMB6C-BM-105554
87CCR1-CMB7C-BM-106132
98CCR1-CMB8C-BM-1071541400
109CCR1-CMB9C-BM-1088812
113CCR1-CMB13C-BM-1123454
124CCR1-CMB14C-BM-113456
135CCR1-CMB15C-BM-11412334
146CCR1-CMB16C-BM-115567876
RP
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Well I understood correctly. This should work

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(1, j)) Then
                  dic(a(1, j)) = Array(a(0, j), a(1, j), a(2, j), a(3, j), Val(a(4, j) & "_"))
               Else
                 x = dic(a(1, j))
                 x(3) = x(3) + a(3, j)
                 x(4) = x(4) + Val(a(4, j) & "_")
                 dic(a(1, 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
Solution
great ! just can you fix autonumbering in column ITEM 1,2,3 after merging ,please?
 
Upvote 0
Yes, use the array like this

VBA Code:
Array(dic.Count + 1, a(1, j), a(2, j), a(3, j), Val(a(4, j) & "_"))
 
Upvote 0
I really appreciate for your time and solution (y)
have a great weekend !;)
 
Upvote 0
Hi Jec
sorry ! suddenly, the code shows error in this line
VBA Code:
.Open x02, x01
it informe me the sheet name rp is not correct despite of I make sure many times and using copy from your code and paste in the others files untile match the same sheets names without any spaces containing .
the headers and the sheets names are the same thing without any doubt .
this causes big headache , any solution for this dilemma,please?
if you need upload the files to see the problem I will that .
BTW your first code will brings the data and shows error and the second code doesn't brings data and shows error .
I hope helping me to find what's the problem.
 
Upvote 0
again sorry Jec !!! this is my bad !
main file should be out the folder when run the macro, I make mistake by make it inside the folder.
thanks
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,287
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