Найти и заменить - PullRequest
       8

Найти и заменить

0 голосов
/ 30 октября 2018

Я написал макрос, который работал нормально, но похоже, что произошло какое-то обновление, и теперь мой код не работает. Может кто-нибудь помочь мне определить, что происходит не так или почему эта функция больше не работает?

Вот функция как есть:

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 голосов
/ 30 октября 2018

Я предлагаю следующий код (проверено с A "", B "Test", C "123").

Обратите внимание, что вам нужно настроить цикл For j = 2 To 3 так, чтобы он заканчивался в вашем последнем столбце.

Option Explicit

Sub Test()
    'Concatenate all the data in the rows into columns A
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Formula2") 'avoid select and specify the sheet by name

    Dim LastRow As Long
    LastRow = ws.UsedRange.Rows.Count

    Dim strConcatenate As String
    Dim i As Long, j As Long

    ws.Range("A:A").Clear 'always specify in which sheet a range is!

    For i = 1 To LastRow
        For j = 2 To 3 'changed that for testing to 3
            strConcatenate = strConcatenate & FindReplace(ws.Cells(i, j).Value)
        Next j
        ws.Cells(i, 1).Value = strConcatenate 
        strConcatenate = "" 
    Next i
End Sub

Function FindReplace(CellValue As String) As String
    Dim strPattern As String
    strPattern = "[^A-Za-z, ]+"    'Pattern to only take care of letters

    Dim strReplace As String
    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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...