Search and Replace Combobox help!

jjsauer

Board Regular
Joined
Jan 11, 2012
Messages
58
Hello,
I created a macro that has a userform to select a workbook, pull data from it, and then go through the comments (its from a survey with an open-end question). The search and replace was designed to remove expletives. I wont post the expletive list, but I will share the rest. The red text below (cl.Value = sReplace) gives me an error: Run-time error '1004: Application -defined or object-defined error.

Any suggestions where I went wrong would be most helpful!


Private Sub CommandButton1_Click()
MyFile = Me.ComboBox1.Value
Workbooks(MyFile).Activate

Dim re As Object
Dim rng As Range, cl As Range
Dim sh As Worksheet
Dim wb As Workbook
Dim wb2 As Workbook
Dim sReplace As String
Dim aReplace(0 To 51, 0 To 51) As String
Dim i As Long

Workbooks(MyFile).Activate
Range("Q2:R" & Range("R65500").End(xlUp).Row).Copy
Windows("FD C-Sat Comment Creator for Portal.xlsm").Activate
Sheets("Comments").Activate
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(MyFile).Activate
Range("D2:D" & Range("D65500").End(xlUp).Row).Copy
Windows("FD C-Sat Comment Creator for Portal.xlsm").Activate
Sheets("Comments").Activate
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(MyFile).Activate
Range("G2:G" & Range("G65500").End(xlUp).Row).Copy
Windows("FD C-Sat Comment Creator for Portal.xlsm").Activate
Sheets("Comments").Activate
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Set wb = ActiveWorkbook
Set wb2 = Workbooks("FD C-Sat Comment Creator for Portal.xlsm")
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.IgnoreCase = False
re.MultiLine = True

aReplace(0, 0) = "\bEXAMPLE\b"
aReplace(0, 1) = "[Expletive Deleted"



For Each sh In wb2.Worksheets
On Error Resume Next
Set rng = sh.UsedRange.SpecialCells(xlCellTypeConstants)
If Err.Number <> 0 Then
Err.Clear
Else
On Error GoTo 0
For Each cl In rng
sReplace = cl.Value
For i = 0 To UBound(aReplace, 1)
re.Pattern = aReplace(i, 0)
If re.test(sReplace) Then
sReplace = re.Replace(sReplace, aReplace(i, 1))
j = j + 1
End If
Next
cl.Value = sReplace
Next
End If
Next
Unload Me
MsgBox ("Number of expletives replaced:" & j)
Quarter = InputBox("What is the current Quarter in the format FYXX-QX", "Data File Quarter")
mySheet = (Quarter & "_Comments")
Application.Dialogs(xlDialogSaveAs).Show mySheet, xlCSV
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top