Cozkincaud
New Member
- Joined
- Apr 18, 2019
- Messages
- 12
Hi,
I’ve spent the last four hours trying to pass a range of cell strings into a variant array for use with an inner for each cell cell.replace loop.
I can't seem to
My macro takes each of a range of string cells in one worksheet and searches for them in a range of string cells in another worksheet. If there is a match the found string is replaced with a null string (“”).
Here is the macro as it presently stands.
My thanks in advance for your help and suggestions.
I’ve spent the last four hours trying to pass a range of cell strings into a variant array for use with an inner for each cell cell.replace loop.
I can't seem to
- Pass the cell string range into my array - myArray = myRange.Value
- Reference each string in this array - SearchValue = myArray(I, 1) I get a 'Subscript out of range' error
My macro takes each of a range of string cells in one worksheet and searches for them in a range of string cells in another worksheet. If there is a match the found string is replaced with a null string (“”).
Here is the macro as it presently stands.
My thanks in advance for your help and suggestions.
Rich (BB code):
Sub EraseTaxCategories()
Dim WSFind As Worksheet ' worksheet with the range holding the text strings that are to be searched for
Dim WSSearch As Worksheet ' worksheet with the range of text cells that are to be searched for text strings
Dim myRange As Range ' the variable naming the worksheet range holding the text strings to be searched for
Dim mySearchRange As Range ' the variable naming the worksheet range holding the text strings to be searched
Dim myArray As Variant ' range holding the text strings that are to be search for (they are copied into an array)
Dim SearchValue As String
Set WSFind = Worksheets("Variable Names") ' two lines to activate the sheet containing the text strings WSFind.Activate to be searched for
Set myRange = Sheets("Variable Names").Range("TaxNames") 'Set a range variable to the range of search strings
myArray = myRange.Value ' create an array containing the text strings that will be searched for in sheet "WSSearch". Seems to fail! I can't tell if any values are passed!
Set WSSearch = Worksheets("Remove Extraneous Text") ' 2 lines to activate the sheet (WSSFIND) containing the WSSearch.Activate text strings that are to be searched in
Set mySearchRange = Sheets("Remove Extraneous Text").Range("TaxPayers") 'Set a range variable to the range of text cells that are to be searched
For Each Cell In myArray 'the text strings in the named range 'TaxNames' in sheet 'Variable Names" that are to be searched for
I = 0 'Initialize a counter for the array containing the text strings that are to be searched for. First row in an array is 0 by default?
SearchValue = myArray(I, 1) 'the string value from the named range "TaxNames" in sheet 'Variable Names' Gives 'Subscript out of range' error
For Each InnerCell In mySearchRange ' Search 'Tax Names' in 'Remove Extraneous Text' for each of the cell string values in rrange "TaxPayers' in 'Variable Names' sheet
Cell.Replace SearchValue, "", LookAt:=xlPart 'Replace any found string with a null string
Next InnerCell
I = I + 1 ' increment the counter so that it moves to the next row in the array 'myArray'
Next Cell
End Sub
Last edited by a moderator: