繁体   English   中英

仅在文本框中替换字符串部分vba excel

[英]replace string part only in textbox vba excel

大家好,我只是想问一下是否可以替换或删除文本框中的一部分字符串? 而不是替换或删除整个字符串,它只会删除其中的一部分。 该程序将如下所示。

  1. 用户将在文本框示例abc123cd中键入字符串或文本
  2. 在excel中,单元格的默认值为abc和cd。 假设abc在A1中,而cd在A2中。
  3. 然后,A1和A2中的默认值将删除abc123cd中的abc和cd。 在同一文本框中,输出为123。 这意味着在abc123cd之前,单击commnad按钮后,结果将仅在同一文本框中为123。

下面是我的示例vba程序。 但是很显然,我找不到适合自己需求的解决方法。 希望有人可以提供帮助。 谢谢。

Private Sub CommandButton1_Click()
    Dim myLastRow As Long
    Dim myRow As Long
    Dim myFind As String
    Dim myReplace1 As String
    Dim myReplace2 As String
    Dim sExportFolder, sFN
    Dim rArticleName As Range
    Dim rDisclaimer As Range
    Dim oSh As Worksheet
    Dim oFS As Object
    Dim oTxt As Object

    'Specify name of Sheet with list of replacements
    Set myReplaceSheet = Sheets("Sheet2")

    'Assuming list of replacement start in column A on row 2, find last entry in list
    myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row

    Application.ScreenUpdating = False

    'Loop through all list of replacments
    For myRow = 2 To myLastRow
        'Get find and replace values (from columns A and B)
        myFind = myReplaceSheet.Cells(myRow, "A")
        myReplace1 = myReplaceSheet.Cells(myRow, "B")

        'Start at top of data sheet and do replacements
        myDataSheet.Activate
        Range("A2").Select

        'Ignore errors that result from finding no matches
        On Error Resume Next

        'Do all replacements 
        With TextBox1
            .Replace What:=myFind, Replacement:=myReplace1, LookAt:=xlPart, _
              SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
              ReplaceFormat:=False
        End With
    Next myRow
End Sub

仅更改文本框内的值如何? 例如:

TextBox1.Value = VBA.Replace(TextBox1.Value, myFind, myReplace1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM