搜索特定字符串VBA Excel / PPT

时间:2014-08-20 22:58:36

标签: excel vba excel-vba replace

使用附加的代码,如果我有一个名为“Haunting,”的文件名(jpg),我怎么能得到它(因为它正在循环)以寻找一个名为“The Haunting”或“The,Haunting?”的文件。

这是用于填充PPT的Excel,我正在尝试加载图像,同时不更改实际源文件夹的文件结构。

非常感谢

如下所示,我有一个特殊字符的替换,然后它使用convertedstring来获取图像名称

                        convertedstring = originalstring
                        For Each char In Split(SpecialCharacters, ",")
                            convertedstring = Replace(convertedstring, char, " ")
                        Next

        For Each oPPtShp In pptSlide.Shapes.Placeholders

            ' Run the Error handler "ErrHandler" when an error occurs.
            Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},:,."

            Dim originalstring  As String
            Dim convertedstring As String

            On Error Resume Next

            '~~> Only need to work on Picture place holders
            If oPPtShp.PlaceholderFormat.Type = ppPlaceholderPicture Then
                With oPPtShp
                    oPPtShpName = oPPtShp.Name
                    pptSlide.Shapes(oPPtShpName).Select

                    If oPPtShp.Name = oPPtShpName And Imagenum = 1 Then paths = "C:\"
                    If oPPtShp.Name = oPPtShpName And Imagenum = 2 Then paths = "C:\"
                    If oPPtShp.Name = oPPtShpName And Imagenum = 3 Then paths = "C:\"

                    If oPPtShp.Name = oPPtShpName And Imagenum = 1 Or oPPtShp.Name = oPPtShpName And Imagenum = 2 Then originalstring = objWorkbook.Worksheets(1).Cells(i, 2).Value
                    convertedstring = originalstring
                    For Each char In Split(SpecialCharacters, ",")
                        convertedstring = Replace(convertedstring, char, " ")
                    Next


                    If oPPtShp.Name = oPPtShpName _
                    And Imagenum = 1 Then
                        pptSlide.Shapes.AddPicture _
                            paths & convertedstring & ".jpg", _
                            msoFalse, msoTrue, .Left, .Top, .Width, .Height
                    ElseIf oPPtShp.Name = oPPtShpName _
                    And Imagenum = 2 Then
                        pptSlide.Shapes.AddPicture _
                            paths & convertedstring & " - Copy" & ".jpg", _
                            msoFalse, msoTrue, .Left, .Top, .Width, .Height
                    ElseIf oPPtShp.Name = oPPtShpName _
                    And Imagenum = 3 Then
                        pptSlide.Shapes.AddPicture _
                            paths & convertedstring & " - Copy (2)" & ".png", _
                            msoFalse, msoTrue, .Left, .Top, .Width, .Height

                    End
                    DoEvents

                End With
                Imagenum = Imagenum + 1
            End If
        Next

1 个答案:

答案 0 :(得分:0)

这是一次尝试。

如果originalstring = "Haunting, The"

以下内容将导致The Haunting

Sub test()
    Dim originalstring As String
    Dim convertedstring As String
    Dim iCommaLocation As Integer

    iCommaLocation = InStr(1, originalstring, ",")
    originalstring = "Haunting, The"
    convertedstring = Right(originalstring, Len(originalstring) _
                                            - iCommaLocation _
                                            - 1) _
                      & " " _
                      & Left(originalstring, iCommaLocation _
                                             - 1)
End Sub

然后,您可以同时查找originalstringconvertedstring

但是,如果字符串中有多个逗号,则无效。