2013-05-07 74 views
0

有人可以幫助我在Excel宏嗎?Excel宏選擇2個特定單詞

我需要2個字是我之前定義並刪除它之間進行選擇..

例如:
「HTTP:!/ ertwertw4r%+ 53445433333/cat.jpgThere是狗」

,我需要選擇從文本的「http:」直到「.JPG」,並刪除或更換爲‘’,因爲我只需要‘有狗’的話...

回答

0

嘗試這個:

Dim firstStr As String 
Dim secondStr As String 
Dim Str As String 

Dim pos1 As Integer 
Dim pos2 As Integer 

firstStr = "http:" 
secondStr = ".jpg" 


For i = 1 To 500 

    Str = Cells(i, 1) ' here i represents row, and 1 means first column 

    pos1 = InStr(UCase(Str), UCase(firstStr)) 
    pos2 = InStr(UCase(Str), UCase(secondStr)) 

    If pos1 = 0 Or pos2 = 0 Then 
     ' MsgBox "Something goes wrong" 
    Else 
     StringToDelete = Mid(Str, pos1, pos2 - pos1 + Len(secondStr)) 
     finalString = Replace(Str, StringToDelete, "") 
     ' MsgBox finalString 
     Cells(i, 1) = finalString 
    End If 

Next i 
+0

似乎是好的,你的,唯一的問題,我有大約500行與這個開始,並與此結束......並與另一個包含...我需要搜索所有行開始和結束與這個詞並刪除它...我有一列500條記錄(其鏈接,我不需要,我只需要在最後的評論),所以我需要分開他們...我嘗試搜索和替換,但所有的500是其他...只有開始和結束相同... 它可能做出某種程度? – Pater 2013-05-07 11:51:10

+0

我的意思是這樣的相似(此作品在MS Word中 - 只選擇 - 而不是在Excel中): 子宏() Selection.Find.ClearFormatting 隨着Selection.Find 。文本= 「HTTP:」 。 Replacement.Text = 「」 的.forward =真 .Wrap = wdFindContinue 尾隨着 Selection.Find.Execute Selection.Extend Selection.Find.ClearFormatting 隨着Selection.Find 。文本= 「.JPG」 。 Replacement.Text =「」 .Forward = True .Wrap = wdFindContinue End With Selection.Find.Execute End Sub – Pater 2013-05-07 12:21:22

+0

這可以通過FOR循環和Cells功能輕鬆完成。我已經更新了我的回答 – 2013-05-07 15:36:28