Hi all,
I hope some of you experts can help me with this. I am trying to put together some VBA code to automatically select a source workbook from a Teams (Sharepoint) folder based on a couple of variables (folder name and filename suffix) that are provided within the cells of my destination worksheet. I would then like to use that source workbook to copy some of its cells into my destination worksheet.
In the example below, I would like to automatically find and activate the workbook named "Randomname_dp1"(based on cell E892) in the folder "January Data" (based on cell C892). Column H would be the destination range.
I am very new to VBA so with a bit of tweaking of other people's code I have just managed to open a dialog box at a specific Sharepoint location from where I can navigate to the desired workbook and copy its data:
Is there any way to have the macro automatically "navigate" the Data folder for me and find the correct workbook? And would it be possible to make the macro references relative, so that it can find different folders/files based on the cell from which it is run (e.g. via a button placed in cell B892 above)? A big bonus would be if the source workbook did not need to be opened to copy its data (so I don't have to suppress screen updating).
Thank you so much for your input!
I hope some of you experts can help me with this. I am trying to put together some VBA code to automatically select a source workbook from a Teams (Sharepoint) folder based on a couple of variables (folder name and filename suffix) that are provided within the cells of my destination worksheet. I would then like to use that source workbook to copy some of its cells into my destination worksheet.
In the example below, I would like to automatically find and activate the workbook named "Randomname_dp1"(based on cell E892) in the folder "January Data" (based on cell C892). Column H would be the destination range.
I am very new to VBA so with a bit of tweaking of other people's code I have just managed to open a dialog box at a specific Sharepoint location from where I can navigate to the desired workbook and copy its data:
VBA Code:
Sub ImportResults()
Application.ScreenUpdating = False
Dim strFile As String, wb As Workbook
'Open the File Dialog
With Application.FileDialog(3)
.AllowMultiSelect = False
.InitialFileName = "https://sharepoint.com/sites/Group/Shared%20Documents/My%20Team/Data/"
'Show the dialog box
If .Show Then
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
'open the file
Set wb = Workbooks.Open(fullpath)
End If
If wb Is Nothing Then Exit Sub
'Copy ranges from selected item into current worksheet
wb.Sheets(1).Range("F21:F116").Copy
ThisWorkbook.ActiveSheet.Activate
ActiveSheet.Range("A1:A96").PasteSpecial (xlPasteValues)
wb.Close False
End With
Application.ScreenUpdating = True
End Sub
Is there any way to have the macro automatically "navigate" the Data folder for me and find the correct workbook? And would it be possible to make the macro references relative, so that it can find different folders/files based on the cell from which it is run (e.g. via a button placed in cell B892 above)? A big bonus would be if the source workbook did not need to be opened to copy its data (so I don't have to suppress screen updating).
Thank you so much for your input!