查找和替换

时间:2018-10-30 15:08:57

标签: excel vba excel-vba excel-formula excel-2010

我编写了一个工作正常的宏,但看起来好像进行了某种更新,现在我的代码无法正常工作。谁能帮助我确定出什么问题或为什么此功能不再起作用?

功能如下:

Function FindReplace(CellValue$)
    Dim strPattern$: strPattern = "[^A-Za-z, ]+"    'Pattern to only take care of letters
    Dim strReplace$: strReplace = ""    'Replace everything else with blank
    Dim regex As Object
    Set regex = CreateObject("vbscript.regexp")

    With regex
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strPattern
    End With

    FindReplace = regex.Replace(CellValue, strReplace) 'RegEx Function replaces the pattern with blank
End Function

我试图让它看一个单元格,只允许特定的字符浮出水面。

以下是此函数一部分的较大代码:

'Concatenate all the data in the rows into columns A
    Sheets("Formula2").Select
    Dim Lastrow%: Lastrow = ActiveSheet.UsedRange.Rows.Count
    Dim strConcatenate$, I%, j%
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = ActiveSheet

    Range("A:A").Clear

    For I = 1 To Lastrow
        For j = 2 To lastColumn(I) 'Calls function "LastColumn" to get the last column of each row
            strConcatenate = strConcatenate & FindReplace(ws.Cells(I, j))
        Next j
        ws.Cells(I, 1) = strConcatenate 'This will past the finished string into column [A] in the specific row
        strConcatenate = "" 'blanks the string, so the next string in the next row is fresh
    Next I

1 个答案:

答案 0 :(得分:1)

我建议使用以下代码(用A“”,B“ Test”,C“ 123”测试)。

请注意,您需要调整0 1 ... z 00 01 ... zz 循环以在最后一列结束。

For j = 2 To 3