2015-12-28 66 views
0

我想使用修剪或Ltrim來更改我的選擇。我有一個很長的If-Then列表。以下是一個簡短示例Word VBA修剪功能

With Selection 
If .Text = "Since" or .Text = "Since " Then .TypeText Text:= "Because" 
If .Text = "since" or .Text = "since " Then .TypeText Text:= "because" 
End With 

如何在if-then語句前修整我的選擇?另外,對於上述示例,是否有更簡單的方法來進行適當的區分大小寫更改?

+0

應該是UCase(Trim(.Text))=「SINCE」 – Sorceri

回答

1

對於切邊部:

If Trim(.Text) = "Since" Then .TypeText Text:= "Because" 
If Trim(.Text) = "since" Then .TypeText Text:= "because" 

對於大寫字母部分:

也許你可以重新格式化每對IF-THEN使用功能,像這樣:

CheckAndType("since", "because") 

然後:

Function CheckAndType(origin as String, typeText as String) 
    If Ucase(Trim(Selection.Text)) = UCase(Trim(origin)) Then 
     If StrComp(Left(Selection.Text, 1), UCase(Left(Selection.Text, 1)), vbBinaryCompare) = 0 Then 
      Selection.TypeText Text:=LCase(typeText) 
     Else 
      Selection.TypeText Text:= UCase(Left(typeText, 1)) & LCase(Right(typeText, Len(typeText) - 1) 
     End If 
    End If 
End Function 

未經測試的代碼。任何懷疑/澄清,只是問。

+0

這看起來好像會清理我的代碼很多。我只是不知道如何實現它。一旦創建函數,我在哪裏創建CheckAndType(「since」,「因爲」)(「as」,「因爲」)的列表 –

+0

謝謝。我制定了函數,現在我試圖弄清楚如何使用'Call CheckAndType'列表中的特定單詞來添加註釋(通過調用宏)'Call CheckAndType(「Since」,「Because 「) Application.Run MacroName:=」Normal.NewMacros.CommentRW5「'這不按預期工作。 –

+0

@WritePlace調用一個宏,你需要使用'Call NewMacros.CommentRW5' –