Find and Replace Multiple Values and update in another column - excel

sub ()
for i = 3 to 6
worksheets("sheet1").range("a2:a35").select
selection.replace what:=cells(i,3).value,replacement:=cells(i,4).value,
lookat:xlpart,searchorder:=xlByRows,matchcase:=False
Next
Worksheets("sheet1").cells(1,1).select
end sub
What if the value to be replaced is in another column? For example, I still want the range of value to be found in column "A" but then replace the value in "column B" of the same row. How would the code change?

Use Find method and read row numbers from the found results (Row property). You can use that information to refer to cell in the column you need to change.

Related

Insert a new column before a certain column based on row header-VBA

I am simply trying to search for a specific column based on the header and insert a new column right before it (so to the left of the column). Right now my code searches for all columns that even contain my search and inserts a new column 2 columns to the left. For example I want to search for the header that says Paid to Date ONLY but it even inserts a column next to Commission Paid to Date etc.
Sheets(NewSheet).Select
Set ColHeaders = Range("A4:CD4").Find("Paid to Date")
If ColHeaders Is Nothing Then
MsgBox "Paid to Date Column was not found."
Exit Sub
Else
Columns(ColHeaders.Column).Offset(0, -1).Insert
End If
In the time it took to confirm a solution, this seems to be answered in the comments.
Remove the Offset as it will insert in column before by default
Read the documentation for Find here and specify the argument LookAt to be xlWhole (put a value of 1, as per enumeration)

Excel: Check for partial matches from column against single cell

I have two columns in Excel:
Column A
Row 1 Apple
Row 2 Blueberry
Row 3 Strawberry
Column B
Row 1 App
Row 2 Application
Row 3 Appendage
I would like to use Column B to see if any cells within it exist within the given cell in Column A. So far, I have used the VLOOKUP and MATCH functions and I can't seem to get either to work properly, but MATCH seems to be the one I should be using. I tried using wildcards on Column B and it returns a value error. Here is what I have:
=MATCH(A1,"*"&B:B&"*",0)
Your help is greatly appreciated!
There is a natural VBA solution. In a standard code module place:
Function PartialMatch(v As Variant, R As Range) As Variant
Dim i As Long
For i = 1 To R.Cells.Count
If v Like "*" & R.Cells(i).Value & "*" Then
PartialMatch = i
Exit Function
End If
Next i
PartialMatch = CVErr(xlErrNA)
End Function
Then where you want it in a spreadsheet you can use the formula:
=PartialMatch(A1,B:B)
It will give the index of the first partial match, if any exists, or #N/A if it doesn't. Note that a blank cell counts as a partial match, so you might want to make sure that the range that you pass the function contains no blanks (so don't pass the whole column). That, or redefine what you mean by a partial match.

Lookup a value from a cell with multiple values

I have an excel file containing 2 columns (Area Code) & (State).
**Area Code** **State**
217, 224, 309, 312, 331, 618, 630, 708, 773, 779, 815, 847, 872 Illinois
219, 260, 317, 574, 765, 812 Indiana
319, 515, 563, 641, 712 Iowa
316, 620, 785, 913 Kansas
270, 502, 606, 859 Kentucky
I want to use vlookup() for a given area code like "620" and get "Kansas". please note that all the values in a row are stored in one cell (i.e. "270, 502, 606, 859" are stored in one cell)
=VLookup("*620*", A2:B6, 2, false)
In VBA:
Function FindState(code as integer) as string
FindState = Application.VLookup("*" & code & "*", mySheet.Range("A2:B6"), 2, false)
End sub
=VLOOKUP(INDEX(A:A,MATCH(TRUE,ISNUMBER(FIND(d2,A:A,1)),0)),A:B,2,0)
Assuming the number you want to find is in d2 and the data is stored in columns a and b.
I asked to find the first find without and error and return the row number. After that, I a lookup for it.
Use array formula ctrl+shift+enter

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.

If values in Column B = specific text, insert specific text into a value in Column A

Simply - if any cell in Column B contains thisvalue then append to the adjoining cell in Column A with sometext.
How is this done?
A simple if statement. For example:
=IF(ISNUMBER(SEARCH(thisvalue, B1)), sometext, "")
EDIT: The ISNUMBER(SEARCH(thisvalue, B1)) searches for thisvalue in B1, and if it finds it, it returns a number (that number being the starting index of thisvalue within B1).
EDIT #2: To append the inserted value to the end of the current value in cell A, use the CONCATENATE formula.
Example:
=CONCATENATE(A1, sometext)
Put this formula in A1, then drag down as necessary:
=IF(B1="thisvalue","sometext","")
EDIT
Using a the Visual Basic Editor, you can update the contents of cell A like this:
Private Sub UpdateColumnA()
Dim x As Long
For x = 1 To 65536
If InStr(1, Sheet1.Range("$B$" & x), "thisvalue") > 0 Then
Sheet1.Range("$A$" & x) = Sheet1.Range("$A$" & x) & "sometext"
End If
Next
End Sub
Repeated runnings of the macro, however, will append the text again; you'll need more validation code if you don't want this to happen.
copy-paste in A1 , considering that you have values in B
=IF(ISNA(VLOOKUP("thisvalue",B:B,1,FALSE)),"",VLOOKUP("thisvalue",B:B,1,FALSE)&"ADDITIONAL VALUE")
it is saying:
if value of vlookup is is empty (if lookup returns nothing) , then show empty value ( double quotes)
but if the value of lookup returns something, then do this lookup and append "ADDITIONAL VALUE" text to found result
I think I have what you are looking for, let me know if you are still interested and if you want me to elaborate further. This formula in cell F2: =IF(ISNUMBER(SEARCH($U$2,E:E)),$V$2,"")&IF(ISNUMBER(SEARCH($U$3,E:E)),$V$3,"")&...
where you are searching for a value that you specify in U2 across all cells in column E:E, if it finds a match it appends the value you specify in V2. To search for multiple words assigning corresponding value simply concatenate as shown as much as you like. I am able to specify hundreds of words (and corresponding values). I hope it helps.

Resources