gpt4 book ai didi

excel - 我是 VBA 新手,所以我不知道如何删除对象的文件扩展名

转载 作者:行者123 更新时间:2023-12-04 07:51:40 25 4
gpt4 key购买 nike

如何删除我输入的对象的文件扩展名,所以“A”列只显示没有扩展名的文件名。

Sub AddOlEObject()
Dim mainWorkBook As Workbook

Set mainWorkBook = ActiveWorkbook
Sheets("Object").Activate
Folderpath = "C:\Users\Sumit Jain\Pictures"
Set fso = CreateObject("Scripting.FileSystemObject")
NoOfFiles = fso.GetFolder(Folderpath).Files.Count
Set listfiles = fso.GetFolder(Folderpath).Files
For Each fls In listfiles
strCompFilePath = Folderpath & "\" & Trim(fls.Name)
If strCompFilePath <> "" Then
If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
counter = counter + 1
Sheets("Object").Range("A" & counter).Value = fls.Name
Sheets("Object").Range("B" & counter).ColumnWidth = 25
Sheets("Object").Range("B" & counter).RowHeight = 100
Sheets("Object").Range("B" & counter).Activate
Call insert(strCompFilePath, counter)
Sheets("Object").Activate
End If
End If
Next
mainWorkBook.Save
End Sub

Function insert(PicPath, counter)
‘MsgBox PicPath
With ActiveSheet.Pictures.insert(PicPath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 50
.Height = 70
End With
.Left = ActiveSheet.Range("B" & counter).Left
.Top = ActiveSheet.Range("B" & counter).Top
.Placement = 1
.PrintObject = True
End With
End Function

谢谢你的帮助
(对不起,我的英语不好)

最佳答案

使用 InStrRev function它从字符串的末尾返回一个字符串在另一个字符串中出现的位置。所以我们寻找.从字符串的右侧开始,然后获取不带扩展名的文件名。
这是你想要的吗?

Sheets("Object").Range("A" & counter).Value = _
Left(fls.Name, (InStrRev(fls.Name, ".", -1, vbTextCompare) - 1))

关于excel - 我是 VBA 新手,所以我不知道如何删除对象的文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66938202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com