Hi Everyone,
I am struggling to create a macro which will import all files from predefined folder having same prefilx. Kindly refer below for more details:
C2 = ShareDrive link in which i have files
Files Name = Sample 1.pdf, Sample 2.pdf, Sample 3.pdf
I have below code by which i can upload predefined files into the excel as object:
Sub Button2_Click()
Dim strPath As String
Dim strFilename As String
Dim strCaption As String
Dim wksTarget As Worksheet
Dim rngTarget As Range
Dim wFiles As Variant
Dim wCells As Variant
Dim i As Long
Set wksTarget = Worksheets("Sheet1")
strPath = wksTarget.Range("C2").Value
If Right(strPath, 1) <> "" Then
strPath = strPath & ""
End If
wFiles = Array("sample 1.pdf", "sample 2.pdf", "sample 3.pdf")
wCells = Array("E7", "F7", "G7")
For i = LBound(wFiles) To UBound(wFiles)
strFilename = wFiles(i)
If Len(Dir(strPath & strFilename, vbNormal)) = 0 Then
MsgBox "'" & strPath & strFilename & "' does not exist!", vbExclamation, "Path and/or file?"
Else
strCaption = strFilename '"myCaption" 'change the caption as desired
Set rngTarget = wksTarget.Range(wCells(i))
wksTarget.OLEObjects.Add _
Filename:=strPath & strFilename, _
link:=False, _
displayasicon:=True, _
iconfilename:="", _
iconindex:=0, _
iconlabel:=strCaption, _
Left:=rngTarget.Left, _
Top:=rngTarget.Top, _
Width:=150, _
Height:=10
End If
Next
MsgBox "End"
End Sub
My query: i want to amend this code and make it possible that, if i click on button all files starts with "Sample" will get upload into the excel and there location would be E7...E9...E11 so on.
Please help me and let me know if there is any questions on it.
I am struggling to create a macro which will import all files from predefined folder having same prefilx. Kindly refer below for more details:
C2 = ShareDrive link in which i have files
Files Name = Sample 1.pdf, Sample 2.pdf, Sample 3.pdf
I have below code by which i can upload predefined files into the excel as object:
Sub Button2_Click()
Dim strPath As String
Dim strFilename As String
Dim strCaption As String
Dim wksTarget As Worksheet
Dim rngTarget As Range
Dim wFiles As Variant
Dim wCells As Variant
Dim i As Long
Set wksTarget = Worksheets("Sheet1")
strPath = wksTarget.Range("C2").Value
If Right(strPath, 1) <> "" Then
strPath = strPath & ""
End If
wFiles = Array("sample 1.pdf", "sample 2.pdf", "sample 3.pdf")
wCells = Array("E7", "F7", "G7")
For i = LBound(wFiles) To UBound(wFiles)
strFilename = wFiles(i)
If Len(Dir(strPath & strFilename, vbNormal)) = 0 Then
MsgBox "'" & strPath & strFilename & "' does not exist!", vbExclamation, "Path and/or file?"
Else
strCaption = strFilename '"myCaption" 'change the caption as desired
Set rngTarget = wksTarget.Range(wCells(i))
wksTarget.OLEObjects.Add _
Filename:=strPath & strFilename, _
link:=False, _
displayasicon:=True, _
iconfilename:="", _
iconindex:=0, _
iconlabel:=strCaption, _
Left:=rngTarget.Left, _
Top:=rngTarget.Top, _
Width:=150, _
Height:=10
End If
Next
MsgBox "End"
End Sub
My query: i want to amend this code and make it possible that, if i click on button all files starts with "Sample" will get upload into the excel and there location would be E7...E9...E11 so on.
Please help me and let me know if there is any questions on it.