Analyst2
Analyst2

Reputation: 31

Replace Function not working Getting error: Wrong number of arguments or invalid property assignment from replace function

I am trying to update a macro that starts by changing the name of a workbook path. When I run the code I am getting the wrong number of arguments error. I have looked over the argument but appears to me to be working correctly. Thank you in advance of any assistance!

Sub agetprevsectinfo()
Dim wb As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim x As Integer
Dim xx As Integer
Dim y As Integer
Dim newdir As String
Dim formulapth As String
Dim curmnth As String
Dim prevmnth As String
x = 0
xx = 0
y = 104
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet
curmnth = wb.Sheets("Section Info").Range("N28")
prevmnth = wb.Sheets("Section Info").Range("N27")

newdir = wb.Path & "\" & replace(replace(wb.Name, curmnth, prevmnth), MonthName(CDate(curmnth)), MonthName(CDate(prevmnth)))
formulapth = wb.Path & "\[" & replace(replace(wb.Name, curmnth, prevmnth), MonthName(CDate(curmnth)), MonthName(CDate(prevmnth))) & "]"
'last chance to cancel import
If MsgBox("Import may take upto 45seconds..." & vbNewLine & "Press OK to continue", vbOKCancel) = vbCancel Then Exit Sub

If Len(Dir(newdir)) = 0 Then
     MsgBox "Previous Month Tracker not found " & vbNewLine & "Please enter results manually", vbOKOnly, "Import Section"
     Exit Sub
    Else
Application.StatusBar = "Importing Section Info..." & xx & "%"
Application.ScreenUpdating = False
Dim teststr As String
    For i = 5 To 26
        If i <> 25 Then
        teststr = "='" & formulapth & "Section Info'!R" & i & "C4"
        ws.Range("D" & i).FormulaR1C1 = teststr
            If ws.Range("D" & i).Value = 0 Then
            ws.Range("D" & i).ClearContents
            Else
            ws.Range("D" & i).Value = ws.Range("D" & i).Value
            End If
        x = (x + 1)
        xx = ((x / y)) * 100
        Application.StatusBar = "Importing Section Info..." & xx & "%"
        End If
Next i
Application.ScreenUpdating = True
Application.ScreenUpdating = False
    For i = 5 To 26
        If i <> 25 Then
            teststr = "='" & formulapth & "Section Info'!R" & i & "C5"
            ws.Range("E" & i).FormulaR1C1 = teststr
            If ws.Range("E" & i).Value = 0 Then
            ws.Range("E" & i).ClearContents
            Else
            ws.Range("E" & i).Value = ws.Range("E" & i).Value
            End If
        x = (x + 1)
        xx = ((x / y)) * 100
        Application.StatusBar = "Importing Section Info..." & xx & "%"
        End If
Next i
Application.ScreenUpdating = True
Application.ScreenUpdating = False
    For i = 5 To 26
        If i <> 25 Then
        teststr = "='" & formulapth & "Section Info'!R" & i & "C6"
        ws.Range("F" & i).FormulaR1C1 = teststr
        ws.Range("F" & i).Value = ws.Range("F" & i).Value
            If ws.Range("F" & i).Value = 0 Then
            ws.Range("F" & i).ClearContents
            Else
            ws.Range("F" & i).Value = ws.Range("F" & i).Value
            End If
        x = (x + 1)
        xx = ((x / y)) * 100
        Application.StatusBar = "Importing Section Info..." & xx & "%"
        End If
Next i
    For i = 5 To 26
        If i <> 25 Then
        teststr = "='" & formulapth & "Section Info'!R" & i & "C7"
        ws.Range("G" & i).FormulaR1C1 = teststr
            If ws.Range("G" & i).Value = 0 Then
            ws.Range("G" & i).ClearContents
            Else
            ws.Range("G" & i).Value = ws.Range("G" & i).Value
            End If
        x = (x + 1)
        xx = ((x / y)) * 100
        Application.StatusBar = "Importing Section Info..." & xx & "%"
        End If
Next i
MsgBox "FINISHED!"
Application.ScreenUpdating = True
Application.StatusBar = False
End If

End Sub

My error is in the second replace function.

enter image description here

Upvotes: 0

Views: 321

Answers (1)

Marc
Marc

Reputation: 11613

I simplified your code to this:

Sub agetprevsectinfo()
Dim wb As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim x As Integer
Dim xx As Integer
Dim y As Integer
Dim newdir As String
Dim formulapth As String
Dim curmnth As String
Dim prevmnth As String
x = 0
xx = 0
y = 104
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet
curmnth = "03"  'hard-coded
prevmnth = "02" 'hard-coded

newdir = wb.Path & "\" & Replace(Replace(wb.Name, curmnth, prevmnth), MonthName(CDate(curmnth)), MonthName(CDate(prevmnth)))
formulapth = wb.Path & "\[" & Replace(Replace(wb.Name, curmnth, prevmnth), MonthName(CDate(curmnth)), MonthName(CDate(prevmnth))) & "]"
'last chance to cancel import
MsgBox (formulapth)

End Sub

And it works.

Everything about that code matches yours except I hard-coded curmnth and prevmnth and cut out everything after the line you say is failing.

In other words, you have not correctly identified the problem (assuming my hard-coded values are what your worksheets actually contain, but I based them on your screenshot).

I realize this is not a good "Answer," but I'm not sure how else to convey it.

Upvotes: 1

Related Questions