Macro issues across Excel versions - excel

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

Related

VBA replacement not working for specific input

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.

Excel search only in column a?

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 ...

Excel/VBA find string and copy offset cells

I'm in need of a small piece of VBA to do the following,
search for a particular string, for instance ("New Applications"), and then copy the cells offset (0,2) and (0,5). Hence if "New Applications" is found in A34, I then need to copy D34 and J34…
Any help, much appreciated.
All I have so far is as below, but i'm sure how to also copy offset(0,5)..
Sub test()
Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Select
Selection.Copy
The rest of the code, re pasting etc I already have, I just need to amend a small part to something like above.
many thanks
In this case it would greatly help to use a variable.
Sub test()
'make a variable called foundrange that is of type "Range"
Dim foundRange as Range
'set this variable based on what is found: (note we remove the 'activate' here
Set foundRange = Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
'You can copy now and do whatever you want. Say you want to copy these values to Sheet2!A1 and B1, respectively:
Sheet2.Range("A1").Value = foundRange.OFfset(0,2).value
Sheet2.Range("B1").Value = foundRange.Offset(0,5).value
'Or copy to the clipboard -- haven't tested this union, but I think it should work
Union(foundRange.Offset(0,2), foundRange.Offset(0,5)).Copy
'Or copy just one and do something
foundRange.Offset(0,2).Copy
'do something
'Copy the other one and do something
foundRange.Offset(0,5).Copy
'do something

Excel Doc macro yields "Named argument not found (Error 448)"

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.

VBA Code to find the row number of a date in data

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

Resources