I am trying to use VBA Find function to find a date from a column and then return the row number of the date.
This works:
Cells.Find(What:="1 Jul 13", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
But, this doesn't work:
Cells.Find(What:=Range("E2"), After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Any idea? I want the user to input a date to "E2" and then run my code, so I would prefer to use the second way if possible.
Many thanks for your help.
Ben
If 1 Jul 13 actually appears in the cell, then use;
What:=Range("E2").Text
Related
I am trying to make a VBA macro in excel which searches for a heading (succeeded at that), then selects the column of the heading (succeeded at that too) to finally do a replacement through the selected column (only partly succeeded in this).
I want to replace e.g SUM(C22:G22) with SUM(C22:INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4))). It works just fine in excel when I select a column and then ctrl+H and find what :*) replace with :INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4))).
Below is the code I tried that works:
Sub FindString_Search_Replace_Column()
Rows("1:1").Select
Selection.Find(What:="XTOTAL 2021", After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.Replace What:=":*)", Replacement:=":A3)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
It does exactly what I want when it "just" needs to replace with e.g. A3. However, when the code replacement bit gets a bit longer, see below code, with what I actually need, it seems like it stops right after ActiveCell.EntireColumn.Select. It never actually changes anything.
Sub FindString_Search_Replace_Column()
Rows("1:1").Select
Selection.Find(What:="XTOTAL 2021", After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.Replace What:=":*)", Replacement:=":INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4)))", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
I am thinking it is the inside ADDRESS bit it kind of chokes on but here my google skills cannot help me any further (no results suiting my problem).
Any ideas? :)
Try replacing the ; separators in your formula with , - when adding a formula in VBA typically you use "US-style" separator (unless assigning the formula via FormulaLocal).
Possibly that also applies to updating an existing formula.
Cells.Find(What:=apple, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate
Want to search only in column A
It search whole sheet
Please ignore apple it's only example
Thanks in advance
Implicit cells always refers to the active sheets cells.
Be explicit regarding the range you want to search within
Dim rgToSearch As Range
Set rgToSearch = ActiveSheet.Columns("A")
rgToSearch.Find ...
I've got a macro-enabled workbook that works just fine in Office365's version of Excel but earlier versions are breaking all over the place. If anyone could provide direction, I would appreciate it!
The current issue is with this bit below:
Range("1:1").Select
Selection.Find(What:="eventdate", After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select
Range(Selection.Offset(1, 0), Selection.Offset(1048570, 0).End(xlUp)).Select
Set rng = Selection
For Each eventdateCell In rng
eventdateCell.NumberFormat = "#"
eventdateCell.Value = WorksheetFunction.Text(eventdateCell.Value, "yyyy-mm-dd")
Next eventdateCell
This code snippet runs perfectly fine in Excel on Windows.
But on OSX, it's giving an error Named argument not found (Error 448).
With Sheets("Colors")
Set rangeFound = .Cells.find(What:=Resource, After:=.Cells(1, 1), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End With
The Mac does not support the SearchFormat argument. Just omit and the code will run.
Say I have a range of data from A1:Y55 containg data like "123 UID"
I am looking for a unique substring (in this case, the UID) in that range, how do I do this?
You can do this with the following code.
On Error Resume Next
Range("A1:Y55").Find(What:="UID", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
The error statement will allow the program to continue if it does not find any matches.