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

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.

Related

Find currency and amount, also ID store in array and paste in Output sheet

I would like to find the text "Currency" in columns A or B, store all the currencies listed under Currency. Same process for Amount (Can be in an column)
Store values in an array. Then paste in Output Sheet. The currencies will already be listed in Output sheet in 1st row of the sheet. However if it is a new currency then the code should find last used cell in row 1 and add it. The value of Amount should be added to Output sheet against the currency and ID number also copied from the Source sheet.
I have some code.
Public Sub loopRow()
Dim curArray As Variant
Dim listarray As Variant
Dim cnt As Long
'Find Currency
Dim rgFound As Range
Set rgFound = Range("A:B").Find("Currency")
'Find last used row
curArray = Cells(rgFound.Address).End(xlUp).Row
'Transpose list of currecny from the row down from the word Currency that it has found
listarray = Application.Transpose(Cells(Rows, curArray).End(xlUp)).Row
For cnt = LBound(curArray) To UBound(curArray)
curArray(cnt) = curArray(cnt)
Next cnt
For cnt = LBound(curArray) To UBound(curArray)
'Debug.Print curArray(cnt)
'Copy and paste into Sheet under the correct curreny, if new currency then add this in row A
Next cnt
End Sub
Whilst you need to understand your question is unanswerable as is, I'll do my best to help.
The problem we have is not seeing the source sheet the way you do, as we can't see it at all. You say you have the word Currency in columns A or B or both, and an ID column somewhere, and Amount values everywhere. That's tricky source data. If as is more likely, the ID is in a specific column and the amounts are in a set of columns, then we'd have a chance.
Your question outlines the basic steps you'd want to take pretty well, so you're off to a good start.
However you can do all of the work without VBA, certainly if I'm right about the Source data. Create yourself a working sheet, or multiple working sheets. Definitely one to sort out the full list of currencies. Grab a copy of columns A and B (by formulae) and then have the working sheet go through line by line and use logic to build the list. Spreadsheets are great at this.
Once you have the list, use it as row headers on your Output sheet and use sumifs to get the values. I am not sure how the IDs would fit in, but if they were to be your row headings, then do the same as the above to get the list of unique ids and link them into your Output page in column A. Your sumifs can handle that.
That will hardly tell you all you need to know, but if you work it out you'll have learned a lot about Excel and when you need to go into VBA.
If you'd rather do it with VBA, break down each step until it works, and then go onto the next one.
And if you want more help, paste your data in here. Anonymise it first if you need to.

Find and replace with column using VBA

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.

How to compare a string within a string list in Excel

There are some food names and prices as you can see between I2 and J22. For instance AYÇICEK YAĞI(SUNFLOWER OIL IN ENGLISH) is 4$ per kg. In the left of the sheet, you can see other list. What I need is;
I want to compare all A* columns with Strings between I2:I22 and get the price which is written between J2:J22 then write it to the D* columns.
There are more than 500 rows and I need to do it for all rows.
And there are some headings as u can see in bold font, they should be protected.
You seem to have come up with a formula; now you need a way to dispense it. Your worksheet design does not lend itself to simply filling down a formula. However, with the numbers in column C identifying valid entries that require a formula in columns D and E, a short sub procedure can make quick work of putting the formulas into the correct places.
Sub fillFormula()
Dim w As Long, vWSs As Variant, vFRMLs As Variant
vWSs = Array("ogle_aksam_gramaj", "kahvalt" & ChrW(305) & "_gramaj", _
"araogun_gramaj")
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
With .Columns(3) '<~~ going to look at column C for numbers
With .SpecialCells(xlCellTypeConstants, xlNumbers)
.Offset(0, 1).FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC1, 'urunler'!C1:C2, 2, FALSE), """")"
.Offset(0, 2).FormulaR1C1 = _
"=IFERROR(RC4*RC3, """")"
End With
End With
End With
Next w
End Sub
The IFERROR function has been used to 'wrap' the lookup and mulltiplication formulas. It catches errors and offers alternative results; in this case, zero-length strings that look blank.
The kahvaltı_gramaj worksheet causes problems in VBA due to the unicode character. You might try other methods of cycling through the worksheets.
      
That binary (macro-enabled) workbook is available from my own public dropbox here.
In the workbook you have attached, VLOOKUP will return #N/A when there is no available value.
In Sheet ogle_aksam_gramaj Cell D4 use the following Formula:
=SUMIF($I:$I,$A4,$J:$J)
You can then drag it down and it should be giving you the prices based on the details provided in the same sheet (Range I:J)
The good thing (or bad, depends on you) of sum if is that it will return 0 if there is nothing to sum. in your sheet, the items must be unique in the list, otherwise, it will keep summing every instance. So if AYÇICEK YAĞI is there 2 times, it will be summed twice.
You can use Advanced Filter with (unique values only) to make sure that all the ingredients are accounted for and are mentioned only once.
Thanks.

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