[英]VBA Excel code to check if certain text is found in selection, then change cell text
I am working in the pricing department, I made a macro to change the part number into a hyperlink using this code 我在价格部门工作,我做了一个宏,使用此代码将零件号更改为超链接
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
Now, I am trying to test if the cell content starts with a certain prefix, if it does I wanna add - after it, I know it is done using this 现在,我正在尝试测试单元格内容是否以某个前缀开头,是否要添加-在此之后,我知道已使用此前缀完成了
If InStr(1, celltxt, "TK") Then
but not sure how to run it, so it would cover my selection and not one cell. 但不知道如何运行它,因此它将涵盖我的选择而不是一个单元。
I think you should make the last If statement to select the cells that make that Match. 我认为您应该在最后一个If语句中选择进行匹配的单元格。 So the matching cells will take the hyperlink substitution and the none matching doesn't. 因此,匹配的单元格将进行超链接替换,而没有匹配项将不进行替换。
If xCell.value <> "" and InStr(1, cellTxt, "TK") > 0 Then
{...}
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.