scecnic74uk
New Member
- Joined
- Aug 15, 2020
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
I have a problem with the code below - sometimes! it works for 3 or 4 times in a row then give me a runtime error.. I copied it from a retired member of my staff excel sheets and wanted to use it for up to 10 files to attach - he only used it for 1 file, here's my code...
VBA Code:
Sub AddSomething_Click()
Dim newFile As Variant
Dim fileCount As Integer
Dim offsetRow As Integer
Dim offsetCol As Integer
fileCount = Range("l7").Value
offsetCol = 1
If fileCount = 0 Then
Range("F25").Select
newFile = Application.GetOpenFilename("All Files,*.*", Title:="Find file to insert")
ActiveSheet.OLEObjects.Add Filename:=newFile, Link:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{90110409-6000-11D3-8CFE-0150048383C9}\xlicons.exe", IconIndex:=0, IconLabel:=newFile
fileCount = fileCount + 1
Range("l7").Select
ActiveCell.FormulaR1C1 = fileCount
newFile = Null
Exit Sub
End If
If fileCount > 0 Then
offsetRow = 0
offsetCol = 2
If fileCount >= 2 Then
offsetRow = 0
offsetCol = fileCount * 2
End If
If fileCount >= 5 Then
offsetRow = 5
offsetCol = 0
If fileCount = 6 Then
offsetCol = 2
End If
If fileCount > 6 Then
offsetCol = (fileCount - offsetRow) * 2
End If
If fileCount >= 10 Then
MsgBox "You can only add 10 files", vbInformation
Exit Sub
End If
End If
Range("F25").Select
ActiveCell.Offset(offsetRow, offsetCol).Range("a1").Select
newFile = Application.GetOpenFilename("All Files,*.*", Title:="Find file to insert")
ActiveSheet.OLEObjects.Add Filename:=newFile, Link:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{90110409-6000-11D3-8CFE-0150048383C9}\xlicons.exe", IconIndex:=0, IconLabel:=newFile
fileCount = fileCount + 1
Range("l7").Select
ActiveCell.FormulaR1C1 = fileCount
newFile = Null
Exit Sub
End If
End Sub
Last edited by a moderator: