简体   繁体   English

VBA Excel代码检查在选择中是否找到某些文本,然后更改单元格文本

[英]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.

相关问题 用于查找和更改单元格内文本子字符串格式的 Excel 代码的 VBA - VBA for Excel code to find and change formatting of substrings of text within a cell 用于比较两列中的文本字符串并突出显示某些文本字符串而不是整个单元格的 Excel VBA 代码? - Excel VBA code to compare text strings in two columns and highlight certain text strings not the whole cell? EXCEL:检查是否在另一列中找到单元格文本 - EXCEL: Check if cell text is found in another column 使用VBA Excel更改单元格中文本的颜色 - Change color in text from cell with VBA excel Excel VBA - 代码问题 - 删除或清除特定单元格不包含特定文本的整行的目标 - Excel VBA - Code Issue - Goal to Delete or Clear Entire Rows for which Specific Cell doesn't Certain Text VBA - Excel 在每个包含特定文本的单元格上方插入行 - VBA - Excel insert row above for each cell containing certain text Excel VBA:如果单元格包含某些文本,则输入具有该内容的单元格范围 - Excel VBA: If cell contains certain text then input range of cells with that content 选择范围,直到带有VBA的Excel中带有特定文本的单元格 - Select range until cell with certain text in Excel with VBA Excel VBA - 检查单元格是否包含文本 - Excel VBA - Check cell whether it contains piece of text VBA代码不允许用户选择带有特定文本的单元格 - VBA code to not allow user to select a cell with certain text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM