vba code for Find the date - vba

Sheet 1:- I mentioned the date in Range A1 with dd-mmm format (i.e., 01-Sep)
Sheet 2:- I updated all the date (from Jan to Dec) with same format (dd-mmm)
But I'm unable to find (select the cell) the date.
I wrote as below:
Sub UpdateToday()
Dim x As String
Dim Cell As Range
x = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
Sheets("Sheet2").Activate
Range("C10:NC10").Select'All 360 days
Application.FindFormat.NumberFormat = "dd-mmm"
Set Cell = Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=True).Activate
Sheets("Sheet2").Activate
End Sub

There are a few issues:
Unless you think it's absolutely necessary, I would remove the notion of searching on the format. Adding the format into the search is only complicating it. I would remove the formatting criteria and simply change the LookIn parameter to the Find method from xlValues to be xlFormulas so that the formatting does not matter at all.
You need to define x as Date to hold the true value as it is stored in all of the cells.
Your code will only find the first cell with the same date and there could be many. Not sure if this was your intention.
You can't Activate at the same time as assigning the Cell variable. You need to do this in two steps.
I would update your code to look like this:-
Sub UpdateToday()
Dim x As Date
Dim Cell As Range
x = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
Sheets("Sheet2").Activate
Range("N10:NC10").Select 'All 360 days
Set Cell = Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Cell Is Nothing Then
Cell.Activate
End If
Sheets("Sheet2").Activate
End Sub

Related

Need explanation: vba find method troubleshooting

I am using Find method to bring the cell value from another workbook.
The code below brings the value. But I wanted to erase Activate methods, so just using Block Statements with Find method to bring values from another workbook.
'Windows(wb_name).Activate
'Sheets("SheetA").Select
'Set rg =Worksheets("SheetA").Range("C:C")
'With rg
'value1 = Cells.Find(What:="11693", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,
SearchFormat:=False).Value
'End With
For clarifying, what exactly I want; in values1 = Cells.Find ...I changed Cells to rg but it doesnt work. I want to know why? Also I see it unnecessary to use activate . I want to write a code where I will get rid of Activate another workbook. So, just by giving source wb and ws names and range to look for the value
Try the next way, please:
Sub FindInOtherSheet()
Dim Value1 As String, rg As Range
Set rg = Workbooks("W1.xlsx").Worksheets("SheetA").Range("C:C")
With rg
Value1 = .cells.Find(What:="11693", After:=.cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).value
End With
End Sub
It starts searching after the first cell of the range. ActiveCell does not have sense in a not activated sheet...
Edited:
As an example to clarify your question about the Find "problem" of not returning any error in case of no any match, I would state that this should be considered an advantage.
You can simple check if the function returned a range in this simple way (I will use the above code to exemplify):
Sub FindInOtherSheet()
Dim Value1 As String, rg As Range, fndCell as Range
Set rg = Workbooks("W1.xlsx").Worksheets("SheetA").Range("C:C")
With rg
set fndCell = .cells.Find(What:="11693", After:=.cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
'check if Find returned a range:
If not fndCell is Nothing Then 'if a match has been found
Value1 = fndCell.value
Else
MsgBox "No match has been found...": Exit Sub
End If
End With
End Sub

Trying to select a cell with a variable as a column number

I'm trying to set up a code that will find a cell using the find function and then select a different cell in that column. I store the column number as a variable, and then try to move to a cell in the same column using the variable, but its not working.
I've tried changing it so that the column is stored as a string instead of an integer and tried using the .Cells method instead, neither have worked.
Dim numCol As String
Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=
_
False, SearchFormat:=False).Activate
numCol = ActiveCell.Column
Range(numCol & "4").Select
Getting a 1004 "method range of object global failed" error from that last line of code.
Here's some modified code from your query that might be useful. You want to try to use the properties of the range. In the below example I've defined a range that is the found cell fcell. A couple examples of what you can do ar there.
Dim numCol As Long
Dim fcell As Range
'this will find the cell and set it as a variable of fcell
Set fcell = Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'you can select it
fcell.Select
'you can select the whole column
fcell.EntireColumn.Select
'you can select the column number (if one column)
numCol = fcell.Column
'you can return the address
MsgBox "the address is " & fcell.Address
Good luck.

Search for € symbol in a range defined in code

I am not experienced in VBA coding.
My VBA code:
Search = InStr(ActiveCell.NumberFormat, Chr(128))
Selection.Find(What:=Search, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
It searches and activates the cells which include the € symbol. It works only if I manually define my selection range.
When I try inserting ActiveSheet.Range("H:H").Select to make column H my selection (which is my goal), the code stops working.
The problem is in the ActiveCell, which is changing depending on what you are selecting. Try like this, you should get lucky:
Option Explicit
Sub TestMe()
Dim Search As String
ActiveSheet.Range("H:H").Select
Search = CStr(Chr(128))
Selection.Find(What:=Search, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select 'or .Activate
End Sub
Once you feel a bit better with recording macros, you may try to avoid ActiveSheet, Selection and ActiveCell:
How to avoid using Select in Excel VBA
This code found the cell with the Greek Euro format in the range A1:A6 on Sheet1 in the workbook containing the code (ThisWorkbook).
The cell must hold a value (to find blanks change "*" to "").
Sub Test()
Dim rRangeToSearch As Range
Dim rFoundRange As Range
Set rRangeToSearch = ThisWorkbook.Worksheets("Sheet1").Range("A1:A6")
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "#,##0.00 [$€-408]"
Set rFoundRange = rRangeToSearch.Find(What:="*", SearchFormat:=True)
If Not rFoundRange Is Nothing Then
MsgBox "Greek Euro format found in cell " & rFoundRange.Address
End If
End Sub
No idea why [$€-408] denotes Greek.

Searching for a string of text in a specific worksheet

Im trying to make excel search for a text string in a specific column in a specific worksheet that is not the active worksheet. VBA gives me an error that says i cannot use this method of selection. so my question is, do you have a suggestion to do it in another way?
Worksheets("Parts for renovation").Columns("Q:Q").Select
Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Worksheets("Parts for Renovation").ActiveCell.Offset(0, 4)
End If
There is no need to select anything there:
With Worksheets("Parts for renovation").Columns("Q:Q")
Set cell = .Find(What:="Total transfer price", After:=.Cells(1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Cell.Offset(0, 4)
End If
End With
Your error comes, because you select a range on a non-active worksheet. This is one of the reasons, why you should be avoiding select in general.
However, if you want to make your code working (which is strongly not advisable), you may consider selecting the worksheet before selecting the range:
Worksheets("Parts for renovation").Select
Columns("Q:Q").Select
For the advisable part, try to avoid the usage of "Select" -
How to avoid using Select in Excel VBA

Find a range in another Sheet

I have the piece of code below:
Selection.Find(What:="4", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Dim cl As Range, rTest As Range
Set rTest = Range("a1", Range("a1").End(xlToRight))
For Each cl In rTest
If Not cl.Value > 0 Then
cl.EntireColumn.Hidden = True
End If
Next cl
End Sub
Where says What=4, i would like to search the Range(e15) of another worksheet. Search the value of E15 in one sheet and look for it in a specific range in another sheet. I have all the other piece set, but I dont know how I can reference the value of e15, this can 4 or any other number. After finding, hide all columns that are not my specific value. Many thanks!
you should act like follows
Dim f As Range
Set f = Selection.Find(What:=Worksheets("otherWorksheetName").Range("e15").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not f Is Nothing Then
f.Activate '<--| what do you need this for?
Range("A1", Range("A1").End(xlToRight)).EntireColumn.Hidden = True '<--| hide all columns in wanted range
f.EntireColumn.Hidden = True '<--| unhide found range column
End If
where you have to change "otherWorksheetName" to you actual "other " worksheet name