• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need assistance with vba code

gdraco123

New Member
I have the below vba code on my workbook, but I’m getting the below runtime error, please advice.

>>> use code - tags <<<
Code:
Sub CopyMyWorkSheet()   
 
Dim sNm As String    
sNm = "MyWorkSheet " & Format(DateAdd("m", 1, [E3].Value), "mm-dd-yy")       

If Not WksExists(wsc.Name) Then  <  Run-time error 424; Object required…here
 
ThisWorkbook.Unprotected "Password”"       
ActiveSheet.Copy       
ActiveSheet.Name = sNm       
ThisWorkbook.Protect”Password”       
Else: Exit Sub   
End If   
End Sub



''///this code in a separate module  Function

Function WksExists(wksName As String) As Boolean   
On Error Resume Next   
WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
 
Last edited by a moderator:
Add first the code tags in your initial post just using the Code option via the 3 dots menu.​
Your issue comes from a logic error as the object variable is empty and this codeline has no sense when this variable is not empty !​
 
My apologies, I’m a beginner and have no idea what you mean with, “Add first the code tags in your initial post just using the Code option via the 3 dots menu.

Thanks…
 
gdraco123
Everybody can always reread Forum Rules from
and those code - tags, can find from:
 
Hello p45cal,

Thank you so much for your response

Like I mentioned above I’m a beginner and I got the above vba code from a website. Not sure how to fix it, does it mean I have to Dim “wcs”?

Cheers,
 
I have a invoice workbook that runs monthly with running balances and tabs for each month. And I want to automate it a bit, I want to create a macro button that will copy the latest month tab and create the following month tab all within the same workbook. And at the same time avoid duplicates if the button is clicked more than once in error. Hope this helps. Thanks..

Cheers,
 
try changing:
Code:
ActiveSheet.Copy
which will create a new workbook, to:
Code:
ActiveSheet.Copy After:=Sheets(Sheets.count)
which will add a sheet to the same workbook as the rightmost sheet.
 
Back
Top