Hi everyone, I have the below code that adds a new sheet to all of my files from subfolders. it works as expected, except when I call a function that I created.
My idea with the function is to pick a folder where some images are saved, and compare them with the name of my workbooks in subfolders. If inside the name of the image is a text that can be found also in the name of the Excel file, I would like to attach that image to that excel file. That is basically my condition. But I can't seem to get a working code. This is what I have so far:
Can anyone help?
VBA Code:
Sub MasterMac()
Dim MyFolder As String
Dim myFile As String n
Dim wbk As Workbook
Dim FSO As New FileSystemObject
Dim ParentFolder As Object, Sub_Folder As Object
Dim FldrPicker As FileDialog
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\"
End With
For Each Sub_Folder In FSO.GetFolder(MyFolder).SubFolders
myFile = Dir(MyFolder & Sub_Folder.Name & "\")
Do While myFile <> ""
If myFile Like "*ABCD*" Then
Set wbk = Workbooks.Open(Filename:=MyFolder & Sub_Folder.Name & "\" & myFile)
With wbk
Sheets.Add.Name = "NewSheetABCD25"
End With
Call PicInsert
wbk.Close savechanges:=True
End If
myFile = Dir
Loop
Next Sub_Folder
End Sub
My idea with the function is to pick a folder where some images are saved, and compare them with the name of my workbooks in subfolders. If inside the name of the image is a text that can be found also in the name of the Excel file, I would like to attach that image to that excel file. That is basically my condition. But I can't seem to get a working code. This is what I have so far:
VBA Code:
Public Function PicInsert(Optional row As Integer, Optional column As Integer)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim ws As Worksheet
Dim NewFolder As String
Dim myFile As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Function
End If
NewFolder = .SelectedItems(1) & "\"
End With
myFile = Dir(NewFolder & "\" & "*.jp*")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewFolder)
i = 1
For Each objFile In objFolder.Files
If InStr(1, objFile.Name, "jpg", vbTextCompare) Like "*ABD*" _
And InStr(1, objFile.Name, "jpg", vbTextCompare) Like ActiveWorkbook.Name Then
With ActiveSheet.Pictures.Insert(objFile.path)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 5
.Height = 15
End With
.Left = ActiveSheet.Cells(row, column).Left
.Top = ActiveSheet.Cells(row, column).Top
.Placement = 1 'locks the picture to a cell
End With
End If
i = i + 1
Next objFile
myFile = Dir
End Function
Can anyone help?