(Mod edit as previous title was : Please Help ASAP !!!!)
I have consolidated 2 files(A.xsl & B.xsl) into master file(Master.xsl).Now I want only selected columns to be copied from my both xsl files into master file.
Column 1 :File Name -Should be the name of file (A/B)
Column 2 : Gender/Value 1 - If my gender is Male in A.xsl/B.xsl then it will copy value 1
else it will copy gender value
Column 3 : Value 2
Column 4 : Region for particular field
In the below code I am getting entire row copied but I want only above columns to be copied in my master file with the condition.
I have filtered my data with country specific .I will really appreciate if someone can help me.
I have consolidated 2 files(A.xsl & B.xsl) into master file(Master.xsl).Now I want only selected columns to be copied from my both xsl files into master file.
Column 1 :File Name -Should be the name of file (A/B)
Column 2 : Gender/Value 1 - If my gender is Male in A.xsl/B.xsl then it will copy value 1
else it will copy gender value
Column 3 : Value 2
Column 4 : Region for particular field
In the below code I am getting entire row copied but I want only above columns to be copied in my master file with the condition.
I have filtered my data with country specific .I will really appreciate if someone can help me.
Code:
Option Explicit
'Test
'Description: Combines all files in a folder to a master file.
Sub MergeFiles()
Dim path As String, ThisWB As String, lngFilecounter As Long
Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
Dim Filename As String, Wkb As Workbook
Dim CopyRng As Range, Dest As Range
Dim RowofCopySheet As Integer
RowofCopySheet = 2 ' Row to start on in the sheets you are copying from
ThisWB = ActiveWorkbook.Name
path = "C:\Users\\Consolidate\"
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shtDest = ActiveWorkbook.Sheets(1)
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
If Filename = "Master.xlsm" Then
Exit Sub
End If
If Not Filename = ThisWB Then
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
Debug.Print Filename
'********If Condition
Select Case Filename
Case "A.xlsx"
ActiveSheet.Range("A1").Autofilter Field:=1, Criteria1:="Brazil"
Case "B.xlsx"
ActiveSheet.Range("A1").Autofilter Field:=1, Criteria1:="United Kingdom"
End Select
Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
CopyRng.Copy Dest
Wkb.Close False
End If
Filename = Dir()
Loop
Range("A1").Select
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
Attachments
Last edited by a moderator: