簡體   English   中英

VBA Excel代碼檢查在選擇中是否找到某些文本,然后更改單元格文本

[英]VBA Excel code to check if certain text is found in selection, then change cell text

我在價格部門工作,我做了一個宏,使用此代碼將零件號更改為超鏈接

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

現在,我正在嘗試測試單元格內容是否以某個前綴開頭,是否要添加-在此之后,我知道已使用此前綴完成了

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

但不知道如何運行它,因此它將涵蓋我的選擇而不是一個單元。

我認為您應該在最后一個If語句中選擇進行匹配的單元格。 因此,匹配的單元格將進行超鏈接替換,而沒有匹配項將不進行替換。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM