v lookup formula in excel to compare values - excel-formula

I need to create a formula to display Hello if it finds HelloWorld in list of values in excel.If it didn't find that value it should display existing value in that list.
My values are like this :
HelloIndia
HelloAustralia
HelloWorld
Please suggest

If you want a simple excel function and not go the VBA/Macro route... this will probably be your answer.
(Example assumes list is in cell A1)
=IF(ISNUMBER(SEARCH("HelloWorld",A1)),A1,"Hello")

Sub FindReplaceAll()
'PURPOSE: Find & Replace text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
fnd = "Hello"
rplc = "HelloWorld"
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub
Original source:
https://www.thespreadsheetguru.com/the-code-vault/2014/4/14/find-and-replace-all

I would use COUNTIF() to find the match:
=IF(COUNTIF(A:A,"HelloWorld"),"Hello",A1)
Replace the A1 with what you would like to return if the value is not found in the range.

Related

Excel VBA to copy only non blank rows from specific column

Making a sheet where the user can select an area from a drop down, and then cells containing info relevant to that area are shown in column T.
The data is formatted in such a way that the areas are the headings across the columns from A1:Q1. Then on each column is a combination of blank cells and cells that contain the info needed.
This shows a simplified example of what I'd like to do. Obviously the X's pertain to actual info.
I've got a code that I think should work, but it's not.... The first section does successfully find the right column from the sheet using whatever is in the drop down. But then the copy paste loop that looks for blank cells does not seem happy, and doesn't want to use the address I found from the find section.
I did explore the idea of using index/match/array, but couldn't get my head round it.
Sub NonBlank()
Dim Found As Range
Dim Clm As String
Dim rngSearch As Range
Dim Criteria As Variant
Dim cell As Range
Criteria = Sheets("Example").Range("S3").Value
Set rngSearch = Sheets("Example").Range("A1:Q1")
Set Found = rngSearch.Find(What:=Criteria, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Found Is Nothing Then
Clm = Found.Address(rowabsolute:=False)
Dim datatocopy As Range
Set datatocopy = Sheets("example").Range("clm").SpecialCells(xlCellTypeConstants)
If Not datatocopy Is Nothing Then
datatocopy.Copy Destination:=Sheets("Example").Range("T3")
End If
End If
End Sub
Any help is much appreciated. :)
If the values are constants and no formulas you don't need to loop through the data and can just use SpecialCells(xlCellTypeConstants) on the range to get all constant values (without the blank cells).
Dim DataToCopy As Range
Set DataToCopy = Sheets("Table").Range("B1:B6").SpecialCells(xlCellTypeConstants)
If Not DataToCopy Is Nothing Then
DataToCopy.Copy Destination:=Sheets("Table").Range("G2")
End If
The following should work
Sub NonBlank()
Dim Criteria As Variant
Criteria = Sheets("Example").Range("S3").Value
Dim rngSearch As Range
Set rngSearch = Sheets("Example").Range("A1:Q1")
Dim Found As Range
Set Found = rngSearch.Find(What:=Criteria, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Found Is Nothing Then
Dim datatocopy As Range
Set datatocopy = Found.EntireColumn.Resize(RowSize:=Rows.Count-1).Offset(RowOffset:=1).SpecialCells(xlCellTypeConstants)
If Not datatocopy Is Nothing Then
datatocopy.Copy Destination:=Sheets("Example").Range("T3")
End If
End If
End Sub

How to check whether a row contains certain text

I am trying to check whether Row 1 of my active sheet named "Exceptions" contains the text "Control  Date" (two spaces) or "Control Date".
My code finds the condition false.
Dim a As Range
Dim exceptions As Worksheet
Set exceptions = ActiveWorkbook.Worksheets("Exceptions")
For Each c In Exceptions.Range("A1:Z1")
If c = "Control Date" Then
Cells.Find(What:="control date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Else
Cells.Find(What:="Control Date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
Next c
Example of a worksheet with two spaces in "Control  Date"
How to write the condition
As far as checking if the cell value is "Control Date" with a single space or one with two spaces, there are two ways of going about it:
Use the like operator
The like operator makes it easy to compare a string to a basic pattern. In this example, using the wildcard character * (Zero or more characters) will return true regardless of how many spaces are between Control and Date.
If cell.Value2 Like "Control*Date" Then
' Do something with that cell
End If
Use the or operator
Using the or operator ok to use as well, although not a flexible and perhaps a bit more difficult to see what's going on for your specific example.
If cell.Value2 = "Control Date" Or cell.Value2 = "Control Date" Then
' Do something with that cell
End If
Worksheet Codename
Each worksheet has whats called a codename. This is a reference that can be called directly in the code to that specific worksheet by it's name.
To set this name, in the properties window update the name property
So instead of
Dim Exceptions As Worksheet
Set Exceptions = ActiveWorkbook.Worksheets("Exceptions")
For Each cell In Exceptions.Range("A1:Z1")
' Do something...
Next cell
you can call the worksheet reference directly
For Each cell In Exceptions.Range("A1:Z1")
' Do something...
Next cell
Putting it together
Instead of using c for your variable, I like to make my variables easier to read and follow so I used cell.
Also, instead of hard coding your header columns in range, you could loop the cells of the entire first row. This is option suggestion though.
Lastly, be more explicit in what property you are looking for in your Cell. In my example I use .value2 to show I am looking for the value of that cell.
Public Sub Demo()
Dim cell As Range
For Each cell In Exceptions.Rows(1).Cells
If cell.Value2 Like "Control*Date" Then
' Do something with that cell
End If
Next cell
End Sub
Why duplicate the data into a third column? Whenever you need the "combined" date, just go get it, but do not store it twice.
Option Explicit '<<-- always have this
Sub doFindControl()
Dim a As Range
Dim c As Range '<<-- add this
Dim colDate As Long, colNumber As Long, colBlank As Long '<<--add this
Dim exceptions As Worksheet
Set exceptions = ActiveWorkbook.Worksheets("Exceptions")
For Each c In exceptions.Range("A1:Z1")
' first find the 2 key columns
If InStr(c, "Control") > 0 Then
If InStr(c, "Date") > 0 Then
colDate = c.Column
ElseIf InStr(c, "Number") > 0 Then
colNumber = c.Column
End If
' second look for the first blank column for you to put results in
ElseIf c.Text = "" Then
colBlank = c.Column
Exit For ' stop looking after its found
End If
Next c
' now you have the 2 FROM columns, and the TO column
MsgBox (colDate & " " & colNumber & " " & colBlank)
' and you can loop thru all the rest of the rows doing combine
End Sub
Thank you for all the answers! Robert Todar's answer led me to my lightbulb moment, and I can't believe at how simple the answer was. All I had to do was change this code:
Cells.Find(What:="Control Date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
to:
Cells.Find(What:="Control*Date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Change part of an excel forumla to include data from another cell

I am looking for an easy way to replace part of a formula in Excel.
I have to do this for over 66,000 formulas every year. Currently i use Find & Replace to speed up the job but it still takes me several hours to do it.
='C:\Excel\[File1.xlsm]08-01-20'!V5
I want to replace the 08-01-20 part of the above formula with the value of cell C2
Create two lists in the format "dd-mm-yy", (one with the incorrect dates, one with the correct
Then replace the example dates in the below script & Run.
Sub Multi_FindReplace()
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
fndList = Array("04-01-20", "11-01-20")
rplcList = Array("05-01-20", "12-01-20")
For x = LBound(fndList) To UBound(fndList)
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub
Use Indirect
=INDIRECT("'C:\Excel\[File1.xlsm]" & TEXT(C2,"dd-mm-yy") & "'!V5")
Just be aware that too many of these volatile functions will slow down Excel as they will re-calc every time anything changes in Excel and that they require the referenced workbook to be open to calc.

Excel VBA Find/Replace Macro

Good Morning,
I am working with a rather large dataset and I am attempting to Find/Replace values which have been modified in one spreadsheet back into the original. What I would like to do is have the macro look for the information in Cell A1 and Replace it with the value in Cell B1, and then continue down until Cell A3600:B3600. Generally, I'm pretty good at peicemealing the code I find on here to get it to do what I want, but I'm at a loss as to what I should be looking for.
What I have right now is:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("A1").Select
Selection.Copy
Cells.Replace What:=ActiveCell.Value, Replacement:="????", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
From here I am at a loss. I understand Range("A1").Select will paste into the Find section of the Find/Replace box, but how do I tell excel I want to select another Cell, in this case B1, for the replace portion? And then how do I tell Excel I want it to keep doing this until it runs out of values in the A Column?
Thank you in advance for any assistance you can provide.
I think you are after something like the code below:
Option Explicit
Sub Macro1()
Dim Rng As Range
Dim OrigStr As String, ReplaceStr As String
Set Rng = Range("A1:B3600")
OrigStr = Range("A1").Value2
ReplaceStr = Range("B1").Value2
' option 1: replace only if the whole cell's string equals to OrigStr
Rng.Replace What:=OrigStr, Replacement:=ReplaceStr, LookAt:=xlWhole
' option 2: replace also partial match inside the cell's string
Rng.Replace What:=OrigStr, Replacement:=ReplaceStr, LookAt:=xlPart
End Sub

Format a range of columns based on their title - Excel

I've got part of a solution but it isn't working like I'd hope, so I've come to you for advice.
I regularly receive Excel files where I need to amend formatting. I'm trying to learn VBA by automating as much of these procedures as possible.
One particular format I complete is converting the date to "DDMMYYYY" (09091986), where it usually comes in as 09/09/1986.
Within my worksheet, there are a total of 3 columns containing dates, all of which need the same formatting and all of which have the word "DATE" in the heading. They are not adjacent to each other.
I must also be careful not to have any other data affected, as I have names and addresses which may contain the characters "DATE".
So, background out of the way... I'm trying to search the first row until I find the word "Date" and then format that for each cell until the last row, before moving on to the next column containing the word "DATE" and repeating this until all columns with the word "DATE" have been formatted.
I'm sure you have a simple solution but I can't seem to find it myself.
Here is the code I have...
Sub Dateformat()
Dim LastRow As Integer
Dim FindCol As Integer
Dim C1 As Integer
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
FindCol = Cells.Find(What:="DATE", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
For C1 = 2 To LastRow
Cells(C1, FindCol).NumberFormat = "DDMMYYYY"
Next C1
End Sub
This works for the first column containing date but doesn't move on to the next column.
Thanks for the help
Regards,
Adam
As you know, you need to loop through and find each Row Header with DATE
Here is one way to do it.
Sub Dateformat()
Dim wks As Worksheet
Dim LastRow As Integer
Dim FindCol As Range
Dim sAdd As String
Set wks = ThisWorkbook.Sheets("Sheet1") ' adjust as needed
With wks
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'find first instance where DATE exists in row 1 (headers)
Set FindCol = .Rows(1).Find(What:="DATE", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'store address of first found instance (to check in loop)
sAdd = FindCol.Address
Do
'format column (row 2 to last used row)
.Range(.Cells(2, FindCol.Column), .Cells(LastRow, FindCol.Column)).NumberFormat = "DDMMYYYY"
'this line works as well and is a bit cleaner
'.Cells(2, FindCol.Column).Resize(LastRow - 1, 1).NumberFormat = "DDMMYYYY"
'find next instance (begin search after current instance found)
Set FindCol = .Cells.FindNext(After:=FindCol)
'keep going until nothing is found or the loop finds the first address again (in which case the code can stop)
Loop Until FindCol Is Nothing Or FindCol.Address = sAdd
End With
End Sub

Resources