Find and replace with column using VBA - excel

I was wondering if there was a way to accomplish this task. I have column A and B. Column A1 contains a text value "Shared Service". Column B1 contains the text value "Shared".
What I want to do is remove the "Shared" part of "Shared Service" so all that's left is "Service". I need to do this for many different values in column A/B though.
Is there any easy way to automate this? It's basically just a mass find and replace with a blank value, it just needs to be based off the values in column B.
UPDATE:
Thanks for the replies but these solutions assume the columns line up side to side. To clarify, I need to look for ALL values in column B and remove ALL of them from column A. So the "of vat" line would become "of" in this example (it finds the "vat" value from column B and removes it from A). This has to be done with 1000s of different values.
Thanks in advance for any help provided.

I could be wrong, but it sounds like you might want a collection. I see something like this as a starting point:
Sub RemoveWords(DeleteFromCol As Range, FindCol As Range)
Dim words As New Collection
Dim r As Range
Dim word As Variant
For Each r In FindCol
words.Add (r.Value2)
Next r
For Each r In DeleteFromCol
For Each word In words
r.Value2 = Replace(r.Value2, word, "")
Next word
Next r
End Sub
And then you could invoke it with aribitrary ranges, in your example:
RemoveWords Range("A1:A11"), Range("B1:B8")
I'm guessing this may not be 100%, as in your example I would purge both words in some cases, since they exist in the "one word" list. You can handle that however you want.

Related

VBA: Extracting coordinates of an active cell

I am new to Excel VBA and what I want to do is the following.
Currently the cell "F3" is active, and I would like to know if there is a function
that returns the value "F3".
I imagine that there is a way to extract the 'coordinate' of F3 as (6,3) since F is the 6th letter and 3 is simply 3 (maybe (3,6) if it counts row first and columns later).
If it can return something like (F,3) it would be even better, and best case scenario, F (the column LETTER) by itself.
It is imperative that I am able to extract the column letters because I intend to use this as an input to "replace all letter A to letter B".
If you have an exact answer that would be great, but if you could guide me towards a useful link that would be appreciated, too.
Thank you!!
Try below sub-
Sub GetCellAddress()
Dim x As Variant
x = Split(ActiveCell.Address, "$")
MsgBox x(1) & "," & x(2)
End Sub
Result in message box: F,3

Replace old values in new values from an Excel Table en masse

I have a large table with about 1,500 values. And I need to replace the "old" values with the "new" values in the table. Basically, I need to "find and replace" en masse.
Column A
Column B
Red
Cherry
Blue
Indigo
If we were take the example above. Say I have a column that has a list of all of our products. Red car, blue mouse, etc. What I need to do is say "Look in column C (containing the products) for the value in column A and Replace it with the value in column B".
So, for example. Let's say I have "Red Car" I would want it to now say "Cherry Car". This is a simplistic way of putting it, but there are around 1,500 values and they are all just numbers. So it's not as simple as doing a find and replace unless I want to spend days finding and replacing and hoping I did it all right.
Here's an example of what it really looks like:
Old Value
New Value
653111
21870538
694182
21212412
653141
34123451
Table of Products
653111xyr
694182jfh
653141ren
Maybe this helps you:
=INDEX(Sheet2!B:B,MATCH(NUMBERVALUE(LEFT(A1,6)),Sheet2!A:A,0))&RIGHT(A1,3)
Sheet2 in this case contains the old and new value data.
A1 is the current value you want to have changed.
This is provided all the data has same length of numbers and characters.
I would suggest using VBA (or if something similar can be done using Office.js) to iterate over each of the old/new value pairs, and programmatically do a global Find/Replace on the products sheet.
Using VBA:
Dim mappings() As Variant
mappings = Worksheets("Mappings").UsedRange.Value
Dim row As Integer, rowCount As Integer
rowCount = UBound(mappings, 1)
For row = 1 To rowCount
Dim wks As Worksheet
Set wks = Worksheets("Products")
wks.UsedRange.Replace mappings(row, 1), mappings(row, 2), xlPart
Next
Given the number of products and mappings you have, I think this likely will take some time to run.

Excel: Formula to extract a string of text delimited by markers from cells

I'm messing with a spreadsheet containing postal addresses that have been inserted in the cells' comments
Each comment contain an address composed of a variable number of lines (damn UK addresses, they can have up to 7 lines!) in the following format:
Line1,
Line2,
Line3,
[...],
State
With my poor skills, I've managed to extract the comment with a VBA script, obtaining the following string on a single cell:
Line1,Line2,Line3,[...],State
At this point each string between commas must be extracted to its own cell.
I've managed to extract the 1st 3 lines with the following formulas:
For Line1:
=LEFT(A8;(SEARCH(",";A8))-1)
For Line2:
=MID(A8; SEARCH(",";A8)+1; SEARCH(","; A8; SEARCH(","; A8)+1)-SEARCH(",";A8)-1)
For Line3:
=MID(A8; SEARCH(",";A8;SEARCH(",";A8;SEARCH(",";A8;SEARCH(",";A8)))+1)+1;SEARCH(","; A8; SEARCH(","; A8;SEARCH(",";A8)+1)+1)-SEARCH(",";A8;SEARCH(",";A8)+1)-1)
From this point I start to get overflow errors from my brain... I probably need some days of sleep.
Can anybody help me to get to "line6", and finally suggest me how to pull out the "State line" which ends without comma?
I thought I could pull out the "State" line with =RIGHT(",";SEARCH(",";A8)-1) but I'm obviously doing something wrong because that pulls out a comma instead of a string.
I guess I could do everything with a VBA script, but I'm not that skilled yet :(
With comma separated data in A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,",",REPT(" ",999)),COLUMNS($A:A)*999-998,999))
and copy across. For example:
Note:
Why not use TextToColumns ?
The row of formulas re-calculates automatically if A1 changes.
The row of formulas will work even if A1, itself, contains a formula.
If you are wanting to do this programmatically instead of using a built-in, check out the split function for chopping up your comma separated string. It will split up your input string into an array. Then you can do whatever you like with the array.
Dim Names() As String
Names() = Split(inputValue, ",")
For i = 0 To UBound(Names)
' do what you want with each piece
Next
Gary's Student's answer is great for using the built-in functions.
If you want a VBA solution:
Sub spitString()
Dim sourceRange As Range
Dim stringArr() As String
Dim i As Integer
Set sourceRange = ActiveSheet.Range("A1")
stringArr = Split(sourceRange.Value, ",")
For i = LBound(stringArr) To UBound(stringArr)
sourceRange.Offset(0, i + 1).Value = stringArr(i)
Next i
End Sub
You could avoid adding comments: Are you aware that users can add line breaks inside a cell by pressing ALT+RETURN?
If having high rows d is a problem and you don't like that formatting, an alternative approach might be to write a simple bit of code that changes the height of the current row when a user clicks in a certain range. It would , make other rows less high. Perhaps.
Just a thought. It has benefits keeping it simple.
Harvey.

Excel: Using a formula to search a column and replace cells

Sorry this may seems super basic but im struggling with excel at the moment as I am self teaching. I'm looking for a function which will search a column and replace all instances of "Big" with 1 and "Tiny" with a 0.
If your data is in column B, then in column A you could have something like
=if(B1="Big",1,if(B1="Tiny",0,B1))
Then copy that formula down your column to make a new column with the replacements you want made.
If you don't want to create a new column, but just modify the old one, then you could use vba, like so
Sub myReplace()
for i = 1 to ActiveSheet.UsedRange.Rows.Count
if cells(i,1) = "Big" then
cells(i,1) = 1
elseif cells(i,1) = "Tiny" then
cells(i,1) = 0
end if
next i
end sub
This assumes your column of interest is column A.
Highlight the column, hit CTRL+H to bring up the search and replace box, tell it what you want to do.
If you prefer a formulaic way and the words are within a text string, =SUBSTITUTE(text,old_text,new_text,[nth_appearance]) would do the job as well.

Put unique distinct values into a list from multiple columns of an array

I'm new to Excel and the journey has been good so far, but I haven't been able to resolve this particular issue by myself. I'm dealing with a table as under:
Essentially, I'm looking to refer to the array of tags in columns from B3:E6, and do the following:
Create a "Unique Tags" column: Create a unique list of "tags" in column H by removing duplicates.
Create a "Maximum Marks" column: Look for each of the unique tags in the array in each row, and return the marks from the marks column in the same row. If the tag appears in multiple rows. the sum of the corresponding marks in these multiple rows should be returned in the maximum marks column in column I. For example, 'EASY' appears in E3 as well as E5. Thus in the 'Unique Tags' List 'EASY' should correspond to Maximum Marks = 4 (2+2).
I could do this manually using formulas such as SUMIF, but I'm looking for a way to automate it since I might have to do this operation for a similar dataset with additional rows & columns. I'm open to VBA solutions as well but would prefer some sort of formula.
I hope I've explained it well enough! Thanks and looking forward to your inputs.
One way to do this is create a function that returns the array of your unique cells and then multiplies them all by matches in your Marks column.
Create the unique cells with this array function. Note this function uses the Dictionary object. In the VB Editor, go to Tools > References, and make sure Microsoft Scripting Runtime is selected.
Public Function UniqueValues(aRange As Range)
Dim DictValues As New Dictionary
Dim cll As Variant
Dim aryResults() As String
For Each cll In aRange
If Not DictValues.Exists(cll.Value) Then DictValues.Add cll.Value, "":
Next
UniqueValues = DictValues.Keys
Set DictValues = Nothing
End Function
Enter in cell H3 and press CTRL SHIFT RETURN (as it's an array function)
=TRANSPOSE(uniquevalues(B3:E6))
and drag down to H15 or beyond
We have to use TRANSPOSE as the array comes out in a row from the function.
Next we need to find the matching cells and multiply. Here in C15 enter the formula below
=INDEX(SUM((($B$3:$E$6=H3)*1)*$F$3:$F$6),1)
Drag this down to H15.

Resources