2013-06-02 157 views
0

我想查找「#」字符,將其移除並在其後標上2個字符。 I`ve得到這個代碼:Excel/vba查找字符,替換後再替換爲兩個字符

pos = InStr(mycell.Value, "#") 
    If pos > 0 Then 
     mycell.Replace What:="#", Replacement:="" 
     mycell.Characters(Start:=pos + 1, Length:=2).Font.Superscript = True 
    End If 

當我只能更換 - 工程。當我只製作一些字體上標 - 作品。當兩者 - 只有替換。

+0

什麼版本的Excel?此外,你有沒有嘗試過'mycell.Characters(開始:= pos,長度:= 2).Font.Superscript = True'('pos'而不是'pos' + 1)? – LittleBobbyTables

+1

@LittleBobbyTables是正確的 - 在用'「替換'#'後,你想要上標的字符位於'pos'和'pos + 1'。如果它們是字符串中的最後兩個字符,那麼可能會出現「無聲錯誤」,並且沒有任何反應。 – Floris

回答

1

pos + 1需要pos,因爲您已經使用Replace方法刪除了一個字符。經過測試,似乎在Excel 2010中工作正常。

pos = InStr(myCell.Value, "#") 
If pos > 0 Then 
    myCell.Replace What:="#", Replacement:="" 
    myCell.Characters(Start:=pos, Length:=2).Font.Superscript = True 
End If 
+0

恩,Excel 2010仍然無法正常工作。你能把你的文件發給我嗎?我想測試它 – Button

+0

如何向您發送文件? – nemmy