简体   繁体   English

从单元格中删除.pdf

[英]To remove .pdf from cell

here's the problem, I have done up the code but it does not seem to work.. tried both method also not possible str = Replace(str, ".pdf", Left(str, Len(str) - 4)) str = Replace(str, ".pdf", Left(str, Len(str) - 4)) 这是问题,我已经完成了代码,但似乎无法正常工作。尝试了两种方法也都不可能str = Replace(str, ".pdf", Left(str, Len(str) - 4)) str = Replace(str, ".pdf", Left(str, Len(str) - 4))

can anyone help? 有人可以帮忙吗? Thanks in advance =) 在此先感谢=)

Sub removepdfword()
Dim str As String
Dim row As Long
row = 1
str = Sheet1.Range("A" & CStr(row))


Do While Sheet1.Range("A" & CStr(row)) <> ""

If Right(str, 4) = ".pdf" Then
     str = Replace(str, ".pdf", Left(str, Len(str) - 4))
     'str = Left(str, Len(str) - 4)
     MsgBox str
Else
End If
row = row + 1
Loop

End Sub

@PatricK already told you what was wrong in your Replace() function usage @PatricK已经告诉您Replace()函数使用中出了什么问题

but you're also missing to explicitly reference Sheet1 worksheet in your ranges object, which then defaults to the currently active sheet (which may not be the one you need) 但是您还缺少在range对象中显式引用Sheet1工作表的信息,该工作表然后默认为当前活动的工作表(可能不是您需要的工作表)

finally you could avoid looping and call Replace() method of Range object an do that in one shot: 最后,您可以避免循环并调用Range对象的Replace()方法,一次完成一次操作:

With Sheet1 ' reference your sheet
    .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Replace what:=".pdf", replacement:="", lookat:=xlPart ' call 'Replace()' method on referenced sheet column A cells from row 1 down to last not empty one
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM