Dears: i have an issue with below code as i need to Merge Two Files once importing them by filedialog then copy the result direclty to another workbook the below cope is working but suddnly it copies only data of the first file which its file size is the bigger and i do not know why
Sub IIII_Import_BSS_Stock_All_Files()
Dim ws As Worksheet
Dim wb As Workbook
Dim Imported1wb As Workbook
Dim Imported1ws As Worksheet
Dim DialFirstFile As FileDialog
Dim Imported1FileName As String
Dim Imported1LastRow As Long
Dim Imported2wb As Workbook
Dim Imported2ws As Worksheet
Dim DialSecondFile As FileDialog
Dim Imported2FileName As String
Dim Imported2LastRow As Long
'FileName after using File Len for ordering the first file to be (available) and the second one to be (Maintain)
Dim FileLenNameSize1 As Long
Dim FileLenNameSize2 As Long
Dim TempFileOrder As String
Dim UpperArray() As String
Dim LowerArray() As String
Dim split_len As Long
Dim Imported1FileNameLnSpFn As String
Dim Imported2FileNameLnSpFn As String
Dim DestinationRange As Range
Dim ImportSelectARange As Range
Dim ImportSelectBRange As Range
Const SelectCols As String = "D:D,A:A,C:C,H:H,K:K,I:I"
Application.ScreenUpdating = False
Set DialFirstFile = Application.FileDialog(msoFileDialogFilePicker)
DialFirstFile.AllowMultiSelect = False
DialFirstFile.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
DialFirstFile.Show
Imported1FileName = DialFirstFile.SelectedItems.Item(1)
need.
If InStr(Imported1FileName, ".xls") = 0 Then
Exit Sub
End If
Set Imported1wb = Workbooks.Open(Imported1FileName)
Application.ScreenUpdating = False
Set DialSecondFile = Application.FileDialog(msoFileDialogFilePicker)
DialSecondFile.AllowMultiSelect = False
DialSecondFile.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
DialSecondFile.Show
Imported2FileName = DialSecondFile.SelectedItems.Item(1)
If InStr(Imported2FileName, ".xls") = 0 Then
Exit Sub
End If
Set Imported2wb = Workbooks.Open(Imported2FileName)
Set ws = ActiveSheet
FileLenNameSize1 = FileLen(Imported1FileName)
FileLenNameSize2 = FileLen(Imported2FileName)
If (FileLenNameSize1 < FileLenNameSize2) Then
TempFileOrder = Imported1FileName
Imported1FileName = Imported2FileName
Imported2FileName = TempFileOrder
End If
UpperArray = Split(Imported1FileName, "\")
LowerArray = Split(Imported2FileName, "\")
split_len = UBound(UpperArray) - LBound(UpperArray)
Imported1FileNameLnSpFn = UpperArray(split_len)
Imported2FileNameLnSpFn = LowerArray(split_len)
Set Imported1ws = Workbooks(Imported1FileNameLnSpFn).Worksheets("Default")
Set Imported2ws = Workbooks(Imported2FileNameLnSpFn).Worksheets("Default")
Imported1LastRow = Imported1ws.Cells(Imported1ws.Rows.Count, "A").End(xlUp).Offset(1).Row
Imported2LastRow = Imported2ws.Cells(Imported2ws.Rows.Count, "A").End(xlUp).Row
'Copy & Paste to the total stock sheet from the merged file
' but only Copy Selection of Non Adjacent Columns of the imported file not copying the entire sheet
Set Imported1ws = Workbooks(Imported1FileNameLnSpFn).Worksheets("Default")
'Selection of data in sheet 2 to be copied
Set ImportSelectARange = Intersect(Imported1ws.Range(SelectCols), Imported1ws.Rows("2:" & Imported1LastRow))
Set ImportSelectBRange = Intersect(Imported2ws.Range(SelectCols), Imported2ws.Rows("2:" & Imported2LastRow))
'Selection of last empy row at Sheet 1 to be copy data into it
'Copy from sheet 2 to sheet 1
Set Destination2FRange = ThisWorkbook.Worksheets("Total Stock").Range("A2")
Set Imported1ws = Workbooks(Imported1FileNameLnSpFn).Worksheets("Default")
Imported2ws.Range("A2:L" & Imported2LastRow).Copy Destination:=Imported1ws.Cells(Imported1LastRow + 1, "A")
'Copy from sheet 1 to sheet 2
Set Imported1ws = Workbooks(Imported1FileNameLnSpFn).Worksheets("Default")
Imported2ws.Range("A2:L" & Imported2LastRow).Copy Destination:=Imported1ws.Cells(Imported1LastRow + 1, "A")
ImportSelectARange.Copy _
Destination:=Destination2FRange
Workbooks(Imported1FileNameLnSpFn).Close
Workbooks(Imported2FileNameLnSpFn).Close