![]() |
|
|
|
#1
|
|||
|
|||
|
Hello all,
I have made following code that loops through each file in a folder I select. when the loop finds a file contains "*NoTrans.xls" a filename can be example: test_en_NoTrans.xls I have a filter that removes "*NoTrans.xls" so result: test_en.xls I need to to make a function to open the file also merging the file into the file I looping with test_en_noTrans.xls Could someone help me? See code below: Code:
Sub files()
Application.DisplayAlerts = False
Dim wb As Workbook
Dim MyPath As String
Dim MyFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
MyPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
MyPath = MyPath
If MyPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*NoTrans.xls"
'Target Path with Ending Extention
MyFile = Dir(MyPath & myExtension)
'Loop through each Excel file in folder
Do While MyFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=MyPath & MyFile)
'gör något
MyFileNoTransOpen = ActiveWorkbook.Name
myActFilePath = Application.ActiveWorkbook.Path
myOpenNoTrans = Left(MyFileNoTransOpen, (InStrRev(MyFileNoTransOpen, ".", -1, vbTextCompare) - 9))
myLangFile = myOpenNoTrans & ".xls"
MsgBox myOpenNoTrans
MsgBox myLangFile
MsgBox myActFilePath
' MsgBox ActiveWorkbook.Name
'Save and Close Workbook
wb.Close savechanges:=True
'Get next file name
MyFile = Dir
Loop
'Message Box when tasks are completed
' MsgBox "Nu är alla celler på Rad 1 Dolda!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
|
|
| Tags |
| loop, merge |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Merge files if filenames match
|
dapeamel | Excel | 1 | 03-12-2015 10:21 AM |
merge multiple files
|
shmu | Word | 1 | 01-05-2015 02:34 PM |
Insert images with incrementallly-numbered filenames
|
mqx | Word VBA | 4 | 11-13-2013 11:22 AM |
| Sequential Default Filenames | dude10321 | Word | 0 | 08-05-2011 02:54 PM |
| Color code filenames in explorer | terence_laoshi | Office | 0 | 01-18-2011 11:01 PM |