Run-time error '5': Invalid procedure or call argument - excel

I'm attempting to run a macro and keep getting a "run-time error '5': invalid procedure or call argument." Nothing has changed to the file nor the macro since I last used it, but I did get a new laptop. I'm not sure if this has to do with permissions in my excel or if in fact the macro is broken and needs to be fixed.
Sub SS_Cleanup()
ActiveWorkbook.RefreshAll
Sheets("report").Select
Columns("H:H").Select
Range("H:H").Activate
Selection.Replace What:="Global Equity, benchmarked to the MSCI ACWI", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="ESG High Conviction, benchmarked to the MSCI ACWI", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="International Conviction, benchmarked to the MSCI ACWI", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:= _
"No Animal Testing (Medical/Pharma Allowed) Strategy benchmarked to the S", _
Replacement:= _
"No Animal Testing (Medical/Pharma Allowed) Strategy benchmarked to the S&P 1500" _
, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat _
:=False, ReplaceFormat:=False
Selection.Replace What:= _
"Gifting", _
Replacement:= _
"Not Applicable" _
, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat _
:=False, ReplaceFormat:=False
Dim i, n As Integer
Dim ProductHolder, Product As String
Dim ProductName As String
i = 2
Do While Workbooks("SS Data Converter.xlsm").Worksheets("report").Cells(i, 1) <> ""
'enters the product in last column
ProductHolder = ""
Product = ""
ProductName = ""
ProductHolder = Workbooks("SS Data Converter.xlsm").Worksheets("report").Cells(i, 8)
If ProductHolder <> "" And ProductHolder <> "Not Applicable" And ProductHolder <> "Jointly Managed" Then
Product = Right(ProductHolder, Len(ProductHolder) - InStr(ProductHolder, "S&") + 1)
**ProductName = Left(ProductHolder, InStr(ProductHolder, "Strategy") - 2)**
End If
Select Case Product
Case "S&P 1500"
The bold text above is what is saying is a problem with the macro. I haven't attempted to fix it but instead research the root cause. Thanks.

Related

Why doesn't my VBA code work when the selection starts with a dot?

I've created this code that is supposed to replace the commas in a selection with dots if there are any. The code, however, doesn't work if the selection starts a cell containing a dot, but it works if it starts with a cell containing a comma. It even works if it starts with a comma, then a cell with a dot and then again a cell with a comma. Here is the code:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(ActiveCell.Value, ".") > 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(ActiveCell.Value, ".") = 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormul
End If
Next cell
End Sub
Any idea about why this is happening? Thanks :)
You seem to be running your search/replace on ActiveCell, rather than each cell you're cycling through with your For..Each loop. I've tidied it up slightly using the With cell.. End With block.
Try this:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
With cell
.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(.Value, ".") > 0 And InStr(.Value, ",") > 0 Then
.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(.Value, ".") = 0 And InStr(.Value, ",") > 0 Then
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula
End If
End With
Next cell
End Sub

Convert MMM-DD-YYYY text to date

I have a set of data which only pulls as MMM-DD-YYYY. I'd like to convert it to a date (MM/DD/YYYY format) to look it up versus another set of data.
I recorded a macro to simply replace the months with their respective numbers individually but I know there has to be a better way to do this. Below is my
current code:
With ws1.Cells
.Replace What:="jan-", Replacement:="01-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="feb-", Replacement:="02-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="mar-", Replacement:="03-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="apr-", Replacement:="04-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="may-", Replacement:="05-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jun-", Replacement:="06-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jul-", Replacement:="07-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="aug-", Replacement:="08-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="sep-", Replacement:="09-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="oct-", Replacement:="10-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="nov-", Replacement:="11-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="dec-", Replacement:="12-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
This will convert your text string into a true date for the active cell:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
End Sub
You can format it or loop it to your heart's content.
(I am using US locale)
EDIT#1:
With your desired format:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
ActiveCell.NumberFormat = "mm/dd/yyyy"
End Sub
Before:
and after:

Finding and replacing one column in current sheet on Mac

I found the below code, it works good when "search in sheet".
But sometimes, when I open the excel, it default shows "search worksheet". Please see the image.
Then the code will replace entire sheets instead of Columns("V").
Any additional code can control this thing happens?
Thank you in advance.
Sub ReplaceTitleMs()
Columns("V").Replace What:="Ms ", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
So here is my currently code:
Sub Test_Replace()
Worksheets("Sheet4").Columns("B").Replace What:="XXX", _
Replacement:="KKK", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Worksheets("Sheet4").Columns("B").Replace What:="SIN", _
Replacement:="OOO", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
When select "search in worksheet" in the menu, and run the code.

Convert values to a number using excel vba if statement

I have a column in excel that has two values "Disqualified" and "Open".
I want to use an If Statement using VBA to change the disqualified values to 0 and the Open values to 1.
Here is the excel formula that shows what I want to do
=IF(H:H="Disqualified","0","1")
I think I need a for loop to loop through all the values in column H but can't seem to get this to work. Thanks
Taking the code in your self-answer, it can (and should) be refactored to get rid of the Select parts (which will almost always generate problems in the future).
Sub changeValues()
With Worksheets("your_sheet_name")
With .Range("H1:H" & .Range("H" & .Rows.Count).End(xlUp).Row)
.Replace What:="Disqualified", _
Replacement:="0", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="Open", _
Replacement:="1", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End With
End With
'I doubt if the next line is needed
'ActiveWindow.SmallScroll Down:=-66
End Sub
Well there is probably a better way of writing this but this macro does the job.
Sub changeValues()
'
' changeValues Macro
'
'
Range(Selection, Selection.End(xlDown)).Select
Selection.replace What:="Disqualified", Replacement:="0", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.replace What:="Open", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWindow.SmallScroll Down:=-66
End Sub

VBA Replace not working with numbers format

I am, once again, stuck on something.
I am trying to clean phone numbers data, and the code isn't doing anything.
Columns(icount).Replace What:=",", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns(icount).Replace What:="-", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
icount is the column where the phone # are.
I don't understand why it's not working. Replacing "à" with "à" works fine.
Try Using 'LookAt:=xlPart' instead of using 'LookAt:=xlWhole'
Columns(icount).Replace What:=",", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns(icount).Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
You could it like so:
Sub rep()
For Each c In Sheets("Sheet1").Range("A:A").Cells ' Change the range that you want
If InStr(c.Value, ",") > 0 Then
deli = Split(c, "")
For a = 0 To UBound(deli)
c.Value = replace(c.Value, ",", "")
Next a
End If
If InStr(c.Value, "-") > 0 Then
deli = Split(c, "")
For a = 0 To UBound(deli)
c.Value = replace(c.Value, "-", "")
Next a
End If
Next c
End Sub

Resources