vbareplace与可变的来源

必须用特定的新链接replace超过800个长的html代码中的几个不同的链接。 得到一个Excel表单,列A中的旧链接和列B中的新链接。我的意图是创build一个用input字段和输出字段的用户表单。 我的目标是用一个特定的新的旧的链接取代旧的链接。 我可以用一个单一的链接,而不是在大约350个不同的列表。 我有

Private Sub CommandButton1_Click() Dim varInput As String Dim varOutput As String varInput = tbInput varOutput = Replace(varInput, "oldLink.intern/a1b2c3", "newLink.intern/z9y8x7") tbOutput = varOutput End 

其中“tbInput”是我的第一个文本框,“tbOutput”是我的第二个,但不能与我的链接列表

使用combobox会给你自动完成的好处。

在这里输入图像说明


 Option Explicit Const OLDLINK_ROOT As String = "oldLink.intern/" Const NEWLINK_ROOT As String = "newLink.intern/" Private Sub cboOldLink_Change() With cboOldLink If .ListIndex = -1 Then txtNewLink.Value = "" Else txtNewLink.Value = NEWLINK_ROOT & .list(.ListIndex, 1) End If End With End Sub Private Sub UserForm_Initialize() Dim Column1Max As Long, Column2Max As Long, NewListWidth As Long With cboOldLink .ColumnCount = 2 .list = getLinkList(Column1Max, Column2Max) .ColumnWidths = (Column1Max * .Font.Size + 1) & " pt;" & (Column2Max * .Font.Size + 1) & " pt;" NewListWidth = (Column1Max * .Font.Size + 1) + (Column2Max * .Font.Size + 1) If NewListWidth > .ListWidth Then .ListWidth = NewListWidth End With End Sub Function getLinkList(ByRef Column1Max As Long, ByRef Column2Max As Long) As Variant Dim cell As Range Dim olist As Object Dim key As String, item As String Dim list As Variant Dim x As Long Set olist = CreateObject("System.Collections.SortedList") With Worksheets("Sheet1") For Each cell In .Range("A2", .Range("A" & .Rows.Count).End(xlUp)) key = Right(cell.Value, Len(cell.Value) - Len(OLDLINK_ROOT)) item = Right(cell.Offset(0, 1).Value, Len(cell.Offset(0, 1).Value) - Len(OLDLINK_ROOT)) If Not olist.Contains(key) Then olist.Add key, item Next End With ReDim list(0 To olist.Count - 1, 0 To 1) For x = 0 To olist.Count - 1 list(x, 0) = olist.GetKey(x) list(x, 1) = olist.GetByIndex(x) If Len(olist.GetKey(x)) > Column1Max Then Column1Max = Len(olist.GetKey(x)) If Len(olist.GetByIndex(x)) > Column2Max Then Column2Max = Len(olist.GetByIndex(x)) Next getLinkList = list End Function 

好吧,就这么简单:

 Private Sub cbMach_Click() Dim varIn As String Dim varOut As String Dim i As Integer varIn = tbIn For i = 1 To 500 tbIn = Replace(tbIn, Cells(i, 1), Cells(i, 2)) Next tbOut = tbIn End Sub 

太瞎了看。 多谢你们!