Snabelhund
New Member
- Joined
- Nov 11, 2021
- Messages
- 20
- Office Version
- 2016
- Platform
- Windows
Hello, i have a macro that runs fine as long as i run it from a standard module. However i need to run it on several computers and tried to create an add-in. However i have problems when referencing workbooks in the code when run as add-in
Basically what the code is supposed to do is let the user select a folder that contains several workbooks, copy all the workbboks as sheets into the workbook where the code is run.
Altough i it does noot seem to work as i have a reference to "activeworkbook" which when run from a add in refereces the workbook where the same workbook that it copies from (copies the content into the next workbook that it opens instead of into the workbook where the code is run . I have tried to change it to this workbook but in that case it references the workbook where the add-in is stored instead of the actual open workbook from where the add-in code is run
Please comment if the question is unclear., to sumnmarize what i want to do i create a code that merges several workbooks into one.
See code with comments
Basically what the code is supposed to do is let the user select a folder that contains several workbooks, copy all the workbboks as sheets into the workbook where the code is run.
Altough i it does noot seem to work as i have a reference to "activeworkbook" which when run from a add in refereces the workbook where the same workbook that it copies from (copies the content into the next workbook that it opens instead of into the workbook where the code is run . I have tried to change it to this workbook but in that case it references the workbook where the add-in is stored instead of the actual open workbook from where the add-in code is run
Please comment if the question is unclear., to sumnmarize what i want to do i create a code that merges several workbooks into one.
See code with comments
VBA Code:
Sub CombineFiles()
Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Dim fullpath As Variant
With Application.FileDialog(msoFileDialogFolderPicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
'.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
End With
Application.EnableEvents = False
Application.ScreenUpdating = False
Path = fullpath
FileName = Dir(Path & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
For Each WS In Wkb.Worksheets
WS.Copy After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
Next WS
Wkb.Close False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub