VBA Excel代码来检查是否在select中find某些文本,然后更改单元格文本

我在定价部门工作,我做了一个macros,使用这个代码将零件编号改成超链接

Private Sub CommandButton1_Click() Dim userResponce As Range On Error Resume Next Set userResponce = Application.InputBox("select a range with the mouse", Default: = Selection.Address, Type: = 8) On Error GoTo 0 If userResponce Is Nothing Then MsgBox "Cancel clicked" Else MsgBox "You selected " & userResponce.Address End If For Each xCell In userResponce If xCell.Value < > "" Then xCell.NumberFormat = "@" xCell.Value = Trim(xCell.Value) URL = "~pricer.cfm?part_number=" & xCell.Value & "&buyQty=0&buyUofm=1" ActiveSheet.Hyperlinks.Add Anchor: = xCell, Address: = URL, TextToDisplay: = xCell.Value End If Next xCell 

现在,我试图testing单元格内容是否以某个前缀开头,如果它是我想添加的 – 之后,我知道这是使用这个

 If InStr(1, celltxt, "TK") Then 

但不知道如何运行它,所以它会覆盖我的select,而不是一个单元格。

我想你应该使最后的If语句来select进行匹配的单元格。 所以匹配的单元格会进行超链接replace,而没有匹配的单元格则不会。

 If xCell.value <> "" and InStr(1, cellTxt, "TK") > 0 Then {...} End If